转自:http://meiowei.iteye.com/blog/413919
这个只是个简单AOP例子,包括前置通知,后置通知,环绕通知,和目标对象。写这个例子的主要目标只是想让想学AOP的能更快地入门,了解一下如何去配置AOP里面的东东。
目标对象的接口:IStudent.java
-
1
-
-
-
4packagecom.dragon.study;
-
5
-
6
-
-
-
-
10publicinterfaceIStudent{
-
11
-
12publicvoidaddStudent(Stringname);
-
13}
目标类:StudentImpl.java
-
1
-
-
-
4packagecom.dragon.study.Impl;
-
5
-
6importcom.dragon.study.IStudent;
-
7
-
8
-
-
-
-
12publicclassStudentImplimplementsIStudent{
-
13
-
14publicvoidaddStudent(Stringname){
-
15System.out.println("欢迎"+name+"你加入Spring家庭!");
-
16}
-
17}
-
18
前置通知:BeforeAdvice.java
-
1
-
-
-
4packagecom.dragon.Advice;
-
5
-
6importjava.lang.reflect.Method;
-
7
-
8importorg.springframework.aop.MethodBeforeAdvice;
-
9
-
10
-
-
-
-
14publicclassBeforeAdviceimplementsMethodBeforeAdvice{
-
15
-
16publicvoidbefore(Methodmethod,Object[]args,Objecttarget)
-
17throwsThrowable{
-
18
-
19System.out.println("这是BeforeAdvice类的before方法.");
-
20
-
21}
-
22}
后置通知:AfterAdvice.java
-
1
-
-
-
4packagecom.dragon.Advice;
-
5
-
6importjava.lang.reflect.Method;
-
7
-
8importorg.springframework.aop.AfterReturningAdvice;
-
9
-
10
-
-
-
-
14publicclassAfterAdviceimplementsAfterReturningAdvice{
-
15
-
16publicvoidafterReturning(ObjectreturnValue,Methodmethod,
-
17Object[]args,Objecttarget)throwsThrowable{
-
18System.out.println("这是AfterAdvice类的afterReturning方法.");
-
19}
-
20
-
21
-
22}
环绕通知:CompareInterceptor.java
-
1
-
-
-
4packagecom.dragon.Advice;
-
5
-
6importorg.aopalliance.intercept.MethodInterceptor;
-
7importorg.aopalliance.intercept.MethodInvocation;
-
8
-
9
-
10
-
-
-
-
14publicclassCompareInterceptorimplementsMethodInterceptor{
-
15
-
16publicObjectinvoke(MethodInvocationinvocation)throwsThrowable{
-
17Objectresult=null;
-
18Stringstu_name=invocation.getArguments()[0].toString();
-
19if(stu_name.equals("dragon")){
-
20
-
21result=invocation.proceed();
-
22
-
23}else{
-
24System.out.println("此学生是"+stu_name+"而不是dragon,不批准其加入.");
-
25}
-
26
-
27returnresult;
-
28}
-
29}
配置文件applicationContext.xml
-
1<?xmlversion="1.0"encoding="UTF-8"?>
-
2<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
-
3
-
4<beans>
-
5
-
6<beanid="beforeAdvice"class="com.dragon.Advice.BeforeAdvice"></bean>
-
7<beanid="afterAdvice"class="com.dragon.Advice.AfterAdvice"></bean>
-
8<beanid="compareInterceptor"class="com.dragon.Advice.CompareInterceptor"></bean>
-
9<beanid="studenttarget"class="com.dragon.study.Impl.StudentImpl"></bean>
-
10
-
11<beanid="student"class="org.springframework.aop.framework.ProxyFactoryBean">
-
12<propertyname="proxyInterfaces">
-
13<value>com.dragon.study.IStudent</value>
-
14</property>
-
15<propertyname="interceptorNames">
-
16<list>
-
17<value>beforeAdvice</value>
-
18<value>afterAdvice</value>
-
19<value>compareInterceptor</value>
-
20</list>
-
21</property>
-
22<propertyname="target">
-
23<refbean="studenttarget"/>
-
24</property>
-
25
-
26</bean>
-
27
-
28</beans>
现在开始写测试类,Test.java
-
1
-
-
-
4packagecom;
-
5
-
6importorg.springframework.context.ApplicationContext;
-
7importorg.springframework.context.support.FileSystemXmlApplicationContext;
-
8
-
9importcom.dragon.study.IStudent;
-
10
-
11
-
-
-
-
15publicclassTest{
-
16
-
17
-
-
-
20publicstaticvoidmain(String[]args){
-
21
-
22ApplicationContextctx=
-
23newFileSystemXmlApplicationContext("/com/dragon/applicationContext.xml");
-
24
-
25IStudentperson=(IStudent)ctx.getBean("student");
-
26person.addStudent("dragon");
-
27
-
28
-
29}
-
30
-
31}
-
32
当然,运行该程序时,要加上commons-logging.jar 包
在Spring Appactiocontext.xml配置文件;你定义的前置,后置;环绕等通知在配置文件中实现了代理(org.springframework.aop.framework.ProxyFactoryBean)
以此将通知放入到了原Bean中;这样才能使原Bean中方法调用时自动执行通知
这是其一》
<property name="proxyInterfaces">
<property name="interceptorNames">
<property name="target">
这三个属性是一定要配置的
第一是被代理的接口(IStudent)
第二是通知列表(前置,后置;环绕)上面定义的三个类
第三是被代理的原Bean(StudentImpl )
分享到:
相关推荐
NULL 博文链接:https://306963591.iteye.com/blog/1129837
spring aop例子,这是spring aop例子哦
spring的aop简单例子spring的aop简单例子spring的aop简单例子spring的aop简单例子
Spring AOP 几个不同使用方法的完整例子,使用Junit4c测试, 在我的博客上有不同配置组合的说明,可以参考
spring aop简单例子,入门学习的好资料
spring aop 经典例子(原创),无论新手还是老手,可以快速对Spring AOP有个总体的认识。基于JDK1.6,Spring2.0.5,Eclipse IDE。
最近做的Spring 2.5 AOP 例子,保留下以后复习说不定用得上. 1.使用类扫描功能这个有点意思.使用时方便了许多. 2.XML配置方式
spring aop 通过注解实现的项目,这只是一个简单的例子。运行环境也简单。
eclipse的SpringAOP和动态代理的小例子
Spring.net AOP 动态代理例子
全面讲解spring Aop 功能,例子丰富。
Spring AOP 日志管理 实例LoggingThrowsAdvice.java
找了很久,也没在网上找到spring mvc框架下的aop例子~研究了好久终于写出来了~拿出来和大家分享一下~
springAOP小例子 装配器 切入点 通知等
实现了springAOP 适合新手进行学习
spring aop 自定义切面示例 aspectj 需要相应的架包支持
Spring3.1AOP简单例子,需要的jar包分开上传了
Spring IOC AOP MVC 简单例子
利用SPring AOP配置切面的一个例子
Spring aop的一个小例子,里面带jar包