生活随笔
收集整理的這篇文章主要介紹了
Spring学习笔记(三) AOP_annotation,AOP_XML
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在學習課程以前,聽說AOP有種很神秘的感覺,好像很好深的技術。其實原理很簡單,使用動態代理的方式給程序增加邏輯。與此相似的有struts2中的filter攔截器。
再講AOP之前先把需求說一下: 同Spring學習筆記(一)IoC中的需求一樣,只不過要在save()方法前后增加日志log的記錄。
不使用AOP的方式可以有兩種實現思路:
1—>使用繼承的的方式,把有save方法的類繼承,然后在增加log邏輯
2—>使用組合的方式,即增加一個記錄邏輯的類,分別有在save()方法前save()方法后記錄日志的功能,然后把這兩個類組合到一起
雖然上面兩種方式都能解決我們的需求,但是一個問題來了:如果有500個更甚至1000各類中的方法需要增加邏輯,使用繼承的方法和組合的方法的弊端就出來了。一方面會增加海量的代碼量。另一方面,會使整個程序的可讀性變差。程序員在寫程序的時候不能專注于業務邏輯,還要專注于維護邏輯,增加程序員的邏輯壓力,從而用于調試的時間會增多。
從而,出現了AOP,來解決這類問題。Spring中的AOP實現原理使用的是JavaJDK中的動態代理。
AOP_annotation
AOP_annotation環境搭建
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" ???????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ???????xmlns:aop="http://www.springframework.org/schema/aop" ????????xsi:schemaLocation="http://www.springframework.org/schema/beans ???????????http://www.springframework.org/schema/aop ???????????http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> ????<aop:aspectj-autoproxy /> </beans> 增加AOP的xml命名空間以及xsd路徑設置。最重要的是增加<aop:aspect-autoproxy>
Aspectj是專門用來實現動態代理的類庫,spring中的AOP使用的是aspectj.jar實現的。需要在庫中增加aspectj的類庫。
@Aspect,@Before
@Aspect:即標注這個類為切面類,是增加的邏輯的那個類。放在我們的需求里面,log記錄方法應寫在被@Aspect標注的類
@Before:切入點的邏輯,即是在增加save()方法前面執行
Execution…:切入點語法
@Aspect @Component public class LogInterceptor { ??@Before("execution(* com.xxxx.dao.impl.UserDAOImpl.save(com.xxxx.model.User))") ???public void before() { ??????System.out.println("method before"); ???} } 其中@Before口號中的execution()是注冊那個保重的那個類中的那個方法加入切入邏輯。
@PointCut
@Aspect @Component public class LogInterceptor { ???@Pointcut("execution(public * com.xxxx.service..*.add(..))") ???public void myMethod(){}; } @PointCut:切入點集合。即對com.xxx.service包中的類中任何返回值的add()方法(add()方法的參數任意)增加切入邏輯????
這種給相關方法加入切入邏輯的動作又叫作織入(Weave),切入點語法為execution的為aspectj的織入語法。Spring AOP也有自己的語法,到時候查閱參考文檔吧!
@AfterReturning,@AfterThrowing,@After(finally)
這些方法的使用跟@Before類似
@Around
@Aspect @Component public class LogInterceptor { ???@Pointcut("execution(public * com.bjsxt.service..*.add(..))") ???public void myMethod(){}; ???@Around("myMethod()") ???public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable { ??????System.out.println("method around start"); ??????pjp.proceed(); ??????System.out.println("method around end"); ???} } 其中有兩個知識點
????1à這一點在剛才說@PointCut是沒有說明,就是唄@PointCut標記的方法名字myMethod為@PointCut所標記的那些切入點方法集合的表示符號。舉個例子,
int myMethod=@PointCut
????2à@around要使用pjp.proceed()執行切入點save()方法
AOP_XML
<bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor" ></bean> <aop:config> ????<aop:aspect id="logAspect" ref="logInterceptor" > ????????<aop:pointcut id="myMethod" expression="execution(public void com.bjsxt.dao.impl.UserDAOImpl.save(com.bjsxt.model.User))"></aop:pointcut> ????????<aop:before method="before" pointcut-ref="myMethod"></aop:before> ? ?<aop:after-returning method="myafter" pointcut="execution(public void com.bjsxt.dao.impl.UserDAOImpl.save(com.bjsxt.model.User))"> ????</aop:aspect> </aop:config> 上述代碼寫了AOP中XML方式書寫的格式,其中pointcut的三種寫法都在上面4,5,6行中。
轉載于:https://www.cnblogs.com/wenlonghor/p/3295863.html
總結
以上是生活随笔為你收集整理的Spring学习笔记(三) AOP_annotation,AOP_XML的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。