Skip to content

Commit 79e676f

Browse files
committed
aspectj advice
1 parent df8b8ac commit 79e676f

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package us.codecraft.tinyioc.aop;
2+
3+
import org.aopalliance.aop.Advice;
4+
import org.aopalliance.intercept.MethodInterceptor;
5+
import org.aopalliance.intercept.MethodInvocation;
6+
import us.codecraft.tinyioc.beans.factory.BeanFactory;
7+
8+
import java.lang.reflect.Method;
9+
10+
/**
11+
12+
*/
13+
public class AspectJAroundAdvice implements Advice, MethodInterceptor {
14+
15+
private BeanFactory beanFactory;
16+
17+
private Method aspectJAdviceMethod;
18+
19+
private String aspectInstanceName;
20+
21+
@Override
22+
public Object invoke(MethodInvocation invocation) throws Throwable {
23+
return aspectJAdviceMethod.invoke(beanFactory.getBean(aspectInstanceName), invocation);
24+
}
25+
26+
public BeanFactory getBeanFactory() {
27+
return beanFactory;
28+
}
29+
30+
public void setBeanFactory(BeanFactory beanFactory) {
31+
this.beanFactory = beanFactory;
32+
}
33+
34+
public Method getAspectJAdviceMethod() {
35+
return aspectJAdviceMethod;
36+
}
37+
38+
public void setAspectJAdviceMethod(Method aspectJAdviceMethod) {
39+
this.aspectJAdviceMethod = aspectJAdviceMethod;
40+
}
41+
42+
public String getAspectInstanceName() {
43+
return aspectInstanceName;
44+
}
45+
46+
public void setAspectInstanceName(String aspectInstanceName) {
47+
this.aspectInstanceName = aspectInstanceName;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package us.codecraft.tinyioc.aop;
2+
3+
import us.codecraft.tinyioc.beans.BeanPostProcessor;
4+
5+
/**
6+
7+
*/
8+
public class AspectJAwareAdvisorAutoProxyCreator implements BeanPostProcessor {
9+
@Override
10+
public Object postProcessBeforeInitialization(Object bean, String beanName) throws Exception {
11+
return bean;
12+
}
13+
14+
@Override
15+
public Object postProcessAfterInitialization(Object bean, String beanName) throws Exception {
16+
return bean;
17+
}
18+
}

src/test/java/us/codecraft/tinyioc/aop/TimerInterceptor.java

+1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
1717
+ " nanoseconds.");
1818
return proceed;
1919
}
20+
2021
}

src/test/resources/tinyioc.xml

+11
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,15 @@
1717
<property name="outputService" ref="outputService"></property>
1818
</bean>
1919

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+
2031
</beans>

0 commit comments

Comments
 (0)