AOP的三種實現方式
什么是AOP
AOP(Aspect Oriented Programming),即面向切面編程,可以說是OOP(Object Oriented Programming,面向對象編程)的補充和完善。OOP引入封裝、繼承、多態等概念來建立一種對象層次結構,用於模擬公共行為的一個集合。不過OOP允許開發者定義縱向的關系,但並不適合定義橫向的關系,例如日志功能。日志代碼往往橫向地散布在所有對象層次中,而與它對應的對象的核心功能毫無關系對於其他類型的代碼,如安全性、異常處理和透明的持續性也都是如此,這種散布在各處的無關的代碼被稱為橫切(cross cutting),在OOP設計中,它導致了大量代碼的重復,而不利於各個模塊的重用。
AOP技術恰恰相反,它利用一種稱為"橫切"的技術,剖解開封裝的對象內部,並將那些影響了多個類的公共行為封裝到一個可重用模塊,並將其命名為"Aspect",即切面。所謂"切面",簡單說就是那些與業務無關,卻為業務模塊所共同調用的邏輯或責任封裝起來,便於減少系統的重復代碼,降低模塊之間的耦合度,並有利於未來的可操作性和可維護性。
使用"橫切"技術,AOP把軟件系統分為兩個部分:核心關注點和橫切關注點。業務處理的主要流程是核心關注點,與之關系不大的部分是橫切關注點。橫切關注點的一個特點是,他們經常發生在核心關注點的多處,而各處基本相似,比如權限認證、日志、事物。AOP的作用在於分離系統中的各種關注點,將核心關注點和橫切關注點分離開來。
AOP核心概念
1、橫切關注點
對哪些方法進行攔截,攔截后怎么處理,這些關注點稱之為橫切關注點
2、切面(aspect)
類是對物體特征的抽象,切面就是對橫切關注點的抽象
3、連接點(joinpoint)
被攔截到的點,因為Spring只支持方法類型的連接點,所以在Spring中連接點指的就是被攔截到的方法,實際上連接點還可以是字段或者構造器
4、切入點(pointcut)
對連接點進行攔截的定義
5、通知(advice)
所謂通知指的就是指攔截到連接點之后要執行的代碼,通知分為前置、后置、異常、最終、環繞通知五類
6、目標對象
代理的目標對象
7、織入(weave)
將切面應用到目標對象並導致代理對象創建的過程
8、引入(introduction)
在不修改代碼的前提下,引入可以在運行期為類動態地添加一些方法或字段
實現AOP
- 首先創建UserService接口實現增刪改查
package com.wuxin.service;
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 query() {
System.out.println("查詢一個用戶");
}
}
- 創建UserServiceImpl類實現UserService接口
package com.wuxin.service;
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 query() {
System.out.println("查詢一個用戶");
}
}
一、原生API實現AOP
- 創建一個BeforeLog類作為前置方法
package com.wuxin.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class BeforeLog implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("執行了"+method.getClass().getName()+"方法");
}
}
- 創建一個AfterLog作為后置方法
package com.wuxin.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println(method.getClass().getName()+"方法,執行完畢" + method.getName() +"返回值為:"+ o);
}
}
- 定義xml配置文件,實現前置和后置的織入
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--1.原生API-->
<bean id="after" class="com.wuxin.log.AfterLog"/>
<bean id="before" class="com.wuxin.log.BeforeLog"/>
<bean id="userServiceImpl" class="com.wuxin.service.UserServiceImpl"/>
<aop:config>
<aop:pointcut id="point" expression="execution(* com.wuxin.service.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="after" pointcut-ref="point"/>
<aop:advisor advice-ref="before" pointcut-ref="point"/>
</aop:config>
</beans>
- 創建測試類,測試是否織入成功
import com.wuxin.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userServiceImpl = (UserService) context.getBean("userServiceImpl");
userServiceImpl.add();
}
}
- 測試結果
二、自定義方法實現AOP
- 創建DivLog方法
package com.wuxin.log;
public class DivLog {
public void BeforeLog(){
System.out.println("前置通知");
}
public void AfterLog(){
System.out.println("后置通知");
}
}
- 創建XML解析
<!--2.自定義方法實現-->
<bean id="divLog" class="com.wuxin.log.DivLog"/>
<aop:config>
<aop:aspect ref="divLog">
<aop:pointcut id="point" expression="execution(* com.wuxin.service.UserServiceImpl.*(..))"/>
<aop:after method="AfterLog" pointcut-ref="point"/>
<aop:before method="BeforeLog" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
- 測試結果
三、注解實現AOP
- 創建AnnLog並注解
package com.wuxin.log;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AnnLog {
@After("execution(* com.wuxin.service.UserServiceImpl.*(..))")
public void BeforeLog(){
System.out.println("前置通知");
}
@Before("execution(* com.wuxin.service.UserServiceImpl.*(..))")
public void AfterLog(){
System.out.println("后置通知");
}
}
- 開啟注解
<!--3.注解實現AOP-->
<bean id="annLog" class="com.wuxin.log.AnnLog"/>
<aop:aspectj-autoproxy proxy-target-class="true"/>
- 測試類
import com.wuxin.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userServiceImpl = (UserService) context.getBean("userServiceImpl");
userServiceImpl.add();
}
}