一个简单的Spring的AOP例子
目标对象的接口:IStudent.java
1
/**
2
*
3
*/
4
package com.dragon.study;
5
6
/**
7
* @author dragon
8
*
9
*/
10
public interface IStudent {
11
12
public void addStudent(String name);
13
}
14![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
3
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
5
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
6
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
8
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
9
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
10
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
11
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
12
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
13
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
14
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
目标类:StudentImpl.java
1
/**
2
*
3
*/
4
package com.dragon.study.Impl;
5
6
import com.dragon.study.IStudent;
7
8
/**
9
* @author dragon
10
*
11
*/
12
public class StudentImpl implements IStudent {
13
14
public void addStudent(String name) {
15
System.out.println( " 欢迎 " + name + " 你加入Spring家庭! " );
16
}
17
}
18![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
3
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
5
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
6
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
7
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
8
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
9
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
10
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
11
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
12
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
13
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
14
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
15
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
16
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
18
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
前置通知:BeforeAdvice.java
1
/**
2
*
3
*/
4
package com.dragon.Advice;
5
6
import java.lang.reflect.Method;
7
8
import org.springframework.aop.MethodBeforeAdvice;
9
10
/**
11
* @author dragon
12
*
13
*/
14
public class BeforeAdvice implements MethodBeforeAdvice {
15
16
public void before(Method method,Object[] args, Object target)
17
throws Throwable {
18
19
System.out.println( " 这是BeforeAdvice类的before方法. " );
20
21
}
22
}
23![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
3
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
5
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
6
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
7
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
8
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
9
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
10
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
11
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
12
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
13
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
14
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
15
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
16
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
17
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
19
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
20
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
21
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
23
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
后置通知:AfterAdvice.java
1
/**
2
*
3
*/
4
package com.dragon.Advice;
5![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
6
import java.lang.reflect.Method;
7![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
8
import org.springframework.aop.AfterReturningAdvice;
9![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
10
/**
11
* @author dragon
12
*
13
*/
14
public class AfterAdvice implements AfterReturningAdvice{
15
16
public void afterReturning(Object returnValue ,Method method,
17
Object[] args,Object target) throws Throwable{
18
System.out.println("这是AfterAdvice类的afterReturning方法.");
19
}
20
21![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
22
}
23![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
3
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
5
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
6
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
7
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
8
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
9
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
10
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
11
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
12
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
13
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
14
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
15
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
16
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
17
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
19
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
21
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
22
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
23
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
环绕通知:CompareInterceptor.java
1
/**
2
*
3
*/
4
package com.dragon.Advice;
5![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
6
import org.aopalliance.intercept.MethodInterceptor;
7
import org.aopalliance.intercept.MethodInvocation;
8![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
9![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
10
/**
11
* @author dragon
12
*
13
*/
14
public class CompareInterceptor implements MethodInterceptor{
15![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
16
public Object invoke(MethodInvocation invocation) throws Throwable{
17
Object result = null;
18
String stu_name = invocation.getArguments()[0].toString();
19
if ( stu_name.equals("dragon")){
20
//如果学生是dragon时,执行目标方法,
21
result= invocation.proceed();
22
23
} else{
24
System.out.println("此学生是"+stu_name+"而不是dragon,不批准其加入.");
25
}
26
27
return result;
28
}
29
}
30![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
3
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
5
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
6
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
7
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
8
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
9
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
10
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
11
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
12
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
13
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
14
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
15
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
16
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
18
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
19
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
21
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
22
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
23
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
25
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
27
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
28
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
29
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
30
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
配置文件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![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
4
<beans>
5![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
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![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
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![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
26
</bean>
27![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
28![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
29![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
30![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
31
</beans>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
2
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
3
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
4
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
5
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
6
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
7
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
8
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
9
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
10
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
11
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
12
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
13
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
14
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
15
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
16
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
17
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
18
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
19
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
20
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
21
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
22
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
23
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
24
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
25
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
26
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
27
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
28
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
29
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
30
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
31
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
现在开始写测试类,Test.java
1
/**
2
*
3
*/
4
package com;
5![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
6
import org.springframework.context.ApplicationContext;
7
import org.springframework.context.support.FileSystemXmlApplicationContext;
8![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
9
import com.dragon.study.IStudent;
10![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
11
/**
12
* @author dragon
13
*
14
*/
15
public class Test {
16![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
17
/**
18
* @param args
19
*/
20
public static void main(String[] args) {
21
// TODO Auto-generated method stub
22
ApplicationContext ctx =
23
new FileSystemXmlApplicationContext("/com/dragon/applicationContext.xml");
24
25
IStudent person = (IStudent)ctx.getBean("student");
26
person.addStudent("dragon");
27
28
// person.addStudent("javadragon");
29
}
30![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
31
}
32![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
3
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
5
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
6
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
7
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
8
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
9
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
10
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
11
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
12
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
13
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
14
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
15
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
16
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
17
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
19
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
22
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
23
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
24
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
25
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
26
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
27
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
28
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
29
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
30
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
31
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
32
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)