Spring AOP 示例

解釋就懶得寫了,有代碼運行一下就知道了,jar除了Spring所必備的以外還要加上bsh-1.2b7.jar

  1. packagecom.test;
  2. /**
  3. *在線圖書銷售系統業務邏輯接口
  4. */
  5. publicinterfaceBookBiz{
  6. publicfloat[]buy(StringuserName,StringbookName,doubleprice);
  7. publicvoidcomment(StringuserName,Stringcomments);
  8. }

  1. packagecom.test;
  2. publicclassBookBizImplimplementsBookBiz{
  3. /**
  4. *購買圖書
  5. */
  6. publicfloat[]buy(StringuserName,StringbookName,doubleprice){
  7. System.out.println("業務方法buy開始執行");
  8. System.out.println("·"+userName+"購買圖書:"+bookName);
  9. System.out.println("·"+userName+"增加積分:"+(int)(price/10));
  10. System.out.println("·"+"向物流系統下發貨單");
  11. System.out.println("業務方法buy結束");
  12. returnnull;
  13. }
  14. /**
  15. *發表書評
  16. */
  17. publicvoidcomment(StringuserName,Stringcomments){
  18. System.out.println("業務方法comment開始執行");
  19. System.out.println("·"+userName+"發表書評"+comments);
  20. System.out.println("業務方法comment結束");
  21. }
  22. }
  1. packagecom.test;
  2. importjava.lang.reflect.Method;
  3. importjava.util.Arrays;
  4. importorg.aopalliance.intercept.MethodInterceptor;
  5. importorg.aopalliance.intercept.MethodInvocation;
  6. importorg.springframework.aop.AfterReturningAdvice;
  7. importorg.springframework.aop.MethodBeforeAdvice;
  8. importbsh.Interpreter;
  9. publicclassMyAdviceimplementsMethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor{
  10. /**
  11. *前通知
  12. */
  13. publicvoidbefore(Methodm,Object[]args,Objecttarget)
  14. throwsThrowable{
  15. System.out.println("前通知,調用的方法:"+m.getName()+",參數:"+Arrays.toString(args));
  16. }
  17. /**
  18. *後通知
  19. */
  20. publicvoidafterReturning(Objectonject,Methodmethod,Object[]args,
  21. Objectarg3)throwsThrowable{
  22. System.out.println("後通知,調用的方法:"+method.getName()+",參數:"+Arrays.toString(args));
  23. }
  24. /**
  25. *環繞通知,最維強大的通知,可以控制目標方法是否執行,也可以改變方法的返回值
  26. */
  27. publicObjectinvoke(MethodInvocationmethod)throwsThrowable{
  28. System.out.println("[環繞通知]");
  29. Object[]args=method.getArguments();
  30. /**
  31. *這裏我們禁止李四發表任何的評論
  32. */
  33. if(method.getMethod().getName().equals("comment")&"李四".equals(args[0])){
  34. System.out.println("屏蔽李四所有的評論");
  35. StringreturnType=method.getMethod().getReturnType().getName();
  36. if("int".equals(returnType)||"long".equals(returnType)||"float".equals(returnType)||"double".equals(returnType)||"byte".equals(returnType)||"short".equals(returnType)){
  37. //利用BeanShell構造一個內置變量返回,這裏想了好久,沒有想到什麼方法可以根據
  38. //指定數據類型返回指定的變量
  39. Interpreteri=newInterpreter();
  40. returni.eval("("+returnType+")0");
  41. }elseif("boolean".equals(returnType)){
  42. returnfalse;
  43. }
  44. returnnull;
  45. }else{
  46. returnmethod.proceed();
  47. }
  48. }
  49. }
  1. packagecom.test;
  2. importorg.springframework.context.ApplicationContext;
  3. importorg.springframework.context.support.ClassPathXmlApplicationContext;
  4. publicclassAOPTest{
  5. /**
  6. *@paramargs
  7. */
  8. publicstaticvoidmain(String[]args){
  9. ApplicationContextcontext=
  10. newClassPathXmlApplicationContext("springAop.xml");
  11. BookBizbookBiz=(BookBiz)context.getBean("bookBiz");
  12. bookBiz.buy("張三","Spring深入潛出",50);
  13. bookBiz.comment("李四","《恐怖世界》一點都不恐怖,很好看!");
  14. bookBiz.comment("張三","《Spring深入潛出》還是寫得不錯的!");
  15. }
  16. }
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
  3. <beans>
  4. <beanid="bookBizTarget"class="com.test.BookBizImpl"/>
  5. <beanid="myAdvice"class="com.test.MyAdvice"/>
  6. <beanid="bookBiz"class="org.springframework.aop.framework.ProxyFactoryBean">
  7. <propertyname="proxyInterfaces">
  8. <value>com.test.BookBiz</value>
  9. </property>
  10. <propertyname="interceptorNames">
  11. <list>
  12. <value>myAdvice</value>
  13. </list>
  14. </property>
  15. <propertyname="target"ref="bookBizTarget"/>
  16. </bean>
  17. </beans>

運行AOPTest 這個類,下面是輸出結果:

[環繞通知]
前通知,調用的方法:buy,參數:[張三, Spring深入潛出, 50.0]
業務方法buy開始執行
·張三購買圖書:Spring深入潛出
·張三增加積分:5
·向物流系統下發貨單
業務方法buy結束
後通知,調用的方法:buy,參數:[張三, Spring深入潛出, 50.0]
[環繞通知]
屏蔽李四所有的評論
[環繞通知]
前通知,調用的方法:comment,參數:[張三, 《Spring深入潛出》還是寫得不錯的!]
業務方法comment開始執行
·張三發表書評《Spring深入潛出》還是寫得不錯的!
業務方法comment結束
後通知,調用的方法:comment,參數:[張三, 《Spring深入潛出》還是寫得不錯的!]

從輸出結果可以看出,環繞通知是最先開始執行的,,如果環繞通知把目標方法屏蔽了不執行,那麼後面的前後通知都不會執行

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章