一:你應該明白的知識
1.對於AOP這種編程思想,很多框架都進行了實現。Spring就是其中之一,可以完成面向切面編程。然而,AspectJ也實現了AOP的功能,且實現方式更為簡捷,使用更加方便,而且還支持注解式開發。所以,Spring又將AspectJ對於AOP的實現也引入到了自己的框架中。在Spring中使用AOP開發時,一般使用AspectJ的實現方式。
2.Spring的經典AOP配置方案
01.使用的是Aspectj第三方框架,實現了AOP思想
02.注解配置的AOP
03.純POJO <aop:config>
3..切入點表達式
execution(【modifiers-pattern?】 訪問修飾符
ret-type-pattern 返回值類型
【declaring-type-pattern?】 全限定性類名
name-pattern(param-pattern) 方法名(參數名)
【throws-pattern?】) 拋出異常類型
切入點表達式要匹配的對象就是目標方法的方法名。所以,execution表達式中明顯就是方法的簽名。注意:表達式中加[]的部分表示可省略部分,各部分間用空格分開。在其中可以使用以下符號:
符號 意義
* 0至多個任意字符
.. 用在方法參數中,表示任意多個參數
用在包名后,表示當前包及其子包路徑
+ 用在類名后,表示當前類及其子類
用在接口后,表示當前接口及其實現類
案例:
execution(public * *(..)) 指定切入點為:任意公共方法
execution(* set*(..)) 指定切入點為:任何一個以"set"開始的方法
二.使用的是Aspectj第三方框架注解配置的AOP增強
源碼介紹:
1.ISomeService.java

package entity; //業務接口 public interface ISomeService { //1.1 執行事務 public void doTransaction(); //1.2 書寫日志 public String doLog(); }
2.SomeServiceImpl.java

package entity; //接口的實現類 public class SomeServiceImpl implements ISomeService { //實現接口中的方法 @Override public void doTransaction() { System.out.println("----開啟事務----"); } @Override public String doLog() { //int i=5/0;//制造一個錯誤,用於測試異常增強 System.out.println("---書寫日志-----"); return "abc"; } }
3.MyAspect.java(使用的是Aspectj第三方框架注解配置的AOP增強)

package aop; //aspectj注解實現aop增強 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; //該類為切面 @Aspect public class MyAspect { // 前置通知 @Before(value = "execution(public * *..ISomeService.doLog(..))") public void myBefore() { System.out.println("這是前置增強"); } // 后置通知 @AfterReturning(value = "execution(public * *..ISomeService.doLog(..))") public void myAfterReturning() { System.out.println("這是后置增強"); } // 環繞增強 @Around(value = "execution(public * *..ISomeService.doLog(..))") public void myAround(ProceedingJoinPoint pjp) throws Throwable { System.out.println("這是環繞前置增強"); pjp.proceed(); System.out.println("這是環繞后置增強"); } // 異常增強 @AfterThrowing(value = "execution(public * *..ISomeService.doLog(..))") public void myAfterThrowing() { System.out.println("這是異常增強"); } // 最終增強 @After(value = "execution(public * *..ISomeService.doLog(..))") public void myAfter() { System.out.println("這是最終增強"); } }
4.applicationContext.xml(Spring的配置文件)

<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 目標對象 --> <bean id="someService" class="entity.SomeServiceImpl"></bean> <!-- 切面: --> <bean id="myAspect" class="aop.MyAspect"></bean> <!-- 自動代理 --> <aop:aspectj-autoproxy/> </beans>
5.MyTest.java

package test; //測試類 import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import entity.ISomeService; public class MyTest { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); ISomeService biz=(ISomeService)ctx.getBean("someService"); biz.doLog(); biz.doTransaction(); System.out.println("success!"); } }
6.log4j.properties(日志配置文件)

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=info, stdout
7.當然,同志們別忘了引入jar包啊!
8.其測試結果展示:
三:使用的是Aspectj第三方框架 純POJO 基於Schema配置 (<aop:config>)
源碼介紹:
1.MyAspect.java

package aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; public class MyAspect { // 前置通知 public void myBefore() { System.out.println("這是前置增強"); } public void before(JoinPoint jp) { System.out.println("前置通知方法before() jp = " + jp); } // 后置通知 public void myAfterReturning() { System.out.println("這是后置增強"); } // 帶參數 public void afterReturing(String result) { System.out.println("后置通知方法 result = " + result); } // 環繞通知 public Object around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("環繞通知方法,目標方法執行之前"); // 執行目標方法 Object result = pjp.proceed(); System.out.println("環繞通知方法,目標方法執行之后"); return ((String) result).toUpperCase(); } // 異常通知 public void afterThrowing() { System.out.println("異常通知方法"); } public void afterThrowing(Exception ex) { System.out.println("異常通知方法 ex = " + ex.getMessage()); } // 最終通知 public void after() { System.out.println("最終通知方法"); } }
2.applicationContext.xml(Spring的配置文件)

<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 目標對象 --> <bean id="someService" class="entity.SomeServiceImpl"></bean> <!-- 切面: --> <bean id="myAspect" class="aop.MyAspect"></bean> <aop:config> <!--expression:切入點表達式 --> <aop:pointcut expression="execution(public * *..ISomeService.doLog(..))" id="beforePointcut" /> <aop:aspect ref="myAspect"><!-- ref:指定切面 --> <!-- method:指定切面類中的方法; pointcut-ref:指定定義的切點 --> <!-- 前置增強 --> <aop:before method="myBefore" pointcut-ref="beforePointcut" /> <!-- 前置增強 帶參 --> <aop:before method="before(org.aspectj.lang.JoinPoint)" pointcut-ref="beforePointcut" /> <!-- 后置增強 --> <aop:after-returning method="myAfterReturning" pointcut-ref="beforePointcut" /> <!-- 后置增強 帶參 --> <aop:after-returning method="afterReturing(java.lang.String)" pointcut-ref="beforePointcut" returning="result" /> <!-- 環繞增強 --> <aop:around method="around" pointcut-ref="beforePointcut" /> <!-- 異常增強 --> <aop:after-throwing method="afterThrowing" pointcut-ref="beforePointcut" /> <!-- 異常增強 帶參 --> <aop:after-throwing method="afterThrowing(java.lang.Exception)" pointcut-ref="beforePointcut" throwing="ex" /> <!-- 最終增強 --> <aop:after method="after" pointcut-ref="beforePointcut" /> </aop:aspect> </aop:config> </beans>
其中,其他的類和文件和上面的例子中完全相同。這里則不做介紹。
3.其測試結果展示: