Spring-11-AOP面向切面程式設計
阿新 • • 發佈:2020-08-27
11.AOP
11.1 什麼是AOP?
AOP,意為面向切面程式設計,通過預編譯和執行時動態代理實現程式功能的統一維護的一種技術。
11.2 AOP在Spring中的作用
提供宣告式事務:允許使用者自定義切面
-
橫切關注點:跨越應用程式多個模組的方法或功能;
-
切面:橫切關注點 被模組化 的特殊物件,即一個類;
-
通知:切面必須要完成的工作:即類中的一個方法;
-
目標:被通知的物件;
-
代理:向目標物件應用通知後建立的物件;
-
切入點:切面通知執行的地點的定義;
-
連線點:與切入點匹配的執行點;
SpringAOP中,通過Advice定義橫切邏輯,Spring支援5種類型的Advice:
- 前置通知
- 後置通知
- 環繞通知
- 異常丟擲通知
- 引介通知
11.3 使用Spring實現AOP
【重點】使用AOP織入,需要匯入一個依賴包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
方式一:使用Spring的API介面【主要是Spring介面實現】
業務介面:
public interface UserService { public void add(); public void delete(); public void update(); public void select(); }
實現類:(類似於host)
public class UserServiceImpl implements UserService{ public void add() { System.out.println("增加一個使用者"); } public void delete() { System.out.println("刪除一個使用者"); } public void update() { System.out.println("更新一個使用者"); } public void select() { System.out.println("查詢一個使用者"); } }
增強方法:
public class Log implements MethodBeforeAdvice {
//method:要執行的目標物件的方法
//args:引數
//target:目標物件
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被執行了");
}
}
public class AfterLog implements AfterReturningAdvice {
//o=returnValue:返回值
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("執行了"+method.getName()+"方法,返回結果為"+o);
}
}
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/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--註冊bean-->
<bean id="userService" class="com.kuang.service.UserServiceImpl"/>
<bean id="log" class="com.kuang.log.Log"/>
<bean id="afterLog" class="com.kuang.log.AfterLog"/>
<!--方式一:使用原生Spring API介面-->
<!--配置aop:需要匯入aop約束-->
<aop:config>
<!--切入點:pointcut 表示式:expression,execution()-->
<aop:pointcut id="pointcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
<!--執行環繞增強-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
方式二:自定義來實現AOP【主要是切面定義】
自定義類(切面):
public class DiyPointCut {
public void before(){
System.out.println("=====方法執行前=====");
}
public void after(){
System.out.println("=====方法執行後=====");
}
}
applicationContext.xml配置:
<!--方式二:自定義類-->
<bean id="diy" class="com.kuang.diy.DiyPointCut"/>
<aop:config>
<!--自定義切面,ref表示要引用的類-->
<aop:aspect ref="diy">
<!--切入點-->
<aop:pointcut id="point" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
方式三:使用註解實現!
切面:
//使用註解實現AOP
@Aspect //標註這個類是一個切面
public class AnnotationPointCut {
@Before("execution(* com.kuang.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("=====方法執行前=====");
}
@After("execution(* com.kuang.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("=====方法執行後=====");
}
//環繞增強中,我們可以給定一個引數,代表我們要獲取處理切入的點
@Around("execution(* com.kuang.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("環繞前");
System.out.println(proceedingJoinPoint.getSignature());//獲得簽名
//執行方法
Object proceed = proceedingJoinPoint.proceed();
System.out.println("環繞後");
}
}
applicationContext.xml的配置:
<!--方式三:使用註解實現aop jdk(預設) cglib-->
<bean id="annotationPointCut" class="com.kuang.diy.AnnotationPointCut"/>
<!--開啟註解支援-->
<aop:aspectj-autoproxy />