Skip to content

Commit 4a92f2e

Browse files
committed
aop complete
1 parent b8755b3 commit 4a92f2e

10 files changed

+56
-43
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
2424
if (bean instanceof AspectJExpressionPointcutAdvisor) {
2525
return bean;
2626
}
27+
if (bean instanceof MethodInterceptor) {
28+
return bean;
29+
}
2730
List<AspectJExpressionPointcutAdvisor> advisors = beanFactory
2831
.getBeansForType(AspectJExpressionPointcutAdvisor.class);
2932
for (AspectJExpressionPointcutAdvisor advisor : advisors) {
@@ -32,10 +35,10 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
3235
advisedSupport.setMethodInterceptor((MethodInterceptor) advisor.getAdvice());
3336
advisedSupport.setMethodMatcher(advisor.getPointcut().getMethodMatcher());
3437

35-
TargetSource targetSource = new TargetSource(bean, bean.getClass());
38+
TargetSource targetSource = new TargetSource(bean, bean.getClass().getInterfaces());
3639
advisedSupport.setTargetSource(targetSource);
3740

38-
return new JdkDynamicAopProxy(advisedSupport);
41+
return new JdkDynamicAopProxy(advisedSupport).getProxy();
3942
}
4043
}
4144
return bean;

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ public JdkDynamicAopProxy(AdvisedSupport advised) {
2121

2222
@Override
2323
public Object getProxy() {
24-
return Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { advised.getTargetSource()
25-
.getTargetClass() }, this);
24+
return Proxy.newProxyInstance(getClass().getClassLoader(), advised.getTargetSource().getTargetClass(), this);
2625
}
2726

