1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| public class MockAopBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware {
private ApplicationContext applicationContext;
@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean.getClass().getPackage().getName().equals("com.nju.service.impl")) { Object beanProxy = Proxy.newProxyInstance( bean.getClass().getClassLoader(), bean.getClass().getInterfaces(), (Object proxy, Method method, Object[] args) -> { MyAdvice myAdvice = applicationContext.getBean(MyAdvice.class); myAdvice.beforeAdvice(); Object result = method.invoke(bean, args); myAdvice.afterAdvice(); return result; } ); return beanProxy; } return bean; }
@Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }
|