亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

一個簡單的spring AOP例子

系統 2135 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條評論
主站蜘蛛池模板: 成人午夜网站 | 国产精品久久久久久久午夜片 | 久青草国产在线 | 国产成 人 综合 亚洲网 | 国产日韩精品欧美在线ccc | 亚洲精品乱码久久久久蜜桃 | 高清国产精品入口麻豆 | 亚洲春色综合另类网蜜桃 | 91精品免费不卡在线观看 | 99久久精品无码一区二区毛片 | 欧美色欧美亚洲高清在线观看 | 日本精品一区二区三本中文 | 精品一本久久中文字幕 | 毛片一区 | 91视频亚洲 | 中文一区二区视频 | 在线播放ww| 狠狠做深爱婷婷久久一区 | 国产一级特黄生活片 | aaa影院 | 免费观看日本污污ww网站精选 | 欧美不卡在线视频 | 国产精品亚洲一区在线播放 | hdxxx色视频 heyzo在线播放4k岛国 | 国产a做爰全过程片 | 一级特黄aaa大片免色 | 日本不卡二 | 3www黄| 精品久久久久久久久久香蕉 | 欧美日韩亚洲第一页 | 国产看色免费 | 国产一区二区三区国产精品 | 七月婷婷精品视频在线观看 | 66av99精品福利视频在线 | 男人的天堂a在线 | 四虎国产精品视频免费看 | 成人精品一区二区www | 中文字幕丝袜在线56页 | 一区二区三区免费在线 | 四虎1515| 99国产精品高清一区二区二区 |