Skip to content

Commit b8755b3

Browse files
committed
beanfactoryaware
1 parent 79e676f commit b8755b3

File tree

5 files changed

+64
-4
lines changed

5 files changed

+64
-4
lines changed

src/main/java/us/codecraft/tinyioc/aop/AdvisedSupport.java

+10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class AdvisedSupport {
1212

1313
private MethodInterceptor methodInterceptor;
1414

15+
private MethodMatcher methodMatcher;
16+
1517
public TargetSource getTargetSource() {
1618
return targetSource;
1719
}
@@ -27,4 +29,12 @@ public MethodInterceptor getMethodInterceptor() {
2729
public void setMethodInterceptor(MethodInterceptor methodInterceptor) {
2830
this.methodInterceptor = methodInterceptor;
2931
}
32+
33+
public MethodMatcher getMethodMatcher() {
34+
return methodMatcher;
35+
}
36+
37+
public void setMethodMatcher(MethodMatcher methodMatcher) {
38+
this.methodMatcher = methodMatcher;
39+
}
3040
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,48 @@
11
package us.codecraft.tinyioc.aop;
22

3+
import org.aopalliance.intercept.MethodInterceptor;
34
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;
49

510
/**
611
712
*/
8-
public class AspectJAwareAdvisorAutoProxyCreator implements BeanPostProcessor {
13+
public class AspectJAwareAdvisorAutoProxyCreator implements BeanPostProcessor, BeanFactoryAware {
14+
15+
private AbstractBeanFactory beanFactory;
16+
917
@Override
1018
public Object postProcessBeforeInitialization(Object bean, String beanName) throws Exception {
1119
return bean;
1220
}
1321

1422
@Override
1523
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+
}
1641
return bean;
1742
}
43+
44+
@Override
45+
public void setBeanFactory(BeanFactory beanFactory) throws Exception {
46+
this.beanFactory = (AbstractBeanFactory) beanFactory;
47+
}
1848
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package us.codecraft.tinyioc.aop;
2+
3+
import us.codecraft.tinyioc.beans.factory.BeanFactory;
4+
5+
/**
6+
7+
*/
8+
public interface BeanFactoryAware {
9+
10+
void setBeanFactory(BeanFactory beanFactory) throws Exception;
11+
}

src/main/java/us/codecraft/tinyioc/aop/JdkDynamicAopProxy.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public JdkDynamicAopProxy(AdvisedSupport advised) {
1919
this.advised = advised;
2020
}
2121

22-
@Override
22+
@Override
2323
public Object getProxy() {
2424
return Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { advised.getTargetSource()
2525
.getTargetClass() }, this);
@@ -28,8 +28,13 @@ public Object getProxy() {
2828
@Override
2929
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
3030
MethodInterceptor methodInterceptor = advised.getMethodInterceptor();
31-
return methodInterceptor.invoke(new ReflectiveMethodInvocation(advised.getTargetSource().getTarget(), method,
32-
args));
31+
if (advised.getMethodMatcher() != null
32+
&& advised.getMethodMatcher().matches(method, advised.getTargetSource().getTarget().getClass())) {
33+
return methodInterceptor.invoke(new ReflectiveMethodInvocation(advised.getTargetSource().getTarget(),
34+
method, args));
35+
} else {
36+
return method.invoke(advised.getTargetSource().getTarget(), args);
37+
}
3338
}
3439

3540
}

src/main/java/us/codecraft/tinyioc/beans/factory/AutowireCapableBeanFactory.java

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package us.codecraft.tinyioc.beans.factory;
22

33
import us.codecraft.tinyioc.BeanReference;
4+
import us.codecraft.tinyioc.aop.BeanFactoryAware;
45
import us.codecraft.tinyioc.beans.BeanDefinition;
56
import us.codecraft.tinyioc.beans.PropertyValue;
67

@@ -14,6 +15,9 @@
1415
public class AutowireCapableBeanFactory extends AbstractBeanFactory {
1516

1617
protected void applyPropertyValues(Object bean, BeanDefinition mbd) throws Exception {
18+
if (bean instanceof BeanFactoryAware){
19+
((BeanFactoryAware)bean).setBeanFactory(this);
20+
}
1721
for (PropertyValue propertyValue : mbd.getPropertyValues().getPropertyValues()) {
1822
Field declaredField = bean.getClass().getDeclaredField(propertyValue.getName());
1923
declaredField.setAccessible(true);

0 commit comments

Comments
 (0)