AOP面向切麵編程 什麼是AOP AOP (Aspect Oriented Programming)意為:面向切麵編程,通過預編譯方式和運行期動態代理實現程式功能的統一維護的一種技術。AOP是OOP的延續,是軟體開發中的一個熱點,也是Spring框架中的一個重要內容,是函數式編程的一種衍生範型。利用 ...
AOP面向切麵編程
什麼是AOP
AOP (Aspect Oriented Programming)意為:面向切麵編程,通過預編譯方式和運行期動態代理實現程式功能的統一維護的一種技術。AOP是OOP的延續,是軟體開發中的一個熱點,也是Spring框架中的一個重要內容,是函數式編程的一種衍生範型。利用AOP可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程式的可重用性,同時提高了開發的效率。
OOP (Object Oriented Programming) 面向對象編程
AOP (Aspect Oritented Programming) 面向切麵編程
OOP 到AOP 不是替換的關係,而是一種擴展,使用了AOP後,OOP還是會繼續使用
Aop在Spring中的作用
提供聲明式事務;允許用戶自定義切麵
- 橫切關註點:跨越應用程式多個模塊的方法或功能。即是,與我們業務邏輯無關的,但是我們需要關註的部分,就是橫切關註點。如日誌,安全,緩存,事務等等.....
- 切麵(ASPECT)︰橫切關註點被模塊化的特殊對象。即,它是一個類。
- 通知(Advice) :切麵必須要完成的工作。即,它是類中的一個方法。·
- 目標(Target):被通知對象。
- 代理(Proxy) ︰向目標對象應用通知之後創建的對象。
- 切入點(PointCut) :切麵通知執行的“地點""的定義。
- 連接點(JointPoint) : 與切入點匹配的執行點。
AOP 主要就是在不改變原本代碼的前提下,新增功能上去不影響原本的功能。
AOP在Spring中是非常重要的一個功能,可以理解為一個業務就是一條線,當使用一把刀在這條線的指定位置砍下去,添加新的功能到斷開處,最後在進行織入,最後連起來成為了一條新的線,新的功能就可以實現。
使用Spring實現Aop
添加依賴
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.18</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.6</version>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.18</version>
</dependency>
註解實現
修改配置文件
<?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"
xmlns:context="http://www.springframework.org/schema/context"
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.xs">
<!--註解掃描 預設開啟註解支持-->
<context:component-scan base-package="com.bing"/>
<!--
proxy-target-class="true" 如果是true 就是cglib代理 如果是false就是jdk 預設是false
-->
<aop:aspectj-autoproxy />
</beans>
添加一個切麵類
package com.bing.aspect;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* @Author IBing
* @Date 2022/8/26 22:01
* @Version 1.0
*/
@Aspect //表示這是一個 切麵類
@Component //將對象放入spring容器
public class UserAspect {
//開啟日誌
private Logger logger = Logger.getLogger(UserAspect.class);
/**
* 目標方法執行之前執行
* @param joinPoint
*/
@Before("execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))")
public void before(JoinPoint joinPoint){
logger.debug("before");
logger.debug("攔截的目標對象"+joinPoint.getTarget());
logger.debug("攔截的方法"+joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName());
logger.debug("攔截的參數"+joinPoint.getArgs());
logger.debug("攔截的位置"+joinPoint.getStaticPart());
logger.debug("攔截的代理對象"+joinPoint.getStaticPart());
}
/**
* 無論是否拋出異常都會執行,相當於finally
* @param joinPoint
*/
@After("execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))")
public void after(JoinPoint joinPoint){
logger.debug("after");
logger.debug("攔截的目標對象"+joinPoint.getTarget());
logger.debug("攔截的方法"+joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName());
logger.debug("攔截的參數"+joinPoint.getArgs());
logger.debug("攔截的位置"+joinPoint.getStaticPart());
logger.debug("攔截的代理對象"+joinPoint.getStaticPart());
}
/**
* 相當於try
* @param joinPoint
*/
@AfterReturning("execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))")
public void afterReturning(JoinPoint joinPoint){
logger.debug("afterReturning");
logger.debug("攔截的目標對象"+joinPoint.getTarget());
logger.debug("攔截的方法"+joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName());
logger.debug("攔截的參數"+joinPoint.getArgs());
logger.debug("攔截的位置"+joinPoint.getStaticPart());
logger.debug("攔截的代理對象"+joinPoint.getStaticPart());
}
/**
* 相當於catch
* @param joinPoint
* @param e
*/
@AfterThrowing(value="execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))",throwing ="e" )
public void afterThrowing(JoinPoint joinPoint,RuntimeException e){
logger.debug("afterThrowing");
logger.debug("攔截的目標對象"+joinPoint.getTarget());
logger.debug("拋出的異常為",e);
}
/**
* 環繞執行
* @param pjd
* @return
* @throws Throwable
*/
@Around("execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))")
public Object around(ProceedingJoinPoint pjd) throws Throwable {
logger.debug("around之前");
Object proceed= pjd.proceed();
logger.debug("around之後");
return proceed;
}
}
測試
@Test
public void test( ){
//創建spring的容器並初始化
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("beans.xml");
String[] beanNameForType = classPathXmlApplicationContext.getBeanNamesForType(UserController.class);
System.out.println(Arrays.toString(beanNameForType));
UserController bean = (UserController) classPathXmlApplicationContext.getBean("userController");
bean.login("aa","123"); //這是 UserController 里寫好的login方法
classPathXmlApplicationContext.close();
}
輸出結果
因為沒有異常,所以 **afterThrowing **方法沒有執行
上面每個方法的切入點都一樣,代碼重覆,我們可以定義切入點,
//定義切入點
@Pointcut("execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))")
private void pointCut(){}
然後在切麵類的方法上使用切入點方法就行,這樣就減少了代碼重覆
/**
* 目標方法執行之前執行
* @param joinPoint
*/
@Before("pointCut()")
public void before(JoinPoint joinPoint){
logger.debug("before");
logger.debug("攔截的目標對象"+joinPoint.getTarget());
logger.debug("攔截的方法"+joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName());
logger.debug("攔截的參數"+joinPoint.getArgs());
logger.debug("攔截的位置"+joinPoint.getStaticPart());
logger.debug("攔截的代理對象"+joinPoint.getStaticPart());
}
下麵是一些切入點語法
execution() 切入點
直接精準到一個方法上面去
execution( public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))
任意許可權修飾符
execution( com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))
無返回類型
execution( void com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))
有返回類型
execution( !void com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))
任意返回類型
execution( * com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))
任意參數
execution( * com.bing.service.impl.UserServiceImpl.login(..))
類中的任意方法
execution( * com.bing.service.impl.UserServiceImpl.*(..))
類中以指定內容開頭的方法
execution( * com.bing.service.impl.UserServiceImpl.select*(..))
包中的任意類的任意方法不包含子包下麵的類
execution( * com.bing.service.impl.*.*(..))
包中及其下的任意類的任意方法
execution( * com.bing.service..*.*(..))
名詞解釋
pointcut 切入點 定義切入的連接點, 一般對應的就是表達式
aspect 切麵 擁有具體功能的一個類
advice 通知 切麵的具體實現 對應的就是切麵類中的方法
joinpoint 連接點 程式運行中可以插入切麵的地方 在spring中只能是方法 比如login方法
target 目標對象 切入的對象 這個對象包含了業務代碼的具體實現 比如:UserServiceImpl類的對象
proxy 代理對象 目標對象應用了通知以後創建的一個新的對象,這個對象中包含了原本的業務實現和擴展實現
weaving 織入 將通知應用到目標對象後創建代理對象的過程
XML實現AOP
首先在配置文件中進行AOP相應的配置
<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">
<!--開啟註解支持,,讓項目支持spring的註解-->
<!-- <context:annotation-config></context:annotation-config>-->
<!--註解掃描 預設開啟註解支持-->
<context:component-scan base-package="com.bing">
<!--設置需要掃描的註解-->
<!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>-->
<!--設置不掃描的註解-->
<!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Component"/>-->
</context:component-scan>
<!--
打開動態代理
proxy-target-class="true" 就是cglib代理 ,為false則會使用jdk代理實現,預設為false
-->
<aop:aspectj-autoproxy proxy-target-class="false"/>
<!-- 下麵這些就是AOP的XML實現方式-->
<bean id="userAspect" class="com.bing.aspect.UserAspect"/>
<aop:config>
<aop:aspect ref="userAspect">
<!-- 定義切麵-->
<aop:pointcut id="pc" expression="execution(* com.bing.service.impl.UserServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="pc"/>
<aop:after-returning method="afterReturning" pointcut-ref="pc"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pc" throwing="e"/>
<aop:after method="after" pointcut-ref="pc"/>
<!-- 如果不想使用已經定義的切入點 pc,也可以使用pointcut="execution()"來自己定義,這裡演示就使用相同的切入點-->
<aop:around method="around" pointcut="execution(* com.bing.service.impl.UserServiceImpl.*(..))"/>
</aop:aspect>
</aop:config>
</beans>
切麵類
package com.bing.aspect;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* @Author IBing
* @Date 2022/8/26 22:01
* @Version 1.0
*/
//@Aspect //表示這是一個 切麵類
@Component //將對象放入spring容器
public class UserAspect {
private Logger logger = Logger.getLogger(UserAspect.class);
/**
* 目標方法執行之前執行
* @param joinPoint
*/
public void before(JoinPoint joinPoint){
logger.debug("before");
logger.debug("攔截的目標對象"+joinPoint.getTarget());
logger.debug("攔截的方法"+joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName());
logger.debug("攔截的參數"+joinPoint.getArgs());
logger.debug("攔截的位置"+joinPoint.getStaticPart());
logger.debug("攔截的代理對象"+joinPoint.getStaticPart());
}
/**
* 無論是否拋出異常都會執行,相當於finally
* @param joinPoint
*/
public void after(JoinPoint joinPoint){
logger.debug("after");
logger.debug("攔截的目標對象"+joinPoint.getTarget());
logger.debug("攔截的方法"+joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName());
logger.debug("攔截的參數"+joinPoint.getArgs());
logger.debug("攔截的位置"+joinPoint.getStaticPart());
logger.debug("攔截的代理對象"+joinPoint.getStaticPart());
}
/**
* 相當於try
* @param joinPoint
*/
public void afterReturning(JoinPoint joinPoint){
logger.debug("afterReturning");
logger.debug("攔截的目標對象"+joinPoint.getTarget());
logger.debug("攔截的方法"+joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName());
logger.debug("攔截的參數"+joinPoint.getArgs());
logger.debug("攔截的位置"+joinPoint.getStaticPart());
logger.debug("攔截的代理對象"+joinPoint.getStaticPart());
}
/**
* 相當於catch
* @param joinPoint
* @param e
*/
public void afterThrowing(JoinPoint joinPoint,RuntimeException e){
logger.debug("afterThrowing");
logger.debug("攔截的目標對象"+joinPoint.getTarget());
logger.debug("拋出的異常為",e);
}
/**
* 環繞執行
* @param pjd
* @return
* @throws Throwable
*/
public Object around(ProceedingJoinPoint pjd) throws Throwable {
logger.debug("around之前");
Object proceed= pjd.proceed();
logger.debug("around之後");
return proceed;
}
}
運行結果