2827
@Override

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
*/
77
public class TargetSource {
88

9-
private Class targetClass;
9+
private Class<?>[] targetClass;
1010

1111
private Object target;
1212

13-
public TargetSource(Object target, Class<?> targetClass) {
13+
public TargetSource(Object target, Class<?>... targetClass) {
1414
this.target = target;
1515
this.targetClass = targetClass;
1616
}
1717

18-
public Class getTargetClass() {
18+
public Class<?>[] getTargetClass() {
1919
return targetClass;
2020
}
2121

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ public Object getBean(String name) throws Exception {
2929
Object bean = beanDefinition.getBean();
3030
if (bean == null) {
3131
bean = doCreateBean(beanDefinition);
32-
initializeBean(bean, name);
32+
bean = initializeBean(bean, name);
3333
}
3434
return bean;
3535
}
3636

37-
protected void initializeBean(Object bean, String name) throws Exception {
37+
protected Object initializeBean(Object bean, String name) throws Exception {
3838
for (BeanPostProcessor beanPostProcessor : beanPostProcessors) {
3939
bean = beanPostProcessor.postProcessBeforeInitialization(bean, name);
4040
}
@@ -43,6 +43,7 @@ protected void initializeBean(Object bean, String name) throws Exception {
4343
for (BeanPostProcessor beanPostProcessor : beanPostProcessors) {
4444
bean = beanPostProcessor.postProcessAfterInitialization(bean, name);
4545
}
46+
return bean;
4647
}
4748

4849
protected Object createBeanInstance(BeanDefinition beanDefinition) throws Exception {

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

+17-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import us.codecraft.tinyioc.beans.PropertyValue;
77

88
import java.lang.reflect.Field;
9+
import java.lang.reflect.Method;
910

1011
/**
1112
* 可自动装配内容的BeanFactory
@@ -15,18 +16,28 @@
1516
public class AutowireCapableBeanFactory extends AbstractBeanFactory {
1617

1718
protected void applyPropertyValues(Object bean, BeanDefinition mbd) throws Exception {
18-
if (bean instanceof BeanFactoryAware){
19-
((BeanFactoryAware)bean).setBeanFactory(this);
20-
}
19+
if (bean instanceof BeanFactoryAware) {
20+
((BeanFactoryAware) bean).setBeanFactory(this);
21+
}
2122
for (PropertyValue propertyValue : mbd.getPropertyValues().getPropertyValues()) {
22-
Field declaredField = bean.getClass().getDeclaredField(propertyValue.getName());
23-
declaredField.setAccessible(true);
2423
Object value = propertyValue.getValue();
2524
if (value instanceof BeanReference) {
2625
BeanReference beanReference = (BeanReference) value;
2726
value = getBean(beanReference.getName());
2827
}
29-
declaredField.set(bean, value);
28+
29+
try {
30+
Method declaredMethod = bean.getClass().getDeclaredMethod(
31+
"set" + propertyValue.getName().substring(0, 1).toUpperCase()
32+
+ propertyValue.getName().substring(1), value.getClass());
33+
declaredMethod.setAccessible(true);
34+
35+
declaredMethod.invoke(bean, value);
36+
} catch (NoSuchMethodException e) {
37+
Field declaredField = bean.getClass().getDeclaredField(propertyValue.getName());
38+
declaredField.setAccessible(true);
39+
declaredField.set(bean, value);
40+
}
3041
}
3142
}
3243
}

src/test/java/us/codecraft/tinyioc/HelloWorldServiceImpl.java

+1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ public void setText(String text) {
2121
public void setOutputService(OutputService outputService) {
2222
this.outputService = outputService;
2323
}
24+
2425
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
package us.codecraft.tinyioc;
22

3-
import org.junit.Assert;
4-
53
/**
64
75
*/
8-
public class OutputService {
9-
10-
private HelloWorldService helloWorldService;
11-
12-
public void output(String text){
13-
Assert.assertNotNull(helloWorldService);
14-
System.out.println(text);
15-
}
16-
17-
public void setHelloWorldService(HelloWorldService helloWorldService) {
18-
this.helloWorldService = helloWorldService;
19-
}
6+
public interface OutputService {
7+
void output(String text);
208
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package us.codecraft.tinyioc;
2+
3+
/**
4+
5+
*/
6+
public class OutputServiceImpl implements OutputService {
7+
8+
@Override
9+
public void output(String text){
10+
System.out.println(text);
11+
}
12+
13+
}

src/test/resources/tinyioc-postbeanprocessor.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
99
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
1010

11-
<bean id="outputService" class="us.codecraft.tinyioc.OutputService">
11+
<bean id="outputService" class="us.codecraft.tinyioc.OutputServiceImpl">
1212
<property name="helloWorldService" ref="helloWorldService"></property>
1313
</bean>
1414

src/test/resources/tinyioc.xml

+10-13
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,21 @@
88
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
99
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
1010

11-
<bean id="outputService" class="us.codecraft.tinyioc.OutputService">
12-
<property name="helloWorldService" ref="helloWorldService"></property>
11+
<bean id="timeInterceptor" class="us.codecraft.tinyioc.aop.TimerInterceptor"></bean>
12+
13+
<bean id="autoProxyCreator" class="us.codecraft.tinyioc.aop.AspectJAwareAdvisorAutoProxyCreator"></bean>
14+
15+
<bean id="aspectjAspect" class="us.codecraft.tinyioc.aop.AspectJExpressionPointcutAdvisor">
16+
<property name="advice" ref="timeInterceptor"></property>
17+
<property name="expression" value="execution(* us.codecraft.tinyioc.*.*(..))"></property>
18+
</bean>
19+
20+
<bean id="outputService" class="us.codecraft.tinyioc.OutputServiceImpl">
1321
</bean>
1422

1523
<bean id="helloWorldService" class="us.codecraft.tinyioc.HelloWorldServiceImpl">
1624
<property name="text" value="Hello World!"></property>
1725
<property name="outputService" ref="outputService"></property>
1826
</bean>
1927

20-
<bean id="timeInterceptor" class="us.codecraft.tinyioc.aop.TimerInterceptor"></bean>
21-
22-
<aop:config>
23-
<aop:aspect id="aroundExample" ref="timeInterceptor">
24-
<aop:pointcut id="timerPointcut" expression="execution( * *.*() )"></aop:pointcut>
25-
<aop:around
26-
pointcut-ref="timerPointcut"
27-
method="doBasicProfiling"/>
28-
</aop:aspect>
29-
</aop:config>
30-
3128
</beans>

0 commit comments

Comments
 (0)