javascript
Spring AOP用法
軟件152 楊浩藝
?
?
Spring AOP
Java web 環(huán)境搭建
Java web 項目搭建
Java Spring IOC用法
spring提供了兩個核心功能,一個是IoC(控制反轉(zhuǎn)),另外一個便是Aop(面向切面編程),IoC有助于應(yīng)用對象之間的解耦,AOP則可以實現(xiàn)橫切關(guān)注點(如日志、安全、緩存和事務(wù)管理)與他們所影響的對象之間的解耦。
1.簡介
AOP主要包含了通知、切點和連接點等術(shù)語,介紹如下
-
通知(Advice)
通知定義了切面是什么以及何時調(diào)用,何時調(diào)用包含以下幾種Before?在方法被調(diào)用之前調(diào)用通知
After?在方法完成之后調(diào)用通知,無論方法執(zhí)行是否成功
After-returning?在方法成功執(zhí)行之后調(diào)用通知
After-throwing?在方法拋出異常后調(diào)用通知
Around?通知包裹了被通知的方法,在被通知的方法調(diào)用之前和調(diào)用之后執(zhí)行自定義的行為 -
切點(PointCut)
通知定義了切面的什么和何時,切點定義了何處,切點的定義會匹配通知所要織入的一個或多個連接點,我們通常使用明確的類的方法名稱來指定這些切點,或是利用正則表達式定義匹配的類和方法名稱來指定這些切點。
切點的格式如下execution(* com.ganji.demo.service.user.UserService.GetDemoUser (..) )
-
連接點(JoinPoint)
連接點是在應(yīng)用執(zhí)行過程中能夠插入切面的一個點,這個點可以是調(diào)用方法時,拋出異常時,甚至是修改一個字段時,切面代碼可以利用這些連接點插入到應(yīng)用的正常流程中,并添加新的行為,如日志、安全、事務(wù)、緩存等。
現(xiàn)階段的AOP框架
AOP框架除了Spring AOP之外,還包括AspectJ、JBoss AOP;
上述框架的區(qū)別是Spring AOP只支持到方法連接點,另外兩個還支持字段和構(gòu)造器連接點。
2.用法
同依賴注入一樣,AOP在spring中有兩種配置方式,一是xml配置的方式,二是自動注解的模式。
-
2.1 xml中聲明切面
- 2.1.1 AOP配置元素
在xml中,我們使用如下AOP配置元素聲明切面
AOP配置元素 | 描述 ------------ | ------------- `<aop:advisor>` | 定義AOP通知器 `<aop:after>` | 定義AOP后置通知(不管該方法是否執(zhí)行成功) `<aop:after-returning>` | 在方法成功執(zhí)行后調(diào)用通知 `<aop:after-throwing>` | 在方法拋出異常后調(diào)用通知 `<aop:around>` | 定義AOP環(huán)繞通知 `<aop:aspect>` | 定義切面 `<aop:aspect-autoproxy>` | 定義`- 2.1.2 定義切面
我們在service層添加com.ganji.demo.service.aspect.XmlAopDemoUserLog類,里面實現(xiàn)了攔截方法,具體如下
package com.ganji.demo.service.aspect;import org.aspectj.lang.ProceedingJoinPoint; /** * Created by admin on 2015/9/2. */ public class XmlAopDemoUserLog { // 方法執(zhí)行前通知 public void beforeLog() { System.out.println("開始執(zhí)行前置通知 日志記錄"); } // 方法執(zhí)行完后通知 public void afterLog() { System.out.println("開始執(zhí)行后置通知 日志記錄"); } // 執(zhí)行成功后通知 public void afterReturningLog() { System.out.println("方法成功執(zhí)行后通知 日志記錄"); } // 拋出異常后通知 public void afterThrowingLog() { System.out.println("方法拋出異常后執(zhí)行通知 日志記錄"); } // 環(huán)繞通知 public Object aroundLog(ProceedingJoinPoint joinpoint) { Object result = null; try { System.out.println("環(huán)繞通知開始 日志記錄"); long start = System.currentTimeMillis(); //有返回參數(shù) 則需返回值 result = joinpoint.proceed(); long end = System.currentTimeMillis(); System.out.println("總共執(zhí)行時長" + (end - start) + " 毫秒"); System.out.println("環(huán)繞通知結(jié)束 日志記錄"); } catch (Throwable t) { System.out.println("出現(xiàn)錯誤"); } return result; } }- 2.1.3 xml聲明切面并調(diào)用
我們在web層,web-inf/dispatcher-servlet.xml中定義切面,具體如下
<!--定義切面 指定攔截方法時 做什么--> <bean id="xmlAopDemoUserLog" class="com.ganji.demo.service.aspect.XmlAopDemoUserLog"></bean> <aop:config> <aop:aspect ref="xmlAopDemoUserLog"> <!--指定切面--> <!--定義切點--> <aop:pointcut id="logpoint" expression="execution(* com.ganji.demo.service.user.UserService.GetDemoUser(..))"></aop:pointcut> <!--定義連接點--> <aop:before pointcut-ref="logpoint" method="beforeLog"></aop:before> <aop:after pointcut-ref="logpoint" method="afterLog"></aop:after> <aop:after-returning pointcut-ref="logpoint" method="afterReturningLog"></aop:after-returning> <aop:after-throwing pointcut-ref="logpoint" method="afterThrowingLog"></aop:after-throwing> </aop:aspect> </aop:config>在controller下調(diào)用,調(diào)用具體如下
DemoUserEntity demoUser=userService.GetDemoUser(1);
這是運行起來 我們將看到打印出如下日志
開始執(zhí)行前置通知 日志記錄
開始執(zhí)行后置通知 日志記錄
方法成功執(zhí)行后通知 日志記錄- 2.1.4 小結(jié)
如果通過xml配置,我們還可以實現(xiàn)環(huán)繞通知,環(huán)繞通知的目的是把前置通知和后置通知的信息共享起來。同時還可以為通知傳遞方法的參數(shù),在切面攔截中驗證參數(shù)的有效性。
-
2.2 自動注解AOP
在上述2.1中我們通過xml配置的形式 實現(xiàn)了AOP編程,現(xiàn)在我們通過不配置xml,配置注解的形式實現(xiàn)AOP。
- 2.2.1 配置自動代理
使用配置注解,首先我們要將切面在spring上下文中聲明成自動代理bean,我們需要在web層的web-inf/dispatcher-servlet.xml文件中配置如下一句話即可
<aop:aspectj-autoproxy />
當(dāng)然我們需要在xml的根目錄beans下引用aop的命名空間和xsi
xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd"- 2.2.2 使用@Aspect注解
聲明一個切面,只需要在類名上添加@Aspect屬性即可,具體的連接點,我們用@Pointcut和@Before、@After等標(biāo)注。具體如下
<dependency><groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.11</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.11</version> </dependency>
在聲明前 我們需要依賴配置pom聲明切面類,包含了注解@Aspect以及何時(如@Before)執(zhí)行通知
package com.ganji.demo.service.aspect;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Service; /** * Created by admin on 2015/9/2. */- 2.2.3 總結(jié)
按照上述兩個步驟,使用注解實現(xiàn)Aop即可,這里依賴了IOC。
轉(zhuǎn)載于:https://www.cnblogs.com/yhy-yhy/p/7093944.html
總結(jié)
以上是生活随笔為你收集整理的Spring AOP用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于函数,对象以及闭包的一些理解
- 下一篇: Software Engineering