AOP
什么是AOP
? 面向切面編程,通過預編譯的方式和運行期動態代理實作程式功能的統一維護的一種技術,AOP是OOP的延續,是軟體開發中的一個熱點,也是Spring框架中的一個重要內容,是函式式編程的一種衍生泛型,利用AOP可以對業務邏輯的各個部分進行隔離,從而使業務邏輯各個部分的耦合度降低,提高程式的可重用性,同時提高了開發效率,
AOP在Spring中的作用
- 提供宣告式事務;允許用戶自定義切面
核心名詞
- 橫切關注點:橫跨應用程式多個模塊的方法或功能,即是,與我們業務邏輯無關的,但是我們需要關注的地方,就是橫切關注點,如:日志、安全、快取、事務
- 切面:橫切關注點被模塊化的特性物件,即:它是一個類
- 通知:切面必須要完成的作業,即它是類中的一個方法
- 目標:被通知物件
- 代理:向目標物件應用通知以后創建的物件,
- 切入點:切面通知執行的“地點的定義
- 連接點:與切入點匹配的執行點
Spring中支持的五種型別的Advice
| 通知型別 | 連接點 | 實作介面 |
|---|---|---|
| 前置通知 | 方法前 | MethodBeforeAdvice |
| 后置通知 | 方法后 | AfterReturningAdvice |
| 環繞通知 | 方法前后 | MethodInterceptor |
| 例外拋出通知 | 方法拋出例外 | ThrowsAdvice |
| 引介通知 | 類中增加新方法屬性 | IntroductionOnterceptor |
即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 {
public void add();
public void delete();
public void update();
public void search();
}
public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println("增加用戶");
}
@Override
public void delete() {
System.out.println("洗掉用戶");
}
@Override
public void update() {
System.out.println("更新用戶");
}
@Override
public void search() {
System.out.println("查詢用戶");
}
}
增強類
前置增強
public class Log implements MethodBeforeAdvice {
//method : 要執行的目標物件的方法
//objects : 被呼叫的方法的引數
//Object : 目標物件
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println( o.getClass().getName() + "的" + method.getName() + "方法被執行了");
}
}
后置增強
public class AfterLog implements AfterReturningAdvice {
//returnValue 回傳值
//method被呼叫的方法
//args 被呼叫的方法的物件的引數
//target 被呼叫的目標物件
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("執行了" + target.getClass().getName()
+"的"+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" />
<bean id="log" />
<bean id="afterLog" />
<!--aop的配置-->
<aop:config>
<!--切入點 expression:運算式匹配要執行的方法 -->
<aop:pointcut id="pointcut" expression="execution(* com.zhonghu.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 = new ClassPathXmlApplicationContext("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" />
<!--aop的配置-->
<aop:config>
<!--第二種方式:使用AOP的標簽實作-->
<aop:aspect ref="diy">
<aop:pointcut id="diyPonitcut" expression="execution(* com.zhonghu.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 = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}
第三種方式——使用注解
注解實作的增強類
package com.zhonghu.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 PointCut {
@Before("execution(* com.zhonghu.pojo.User.*(..))")
public void befer(){
System.out.println("方法執行前");
}
@After("execution(* com.zhonghu.pojo.User.*(..))")
public void after(){
System.out.println("方法執行后");
}
//在環繞增強中,我們可以給定一個引數,代表我們要處理切入的點,
@Around("execution(* com.zhonghu.pojo.User.*(..))")
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" />
<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-/>時,表示使用CGLib動態代理技術織入增強,不過即使proxy-target-class設定為false,如果目標類沒有宣告介面,則spring將自動使用CGLib動態代理
最后
- 如果覺得看完有識訓,希望能給我點個贊,這將會是我更新的最大動力,感謝各位的支持
- 歡迎各位關注我的公眾號【java冢狐】,專注于java和計算機基礎知識,保證讓你看完有所識訓,不信你打我
- 如果看完有不同的意見或者建議,歡迎多多評論一起交流,感謝各位的支持以及厚愛,

歡迎關注公眾號“Java冢狐”獲取最新訊息
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/247967.html
標籤:其他
下一篇:《清單革命》讀書筆記
