1.AOP概念
AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,通過預編譯方式和運行期間動態代理實現程序功能的統一維護的一種技術。
AOP是OOP的延續,是軟件開發中的一個熱點,也是Spring框架中的一個重要內容,是函數式編程的一種衍生范型。利用AOP可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發的效率。
三種實現方式
- 使用原生spring API接口
beforelog.java
public class Log implements MethodBeforeAdvice {
//method:要執行的目標對象的方法
//objects:參數(args)
//object: 目標對象
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println (target.getClass ().getName ()+"的"+method.getName ()+"被執行了");
}
}
afterlog.java
public class AfterLog implements AfterReturningAdvice {
//returnValue執行后返回值
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println ("被執行了"+target.getClass ().getName ()+"的"+method.getName ()+"結果為"+returnValue);
}
}
spring配置文件
<bean id="userservice" class="com.ji.service.UserServiceImpl"/>
<bean id="log" class="com.ji.log.Log"/>
<bean id="afterlog" class="com.ji.log.AfterLog"/>
使用原生的api接口
<aop:config>
切入點:在哪個地方執行 execution(要執行的位置)
<aop:pointcut id="pointcut" expression="execution(* com.ji.service.UserServiceImpl.*(..))"/>
執行環繞增強
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>
</aop:config>
- 自定義類來說實現aop(主要是切面定義)
自定義實現類
public class DiyPointCut {
public void before(){
System.out.println ("方法執行前");
}
public void after(){
System.out.println ("方法執行后");
}
}
spring配置文件
<bean id="diy" class="com.ji.diy.DiyPointCut"/>
<aop:config>
<aop:aspect ref="diy"> 自定義切面要引入的類;
切入點
<aop:pointcut id="pointCut" expression="execution(* com.ji.service.UserServiceImpl.*(..))"/>
通知
<aop:before method="before" pointcut-ref="pointCut"/>
<aop:after method="after" pointcut-ref="pointCut"/>
</aop:aspect>
</aop:config>
- 使用注解實現
注解實現類
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
//使用注解方式實現aop
@Aspect //標注這個類是一個切面
public class AnnotationPointCut {
@Before ("execution(* com.ji.service.UserServiceImpl.*(..))")
public void before(){
System.out.println ("方法執行前");
}
@After ("execution(* com.ji.service.UserServiceImpl.*(..))")
public void after(){
System.out.println ("方法執行后");
}
//在環繞增強中可以給定一個參數代表我們要獲取處理切入的點
@Around ("execution(* com.ji.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp){
System.out.println ("環繞前");
//執行方法
try {
System.out.println (jp.proceed ());
System.out.println (jp.getSignature ());
} catch (Throwable throwable) {
throwable.printStackTrace ();
}
System.out.println ("環繞后");
}
}
spring配置文件
<!--開啟注解支持-->
<aop:aspectj-autoproxy/>
<bean id="userservice" class="com.ji.service.UserServiceImpl"/>
<bean id="annotation" class="com.ji.diy.AnnotationPointCut"/>
總結
注解實現編寫的時候最方便,但后期維護可能會造成困擾,第一種方法需要再寫兩個類,總體個人覺得第二種方法比較好。