AOP面向切面編程
什么是AOP
AOP(Aspect Oriented Programming)意為:面向切面編程,通過預編譯方式和運行期動態代理實現程序功能的統一維護的一種技術。AOP是OOP的延續,是軟件開發中的一個熱點,也是Spring框架中的一個重要內容,是函數式編程的一種衍生范型。利用AOP可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發的效率。
Aop在Spring中的作用
提供聲明式事務;允許用戶自定義切面
以下名詞需要了解下:
- 橫切關注點:跨越應用程序多個模塊的方法或功能。即是,與我們業務邏輯無關的,但是我們需要關注的部分,就是橫切關注點。如日志 , 安全 , 緩存 , 事務等等 ....
- 切面(ASPECT):橫切關注點 被模塊化 的特殊對象。即,它是一個類。
- 通知(Advice):切面必須要完成的工作。即,它是類中的一個方法。
- 目標(Target):被通知對象。
- 代理(Proxy):向目標對象應用通知之后創建的對象。
- 切入點(PointCut):切面通知 執行的 “地點”的定義。
- 連接點(JointPoint):與切入點匹配的執行點。
SpringAOP中,通過Advice定義橫切邏輯,Spring中支持5種類型的Advice:
即 Aop 在 不改變原有代碼的情況下 , 去增加新的功能 .
使用Spring實現Aop
【重點】使用AOP織入,需要導入一個依賴包!
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
第一種方式
通過 Spring API 實現
首先編寫我們的業務接口和實現類
public interface UserService {
void add();
void delete();
void update();
void search();
}
class UserServiceImpl implements UserService{
public void add() {
System.out.println("增加用戶");
}
public void delete() {
System.out.println("刪除用戶");
}
public void update() {
System.out.println("更新用戶");
}
public void search() {
System.out.println("查詢用戶");
}
}
然后去寫我們的增強類 , 我們編寫兩個 , 一個前置增強 一個后置增強
public class Log implements MethodBeforeAdvice {
//method : 要執行的目標對象的方法
//objects : 被調用的方法的參數
//Object : 目標對象
public void before(Method method, Object[] args, Object target) throws Throwable{
System.out.println(target.getClass().getName()+"的"+method.getName()+"被執行了");
}
}
public class AfterLog implements AfterReturningAdvice {
//returnValue 返回值
//method被調用的方法
//args 被調用的方法的對象的參數
//target 被調用的目標對象
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable{
System.out.println("執行了" +method.getName()+"方法返回結果為:"+returnValue);
}
}
最后去spring的文件中注冊 , 並實現aop切入實現 , 注意導入約束 .
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注冊bean-->
<bean id="userService" class="com.maple.service.UserServiceImpl"/>
<bean id="log" class="com.maple.log.Log"/>
<bean id="afterLog" class="com.maple.log.AfterLog"/>
<!--aop的配置-->
<aop:config>
<!--切入點 expression:表達式匹配要執行的方法-->
<aop:pointcut id="pointcut" expression="execution(* com.maple.service.UserServiceImpl.*(..))"/>
<!--執行環繞; advice-ref執行方法 . pointcut-ref切入點-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
測試
public class MyTest {
@Test
public void test(){
ApplicationContext context = newClassPathXmlApplicationContext("beans.xml");
UserService userService = (UserService) context.getBean("userService");
userService.search();
}
}
Aop的重要性 : 很重要 . 一定要理解其中的思路 , 主要是思想的理解這一塊 .
Spring的Aop就是將公共的業務 (日志 , 安全等) 和領域業務結合起來 , 當執行領域業務時 , 將會把公共業務加進來 . 實現公共業務的重復利用 . 領域業務更純粹 , 程序猿專注領域業務 , 其本質還是動態代理 .
第二種方式
自定義類來實現Aop
目標業務類不變依舊是userServiceImpl
第一步 : 寫我們自己的一個切入類
public class DiyPointcut {
public void before(){
System.out.println("---------方法執行前---------");
}
public void after(){
System.out.println("---------方法執行后---------");
}
}
去spring中配置
<!--第二種方式自定義實現-->
<!--注冊bean-->
<bean id="diy" class="com.maple.config.DiyPointcut"/>
<!--aop的配置-->
<aop:config>
<!--第二種方式:使用AOP的標簽實現-->
<aop:aspect ref="diy">
<aop:pointcut id="diyPonitcut" expression="execution(* com.maple.service.UserServiceImpl.*(..))"/>
<aop:before pointcut-ref="diyPonitcut" method="before"/>
<aop:after pointcut-ref="diyPonitcut" method="after"/>
</aop:aspect>
</aop:config>
測試:
public class MyTest {
@Test
public void test(){
ApplicationContext context = newClassPathXmlApplicationContext("beans.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}
第三種方式
使用注解實現
第一步:編寫一個注解實現的增強類
package com.maple.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;
@Aspect
public class AnnotationPointcut {
@Before("execution(* com.maple.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("---------方法執行前---------");
}
@After("execution(* com.maple.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("---------方法執行后---------");
}
//在環繞增強中,可以給定一個參數,代表要獲取處理切入的點。
@Around("execution(* com.maple.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("環繞前");
System.out.println("簽名:"+jp.getSignature());
//執行目標方法proceed
Object proceed = jp.proceed();
System.out.println("環繞后");
System.out.println(proceed);
}
}
第二步:在Spring配置文件中,注冊bean,並增加支持注解的配置
<!--第三種方式:注解實現-->
<bean id="annotationPointcut" class="com.maple.config.AnnotationPointcut"/>
<aop:aspectj-autoproxy/>
aop:aspectj-autoproxy:說明
通過aop命名空間的<aop:aspectj-autoproxy />聲明自動為spring容器中那些配置@aspectJ切面的bean創建代理,織入切面。當然,spring 在內部依舊采用AnnotationAwareAspectJAutoProxyCreator進行自動代理的創建工作,但具體實現的細節已經被<aop:aspectj-autoproxy />隱藏起來了
<aop:aspectj-autoproxy />有一個proxy-target-class屬性,默認為false,表示使用jdk動態代理織入增強,當配為<aop:aspectj-autoproxy poxy-target-class="true"/>時,表示使用CGLib動態代理技術織入增強。不過即使proxy-target-class設置為false,如果目標類沒有聲明接口,則spring將自動使用CGLib動態代理。
AspectJ的Execution表達式
execution (* com.maple.service.UserServiceImpl.*(..))
execution()是最常用的切點函數,其語法如下所示:
整個表達式可以分為五個部分:
1、execution(): 表達式主體。
2、第一個*號:表示返回類型, *號表示所有的類型。
3、包名:表示需要攔截的包名
5、*(..):最后這個星號表示方法名,*號表示所有的方法,后面括弧里面表示方法的參數,兩個句點表示任何參數
【文章地址】
原文鏈接
B站地址:https://space.bilibili.com/95256449