|
1 | 1 | package us.codecraft.tinyioc.aop;
|
2 | 2 |
|
| 3 | +import org.aopalliance.intercept.MethodInterceptor; |
3 | 4 | import us.codecraft.tinyioc.beans.BeanPostProcessor;
|
| 5 | +import us.codecraft.tinyioc.beans.factory.AbstractBeanFactory; |
| 6 | +import us.codecraft.tinyioc.beans.factory.BeanFactory; |
| 7 | + |
| 8 | +import java.util.List; |
4 | 9 |
|
5 | 10 | /**
|
6 | 11 |
|
7 | 12 | */
|
8 |
| -public class AspectJAwareAdvisorAutoProxyCreator implements BeanPostProcessor { |
| 13 | +public class AspectJAwareAdvisorAutoProxyCreator implements BeanPostProcessor, BeanFactoryAware { |
| 14 | + |
| 15 | + private AbstractBeanFactory beanFactory; |
| 16 | + |
9 | 17 | @Override
|
10 | 18 | public Object postProcessBeforeInitialization(Object bean, String beanName) throws Exception {
|
11 | 19 | return bean;
|
12 | 20 | }
|
13 | 21 |
|
14 | 22 | @Override
|
15 | 23 | public Object postProcessAfterInitialization(Object bean, String beanName) throws Exception {
|
| 24 | + if (bean instanceof AspectJExpressionPointcutAdvisor) { |
| 25 | + return bean; |
| 26 | + } |
| 27 | + List<AspectJExpressionPointcutAdvisor> advisors = beanFactory |
| 28 | + .getBeansForType(AspectJExpressionPointcutAdvisor.class); |
| 29 | + for (AspectJExpressionPointcutAdvisor advisor : advisors) { |
| 30 | + if (advisor.getPointcut().getClassFilter().matches(bean.getClass())) { |
| 31 | + AdvisedSupport advisedSupport = new AdvisedSupport(); |
| 32 | + advisedSupport.setMethodInterceptor((MethodInterceptor) advisor.getAdvice()); |
| 33 | + advisedSupport.setMethodMatcher(advisor.getPointcut().getMethodMatcher()); |
| 34 | + |
| 35 | + TargetSource targetSource = new TargetSource(bean, bean.getClass()); |
| 36 | + advisedSupport.setTargetSource(targetSource); |
| 37 | + |
| 38 | + return new JdkDynamicAopProxy(advisedSupport); |
| 39 | + } |
| 40 | + } |
16 | 41 | return bean;
|
17 | 42 | }
|
| 43 | + |
| 44 | + @Override |
| 45 | + public void setBeanFactory(BeanFactory beanFactory) throws Exception { |
| 46 | + this.beanFactory = (AbstractBeanFactory) beanFactory; |
| 47 | + } |
18 | 48 | }
|
0 commit comments