什麼是AOP
AOP(Aspect Oriented Programming),意為面向切面程式設計,通過預編譯方式和執行期間動態代理實現程式功能的統一維護的一種技術。AOP是OOP的延續,是軟體開發中的一個熱點,也是Spring框架中的一個重要內容,是函數語言程式設計的一種衍生範型。利用AOP可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程式的可重用性,同時提高了開發的效率。
AOP在Spring中的作用
--提供宣告式事務:允許使用者自定義切面--
- 橫切關注點:跨越應用程式多個模組的方法或功能,即與業務邏輯無關但需要關注的部分(日誌、安全、快取、事務等等)
- 切面(Aspect):【橫切關注點】模組化的特殊物件(一個類)
- 通知(Advice):切面必須要完成的工作(類中方法)
- 目標(Target):被通知目標
- 代理(Proxy):向目標物件應用通知後建立的物件
- 切入點(PointCut):切面通知執行的“地點”的定義
- 連線點(JoinPoint):與切入點匹配的執行點
AOP實現
【搭建環境(普通Maven專案)】
【匯入依賴】
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
<scope>runtime</scope>
</dependency>
【編寫業務類(示例)】
- UserService介面
package cn.iris.service;
/**
* @author Iris 2021/8/12
*/
public interface UserService {
void add();
void del();
void update();
void query();
}
- UserService實現類
package cn.iris.service;
/**
* @author Iris 2021/8/12
*/
public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println("增加一個使用者");
}
@Override
public void del() {
System.out.println("刪除一個使用者");
}
@Override
public void update() {
System.out.println("更新一個使用者");
}
@Override
public void query() {
System.out.println("查詢一個使用者");
}
}
- 日誌類(示例)
- 前置日誌類
package cn.iris.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
/**
* @author Iris 2021/8/12
*/
public class Log implements MethodBeforeAdvice {
/**
* 前置日誌
* @param method 要執行的目標物件的方法
* @param args 引數
* @param target 目標物件
* @throws Throwable
*/
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+ method.getName()+"被執行");
}
}
- 結尾日誌類
package cn.iris.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
/**
* @author Iris 2021/8/12
*/
public class AfterLog implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+ method.getName()+"被執行後返回"+returnValue);
}
}
- 測試類
import cn.iris.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Iris 2021/8/12
*/
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationConfig.xml");
// 動態代理是代理的介面
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}
方式一:使用Spring的介面
【Spring API 介面實現】
- Spring配置檔案
<?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 class="cn.iris.service.UserServiceImpl" id="userService"/>
<bean class="cn.iris.log.Log" id="log"/>
<bean class="cn.iris.log.AfterLog" id="afterLog"/>
<!--方式一:使用原生Spring API介面-->
<!--配置aop(匯入aop約束)-->
<aop:config>
<!--切入點 execution: 要執行的位置-->
<aop:pointcut id="point" expression="execution(* cn.iris.service.UserServiceImpl.*(..))"/>
<!--執行環繞增加-->
<aop:advisor advice-ref="afterLog" pointcut-ref="point"/>
<aop:advisor advice-ref="log" pointcut-ref="point"/>
</aop:config>
</beans>
方式二:使用自定義類實現
【重點:切面定義】
- Class DiyPointCut
package cn.iris.diy;
/**
* @author Iris 2021/8/13
*/
public class DiyPointCut {
public void before() {
System.out.println("-----Before Method-----");
}
public void after() {
System.out.println("-----After Method-----");
}
}
- applicationConfig.xml
<!--方式二:自定義類-->
<bean class="cn.iris.diy.DiyPointCut" id="diy"/>
<aop:config>
<!--自定義切面,ref引用自定義類-->
<aop:aspect ref="diy">
<!--切入點-->
<aop:pointcut id="point" expression="execution(* cn.iris.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
方式三:註解實現AOP
- Class AnnotationPointCut
package cn.iris.diy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
/**
* 方式三:使用註解實現AOP
* @author Iris 2021/8/13
*/
@Aspect //標註該類為一個切面
public class AnnotationPointCut {
@Before("execution(* cn.iris.service.UserServiceImpl.*(..))")
public void before() {
System.out.println("-----Before Method-----");
}
@After("execution(* cn.iris.service.UserServiceImpl.*(..))")
public void after() {
System.out.println("-----After Method-----");
}
@Around("execution(* cn.iris.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint pjp) {
System.out.println("-----Before Around Method-----");
try {
Signature signature = pjp.getSignature();
System.out.println("Signature : "+signature);
Object proceed = pjp.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("-----After Around Method-----");
}
}
- applicationConfig.xml
<!--方式三:使用註解實現AOP-->
<bean class="cn.iris.diy.AnnotationPointCut" id="anno"/>
<!--開啟註解支援 JDK(預設:proxy-target-class="false") cglib(proxy-target-class="true")-->
<aop:aspectj-autoproxy/>