學而時習之,不亦說乎!html
--《論語》java
看這一篇以前最好先看前面關於AOP的兩篇。web
http://www.cnblogs.com/zby9527/p/6945756.html (JDK代理和CGLIB代理)spring
http://www.cnblogs.com/zby9527/p/6946952.html (Spring的AOP)express
AspectJ:apache
1.AspectJ是一個基於Java語言的AOP框架。app
2.Spring2.0之後新增了對AspectJ切點表達式支持。框架
3.@AspectJ是AspectJ1.5新增功能,經過JDK5註解技術,容許直接在Bean類中定義切面新版本Spring框架,建議使用AspectJ方式來開發maven
AspectJ最強大的地方在於他的切入點表達式:測試
語法:execution(修飾符 返回值 包.類.方法名(參數) throws異常)
修飾符,通常省略
public 公共方法
* 任意
返回值,不能省略
void 返回沒有值
String 返回值字符串
* 任意
包
com.zby.service 固定包
com.zby.oa.*.service oa包下面子包 (例如:com.zby.oa.flow.service)
com.zby.oa.. oa包下面的全部子包(含本身)
com.zby.oa.*.service.. oa包下面任意子包,固定目錄service,service目錄任意包
類
UserServiceImpl 指定類
*Impl 以Impl結尾
User* 以User開頭
* 任意
方法名,不能省略
addUser 固定方法
add* 以add開頭
*Do 以Do結尾
* 任意
(參數)
() 無參
(int) 一個整型
(int ,int) 兩個
(..) 參數任意
throws ,可省略,通常不寫。
固然,execution也是能夠變得,可是通常用這個就夠了,更詳細的表達式用法,固然是查看專業文檔。
AspectJ和aopalliance通知的區別:
AOP聯盟的通知類型具備特性接口,必須實現,從而肯定方法名稱,而AspectJ的通知類型只定義了類型名稱和方法格式,這意味着,咱們的切面不須要實現任何方法!!!。
AspectJ通知:
before:前置通知(應用:各類校驗)
在方法執行前執行,若是通知拋出異常,阻止方法運行
afterReturning:後置通知(應用:常規數據處理)
方法正常返回後執行,若是方法中拋出異常,通知沒法執行,必須在方法執行後才執行,因此能夠得到方法的返回值。
around:環繞通知(應用:十分強大,能夠作任何事情)
方法執行先後分別執行,能夠阻止方法的執行,必須手動執行目標方法
afterThrowing:拋出異常通知(應用:包裝異常信息)
方法拋出異常後執行,若是方法沒有拋出異常,沒法執行
after:最終通知(應用:清理現場)
方法執行完畢後執行,不管方法中是否出現異常
固然,最重要也最經常使用的仍是環繞通知,由於環繞通知必須手動執行目標方法,因此,能夠代替其餘幾個通知。
使用XML配置Spring整合AspectJ的AOP:
1)項目總體結構以下:
2)建立maven項目,pom.xml以下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zby</groupId> <artifactId>aop</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.8.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.8.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.8.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.8.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.10</version> </dependency> </dependencies> </project>
3)建立目標類UserService:
package com.zby.service; public class UserService { public void saveUser(String username, String password) { System.out.println("save user[username=" + username + ",password=" + password + "]"); } }
4)建立切面類:
package com.zby.interceptor; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; public class MyAspect { public void myBefore(JoinPoint joinPoint) { System.out.println("前置通知 : " + joinPoint.getSignature().getName()); } public void myAfterReturning(JoinPoint joinPoint, Object ret) { System.out.println("後置通知 : " + joinPoint.getSignature().getName() + " , -->" + ret); } public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("環繞通知執行方法前"); // 手動執行目標方法 Object obj = joinPoint.proceed(); System.out.println("環繞通知執行方法後"); return obj; } public void myAfterThrowing(JoinPoint joinPoint, Throwable e) { System.out.println("拋出異常通知 : " + e.getMessage()); } public void myAfter(JoinPoint joinPoint) { System.out.println("最終通知"); } }
切面類沒有實現接口,可是有幾種方法參數,這些不是必須的。這些傳入的對象是什麼?固然是咱們在切面點須要的信息!用腦袋想,在給一個方法進行加強的時候,前置方法,或者後置方法,或者環繞方法,有可能須要獲得原方法的哪些信息,這裏面都有。
5)編寫配置文件applicationContext.xml:
<?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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 目標類 --> <bean id="userService" class="com.zby.service.UserService"></bean> <!-- 切面類 --> <bean id="myInterceptor" class="com.zby.interceptor.MyAspect"></bean> <aop:config> <aop:aspect ref="myInterceptor"> <aop:pointcut expression="execution(* com.zby.service.UserService.*(..))" id="myPointcut" /> <!--環繞通知 <aop:around method="" pointcut-ref=""/> 通知方法格式:public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable{ } 返回值類型:Object 方法名:任意 參數:org.aspectj.lang.ProceedingJoinPoint 拋出異常 執行目標方法:Object obj = joinPoint.proceed(); 例如: <aop:around method="myAround" pointcut-ref="myPointCut"/> --> <aop:around method="myAround" pointcut-ref="myPointcut" /> <!-- 最終通知 --> <aop:after method="myAfter" pointcut-ref="myPointcut" /> <!--後置通知 ,目標方法後執行,得到返回值 <aop:after-returning method="" pointcut-ref="" returning=""/> returning 通知方法第二個參數的名稱 通知方法格式: public void myAfterReturning(JoinPoint joinPoint,Object ret){} 參數1:鏈接點描述 參數2:類型Object,參數名 returning="ret" 配置的 例如: <aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut" returning="ret" /> --> <aop:after-returning method="myAfterReturning" pointcut-ref="myPointcut" returning="ret" /> <!--拋出異常 <aop:after-throwing method="" pointcut-ref="" throwing=""/> throwing :通知方法的第二個參數名稱 通知方法格式:public void myAfterThrowing(JoinPoint joinPoint,Throwable e){ } 參數1:鏈接點描述對象 參數2:得到異常信息,類型Throwable ,參數名由throwing="e" 配置 例如: <aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointCut" throwing="e"/> --> <aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointcut" throwing="e" /> <!--前置通知 <aop:before method="" pointcut="" pointcut-ref=""/> method : 通知,及方法名 pointcut :切入點表達式,此表達式只能當前通知使用。 pointcut-ref : 切入點引用,能夠與其餘通知共享切入點。 通知方法格式:public void myBefore(JoinPoint joinPoint){} 參數1:org.aspectj.lang.JoinPoint 用於描述鏈接點(目標方法),得到目標方法名等 例如: <aop:before method="myBefore" pointcut-ref="myPointCut"/> --> <aop:before method="myBefore" pointcut-ref="myPointcut" /> </aop:aspect> </aop:config> </beans>
6)編寫測試類:
package com.zby.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.zby.service.UserService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:applicationContext.xml" }) public class AOPTest { @Autowired private UserService userService; @Test public void testProxy() { System.out.println("After Proxy......"); userService.saveUser("zby", "1234567890"); } }
7)控制檯打印結果:
六月 09, 2017 2:07:56 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper getDefaultTestExecutionListenerClassNames 信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] 六月 09, 2017 2:07:56 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper instantiateListeners 信息: Could not instantiate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute] 六月 09, 2017 2:07:56 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper instantiateListeners 信息: Could not instantiate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource] 六月 09, 2017 2:07:56 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper instantiateListeners 信息: Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext] 六月 09, 2017 2:07:56 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners 信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@6e983d8d, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@4cf12cb4, org.springframework.test.context.support.DirtiesContextTestExecutionListener@6dae04e2] 六月 09, 2017 2:07:56 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] 六月 09, 2017 2:07:56 下午 org.springframework.context.support.GenericApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.GenericApplicationContext@24024ad8: startup date [Fri Jun 09 14:07:56 CST 2017]; root of context hierarchy After Proxy...... 環繞通知執行方法前 前置通知 : saveUser save user[username=zby,password=1234567890] 後置通知 : saveUser , -->null 最終通知 環繞通知執行方法後
這個DEMO就是一個大雜燴,其實使用時使用一個環繞通知就夠了。再環繞通知裏面必須手動執行方法,所以咱們用try-catch把方法執行包裹起來,而後在執行前和執行後寫加強代碼便可。
使用註解配置Spring整合AspectJ的AOP:
1)上面的一二步驟不變。
2)編寫目標類UserService:
package com.zby.service; import org.springframework.stereotype.Service; @Service public class UserService { public void saveUser(String username, String password) { System.out.println("save user[username=" + username + ",password=" + password + "]"); } }
3)編寫切面類,使用註解:
package com.zby.interceptor; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Component @Aspect public class MyAspect { // 多個方法須要使用這個切入點表達式,定義爲一個公用的 @Pointcut("execution(* com.zby.service..*(..))") public void myPointCut() { } // 這裏註解裏面的值爲上面的方法名 @Before("myPointCut()") public void myBefore(JoinPoint joinPoint) { System.out.println("前置通知 : " + joinPoint.getSignature().getName()); } // 當你只有一個方法,或者只在這兒用,能夠直接寫切入點表達式 @AfterReturning(value = "execution(* com.zby.service..*(..))", returning = "ret") public void myAfterReturning(JoinPoint joinPoint, Object ret) { System.out.println("後置通知 : " + joinPoint.getSignature().getName() + " , -->" + ret); } // @Around("myPointCut()") public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("環繞通知執行方法前"); // 手動執行目標方法 Object obj = joinPoint.proceed(); System.out.println("環繞通知執行方法後"); return obj; } @AfterThrowing(value = "myPointCut()", throwing = "e") public void myAfterThrowing(JoinPoint joinPoint, Throwable e) { System.out.println("拋出異常通知 : " + e.getMessage()); } @After("myPointCut()") public void myAfter(JoinPoint joinPoint) { System.out.println("最終通知"); } }
4)編寫配置文件applicationContext.xml:
<?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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <context:component-scan base-package="com.zby"></context:component-scan> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
5)編寫測試類:
package com.zby.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.zby.service.UserService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:applicationContext.xml" }) public class AOPTest { @Autowired private UserService userService; @Test public void testProxy() { System.out.println("After Proxy......"); userService.saveUser("zby", "1234567890"); } }
6)控制檯打印結果:
六月 09, 2017 2:29:21 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper getDefaultTestExecutionListenerClassNames 信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] 六月 09, 2017 2:29:21 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper instantiateListeners 信息: Could not instantiate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute] 六月 09, 2017 2:29:21 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper instantiateListeners 信息: Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext] 六月 09, 2017 2:29:21 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper instantiateListeners 信息: Could not instantiate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource] 六月 09, 2017 2:29:21 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners 信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@6dae04e2, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@3bc2c9af, org.springframework.test.context.support.DirtiesContextTestExecutionListener@71471ecf] 六月 09, 2017 2:29:21 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] 六月 09, 2017 2:29:21 下午 org.springframework.context.support.GenericApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.GenericApplicationContext@69f31d: startup date [Fri Jun 09 14:29:21 CST 2017]; root of context hierarchy After Proxy...... 環繞通知執行方法前 前置通知 : saveUser save user[username=zby,password=1234567890] 環繞通知執行方法後 最終通知 後置通知 : saveUser , -->null
總結:對比起來,能夠看出來使用最後一種方式開發AOP很方便,這也是咱們最經常使用的,至於spring原生的AOP,大多在一些框架裏面看到。使用整合AspectJ的方式,最主要的是要注意切面表達式的書寫和方法參數傳入,以及怎麼使用這些參數。