Java AOP實(shí)踐指南:切面編程詳解
哈嘍,大家好,我是了不起。
AOP就是面向切面編程,或者叫面向方面編程,或者開(kāi)玩笑的說(shuō)叫面向方便面編程,如果粗俗的理解,就是可以自定義注解,然后通過(guò)自己定義的方式定義注解的作用。
什么是SpringAOP
SpringAOP的全稱是(Aspect Oriented Programming)中文翻譯過(guò)來(lái)是面向切面編程,AOP是OOP的延續(xù),是軟件開(kāi)發(fā)中的一個(gè)熱點(diǎn),也是Spring框架中的一個(gè)重要內(nèi)容,是函數(shù)式編程的一種衍生范型。利用AOP可以對(duì)業(yè)務(wù)邏輯的各個(gè)部分進(jìn)行隔離,從而使得業(yè)務(wù)邏輯各部分之間的耦合度降低,提高程序的可重用性,同時(shí)提高了開(kāi)發(fā)的效率。
圖片
AOP體系
圖片
SpringAOP的應(yīng)用場(chǎng)景
- 日志記錄
- 權(quán)限驗(yàn)證(SpringSecurity有使用)
- 事務(wù)控制(調(diào)用方法前開(kāi)啟事務(wù), 調(diào)用方法后提交關(guān)閉事務(wù) )
- 效率檢查(檢測(cè)方法運(yùn)行時(shí)間)
- 數(shù)據(jù)源代理(seata里面,獲取到數(shù)據(jù)源連接執(zhí)行的sql)
- 緩存優(yōu)化 (第一次調(diào)用查詢數(shù)據(jù)庫(kù),將查詢結(jié)果放入內(nèi)存對(duì)象, 第二次調(diào)用, 直接從內(nèi)存對(duì)象返回,不需要查詢數(shù)據(jù)庫(kù) )
Aop在 Spring 中的作用
提供聲明式事允許用戶自定義切面:
- 橫切關(guān)注點(diǎn):跨越應(yīng)用程序多個(gè)橫塊的方法或功能,即是,與我們業(yè)務(wù)邏輯無(wú)關(guān)的,但是我們需要關(guān)注的部分,就是橫切關(guān)注點(diǎn)。如日志,安全,緩存,事務(wù)等等.
- 切面( ASPECT ):橫切關(guān)注點(diǎn)被模塊化的特殊對(duì)象,即,它是一個(gè)類。
- 通知( Advice ):切面必須要完成的工作,即,它是類中的一個(gè)方法。
- 目標(biāo)( Target ):被通知象·代理( Proxy ):向目標(biāo)對(duì)象應(yīng)用通知之后創(chuàng)建的對(duì)象
- 切入點(diǎn)( PointCut ):切面通知執(zhí)行的"地點(diǎn)的定義
- 連接點(diǎn)( JointPoint ):與切入點(diǎn)匹配的執(zhí)行點(diǎn)
圖片
AOP的實(shí)現(xiàn)方式
使用AOP織入,需要導(dǎo)入一個(gè)依賴包:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
使用方式
applicationContext.xml:
<?xml versinotallow="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
">
<!--注冊(cè)bean-->
<bean id="userService" class="service.UserServiceImpl"/>
<bean id="log" class="log.Log"/>
<bean id="afterLog" class="log.AfterLog"/>
<!--配置aop:需要導(dǎo)入aop的約束-->
<aop:config>
<!--切入點(diǎn):expression:表達(dá)式,execution(要執(zhí)行的位置! * * * *)-->
<aop:pointcut id="pointcut" expression="execution(* service.UserServiceImpl.*(..))"></aop:pointcut>
<!--執(zhí)行環(huán)繞增加-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
UserService接口:
public interface UserService {
public void add();
public void delete();
public void update();
public void select();
}
UserServiceImpl實(shí)現(xiàn)類(切入點(diǎn)):
public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println("增加了一個(gè)用戶");
}
@Override
public void delete() {
System.out.println("刪除了一個(gè)用戶");
}
@Override
public void update() {
System.out.println("更新了一個(gè)用戶");
}
@Override
public void select() {
System.out.println("查詢了一個(gè)用戶");
}
}
前置通知:
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
//method:要執(zhí)行的目標(biāo)對(duì)象的方法
//args:參數(shù)
//target:目標(biāo)參數(shù)
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被執(zhí)行了");
}
}
后置通知:
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
//returnValue:返回值
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("執(zhí)行了"+method.getName()+"方法返回結(jié)果為:"+ returnValue);
}
}
測(cè)試類:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;
import service.UserServiceImpl;
import java.lang.annotation.Annotation;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//動(dòng)態(tài)代理的是接口
UserService userService = (UserService) context.getBean("userService");
userService.select();
}
}
結(jié)語(yǔ)
通過(guò)本文的講解,我們深入了解了切面編程的核心概念、動(dòng)態(tài)代理的實(shí)現(xiàn)原理,并通過(guò)一個(gè)實(shí)際的例子展示了使用Java AOP的完整過(guò)程。AOP可以幫助我們將橫切關(guān)注點(diǎn)(例如日志記錄、事務(wù)管理等)從核心業(yè)務(wù)邏輯中解耦出來(lái),提高代碼的可維護(hù)性和重用性。同時(shí),AOP也是實(shí)現(xiàn)設(shè)計(jì)模式和架構(gòu)思想的重要手段之一,我們?cè)陂_(kāi)發(fā)中可以靈活運(yùn)用AOP來(lái)優(yōu)化代碼結(jié)構(gòu)并提高系統(tǒng)的整體性能。