一个简单的spring AOP例子

系统 1542 0

转自: http://meiowei.iteye.com/blog/413919

这个只是个简单AOP例子,包括前置通知,后置通知,环绕通知,和目标对象。写这个例子的主要目标只是想让想学AOP的能更快地入门,了解一下如何去配置AOP里面的东东。
目标对象的接口:IStudent.java

Java代码 收藏代码
  1. 1 /**
  2. 2*
  3. 3*/
  4. 4 package com.dragon.study;
  5. 5
  6. 6 /**
  7. 7*@authordragon
  8. 8*
  9. 9*/
  10. 10 public interface IStudent{
  11. 11
  12. 12 public void addStudent(Stringname);
  13. 13 }

目标类:StudentImpl.java

Java代码 收藏代码
  1. 1 /**
  2. 2*
  3. 3*/
  4. 4 package com.dragon.study.Impl;
  5. 5
  6. 6 import com.dragon.study.IStudent;
  7. 7
  8. 8 /**
  9. 9*@authordragon
  10. 10*
  11. 11*/
  12. 12 public class StudentImpl implements IStudent{
  13. 13
  14. 14 public void addStudent(Stringname){
  15. 15 System.out.println( "欢迎" +name+ "你加入Spring家庭!" );
  16. 16 }
  17. 17 }
  18. 18

前置通知:BeforeAdvice.java

Java代码 收藏代码
  1. 1 /**
  2. 2*
  3. 3*/
  4. 4 package com.dragon.Advice;
  5. 5
  6. 6 import java.lang.reflect.Method;
  7. 7
  8. 8 import org.springframework.aop.MethodBeforeAdvice;
  9. 9
  10. 10 /**
  11. 11*@authordragon
  12. 12*
  13. 13*/
  14. 14 public class BeforeAdvice implements MethodBeforeAdvice{
  15. 15
  16. 16 public void before(Methodmethod,Object[]args,Objecttarget)
  17. 17 throws Throwable{
  18. 18
  19. 19 System.out.println( "这是BeforeAdvice类的before方法." );
  20. 20
  21. 21 }
  22. 22 }

后置通知:AfterAdvice.java

Java代码 收藏代码
  1. 1 /**
  2. 2*
  3. 3*/
  4. 4packagecom.dragon.Advice;
  5. 5
  6. 6importjava.lang.reflect.Method;
  7. 7
  8. 8importorg.springframework.aop.AfterReturningAdvice;
  9. 9
  10. 10 /**
  11. 11*@authordragon
  12. 12*
  13. 13*/
  14. 14public class AfterAdvice implements AfterReturningAdvice{
  15. 15
  16. 16 public void afterReturning(ObjectreturnValue,Methodmethod,
  17. 17 Object[]args,Objecttarget) throws Throwable{
  18. 18 System.out.println( "这是AfterAdvice类的afterReturning方法." );
  19. 19 }
  20. 20
  21. 21
  22. 22 }

环绕通知:CompareInterceptor.java

Java代码 收藏代码
  1. 1 /**
  2. 2*
  3. 3*/
  4. 4packagecom.dragon.Advice;
  5. 5
  6. 6importorg.aopalliance.intercept.MethodInterceptor;
  7. 7importorg.aopalliance.intercept.MethodInvocation;
  8. 8
  9. 9
  10. 10 /**
  11. 11*@authordragon
  12. 12*
  13. 13*/
  14. 14public class CompareInterceptor implements MethodInterceptor{
  15. 15
  16. 16 public Objectinvoke(MethodInvocationinvocation) throws Throwable{
  17. 17 Objectresult= null ;
  18. 18 Stringstu_name=invocation.getArguments()[ 0 ].toString();
  19. 19 if (stu_name.equals( "dragon" )){
  20. 20 //如果学生是dragon时,执行目标方法,
  21. 21 result=invocation.proceed();
  22. 22
  23. 23 } else {
  24. 24 System.out.println( "此学生是" +stu_name+ "而不是dragon,不批准其加入." );
  25. 25 }
  26. 26
  27. 27 return result;
  28. 28 }
  29. 29 }

配置文件applicationContext.xml

Java代码 收藏代码
  1. 1 <?xmlversion= "1.0" encoding= "UTF-8" ?>
  2. 2 <!DOCTYPEbeansPUBLIC "-//SPRING//DTDBEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
  3. 3
  4. 4 <beans>
  5. 5
  6. 6 <beanid= "beforeAdvice" class = "com.dragon.Advice.BeforeAdvice" ></bean>
  7. 7 <beanid= "afterAdvice" class = "com.dragon.Advice.AfterAdvice" ></bean>
  8. 8 <beanid= "compareInterceptor" class = "com.dragon.Advice.CompareInterceptor" ></bean>
  9. 9 <beanid= "studenttarget" class = "com.dragon.study.Impl.StudentImpl" ></bean>
  10. 10
  11. 11 <beanid= "student" class = "org.springframework.aop.framework.ProxyFactoryBean" >
  12. 12 <propertyname= "proxyInterfaces" >
  13. 13 <value>com.dragon.study.IStudent</value>
  14. 14 </property>
  15. 15 <propertyname= "interceptorNames" >
  16. 16 <list>
  17. 17 <value>beforeAdvice</value>
  18. 18 <value>afterAdvice</value>
  19. 19 <value>compareInterceptor</value>
  20. 20 </list>
  21. 21 </property>
  22. 22 <propertyname= "target" >
  23. 23 <refbean= "studenttarget" />
  24. 24 </property>
  25. 25
  26. 26 </bean>
  27. 27
  28. 28 </beans>

现在开始写测试类,Test.java

Java代码 收藏代码
  1. 1 /**
  2. 2*
  3. 3*/
  4. 4packagecom;
  5. 5
  6. 6importorg.springframework.context.ApplicationContext;
  7. 7importorg.springframework.context.support.FileSystemXmlApplicationContext;
  8. 8
  9. 9importcom.dragon.study.IStudent;
  10. 10
  11. 11 /**
  12. 12*@authordragon
  13. 13*
  14. 14*/
  15. 15public class Test{
  16. 16
  17. 17 /**
  18. 18*@paramargs
  19. 19*/
  20. 20 public static void main(String[]args){
  21. 21 //TODOAuto-generatedmethodstub
  22. 22 ApplicationContextctx=
  23. 23 new FileSystemXmlApplicationContext( "/com/dragon/applicationContext.xml" );
  24. 24
  25. 25 IStudentperson=(IStudent)ctx.getBean( "student" );
  26. 26 person.addStudent( "dragon" );
  27. 27
  28. 28 //person.addStudent("javadragon");
  29. 29 }
  30. 30
  31. 31 }
  32. 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 )

一个简单的spring AOP例子


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论