SpringAOP简单例子
这个只是个简单AOP例子,包括前置通知,后置通知,环绕通知,和目标对象。写这个例子的主要目标只是想让想学AOP的能更快地入门,了解一下如何去配置AOP里面的东东。
目标对象的接口:IStudent.java
1
目标类:StudentImpl.java
1
前置通知:BeforeAdvice.java
1
后置通知:AfterAdvice.java
1
环绕通知:CompareInterceptor.java
1
配置文件applicationContext.xml
1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3
4<beans>
5
6<bean id="beforeAdvice" class="com.dragon.Advice.BeforeAdvice"></bean>
7<bean id="afterAdvice" class="com.dragon.Advice.AfterAdvice"></bean>
8<bean id="compareInterceptor" class="com.dragon.Advice.CompareInterceptor"></bean>
9<bean id="studenttarget" class="com.dragon.study.Impl.StudentImpl"></bean>
10
11<bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean">
12 <property name="proxyInterfaces">
13 <value>com.dragon.study.IStudent</value>
14 </property>
15 <property name="interceptorNames">
16 <list>
17 <value>beforeAdvice</value>
18 <value>afterAdvice</value>
19 <value>compareInterceptor</value>
20 </list>
21 </property>
22 <property name="target">
23 <ref bean="studenttarget"/>
24 </property>
25
26</bean>
27
28
29
30
31</beans>
2<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3
4<beans>
5
6<bean id="beforeAdvice" class="com.dragon.Advice.BeforeAdvice"></bean>
7<bean id="afterAdvice" class="com.dragon.Advice.AfterAdvice"></bean>
8<bean id="compareInterceptor" class="com.dragon.Advice.CompareInterceptor"></bean>
9<bean id="studenttarget" class="com.dragon.study.Impl.StudentImpl"></bean>
10
11<bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean">
12 <property name="proxyInterfaces">
13 <value>com.dragon.study.IStudent</value>
14 </property>
15 <property name="interceptorNames">
16 <list>
17 <value>beforeAdvice</value>
18 <value>afterAdvice</value>
19 <value>compareInterceptor</value>
20 </list>
21 </property>
22 <property name="target">
23 <ref bean="studenttarget"/>
24 </property>
25
26</bean>
27
28
29
30
31</beans>
现在开始写测试类,Test.java
1