Java Spring AOP詳解
1.什么是AOP?
AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,通過預編譯方式和運行期間動態(tài)代理實現(xiàn)程序功能的統(tǒng)一維護的一種技術。AOP是OOP的延續(xù),是軟件開發(fā)中的一個熱點,也是Spring框架中的一個重要內容,是函數(shù)式編程的一種衍生范型。利用AOP可以對業(yè)務邏輯的各個部分進行隔離,從而使得業(yè)務邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發(fā)的效率。

2.AOP在Spring中的作用
提供聲明式事務:允許用戶自定義切面
- 橫切關注點:跨越應用程序多個模塊的方法或功能.既是,與我們業(yè)務邏輯無關,但是我們需要關注的部分,就是橫切關注點.如日志,安全,緩存,事務等…
- 切面(ASPECT):橫切關注點 被模塊化 的特殊對象。即,它是一個類。
- 通知(Advice):切面必須要完成的工作。即,它是類中的一個方法。
- 目標(Target):被通知對象。
- 代理(Proxy):向目標對象應用通知之后創(chuàng)建的對象。
- 切入點(PointCut):切面通知 執(zhí)行的 “地點”的定義。
- 連接點(JointPoint):與切入點匹配的執(zhí)行點。

SpringAOP中,通過Advice定義橫切邏輯,Spring中支持5種類型的Advice:

即 Aop 在 不改變原有代碼的情況下 , 去增加新的功能 .
3.使用Spring實現(xiàn)AOP
【重點】需要導入一個依賴包!
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
execution (* com.db.dao….(…)) 整個表達式可以分為五個部分:
1、execution(): 表達式主體。
2、第一個*星號:表示返回類型,星號表示所有的類型。注意星號后面有個空格。
3、包名com.db.dao:表示需要攔截的包名,只攔截這個包,其他包不攔截。
報名后面的兩個句點表示當前包和當前包的所有子包,com.db.dao包、子孫包下所有類的方法。4、第二個*號:表示類名,星號表示所有的類。
5、*(…)最后這個星號表示方法名,星號表示所有的方法,后面括弧里面表示方法的參數(shù),兩個句點表示任何參數(shù)。
方式一:使用Spring的接口
通過 Spring API 實現(xiàn)
首先編寫我們的業(yè)務接口和實現(xiàn)類
public interface UserService {
public void add();
public void delete();
public void update();
public void query();
}
public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println("add");
}
@Override
public void delete() {
System.out.println("delete");
}
@Override
public void update() {
System.out.println("update");
}
@Override
public void query() {
System.out.println("query");
}
}
然后去寫我們的增強類 , 我們編寫兩個 , 一個前置增強 一個后置增強
public class BeforeLog implements MethodBeforeAdvice {
/*
method:要執(zhí)行的目標對象的方法
objects:參數(shù)
target:目標對象
*/
@Override
public void before(Method method, Object[] objects, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName());
}
}
public class AfterLog implements AfterReturningAdvice {
@Override
//returnValue:返回值
public void afterReturning(Object returnValue, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("執(zhí)行了"+method.getName()+"方法,返回結果為:"+returnValue);
}
}
最后去spring的文件中注冊 , 并實現(xiàn)aop切入實現(xiàn) , 注意導入約束 .
<?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.kuang.service.UserServiceImpl"/>
<bean id="log" class="com.kuang.log.Log"/>
<bean id="afterLog" class="com.kuang.log.AfterLog"/>
<!--aop的配置-->
<aop:config>
<!--切入點 expression:表達式匹配要執(zhí)行的方法-->
<aop:pointcut id="pointcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
<!--執(zhí)行環(huán)繞; advice-ref執(zhí)行方法 . pointcut-ref切入點-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
測試
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//動態(tài)代理的必須是接口
UserService userService = (UserService) context.getBean("userService");
userService.add();
userService.delete();
userService.update();
userService.query();
}
}
方法二:使用自定義類來實現(xiàn)
目標業(yè)務類不變依舊是userServiceImpl
第一步 : 寫我們自己的一個切入類
//自定義切入點類
public class DiyPoint {
public void before(){
System.out.println("-----方法執(zhí)行前-----");
}
public void after(){
System.out.println("-----方法執(zhí)行后-----");
}
}
在Spring中配置
<!--第二種方式自定義實現(xiàn)-->
<!--注冊bean-->
<bean id="diy" class="com.kuang.config.DiyPointcut"/>
<!--aop的配置-->
<aop:config>
<!--第二種方式:使用AOP的標簽實現(xiàn)-->
<aop:aspect ref="diy">
<aop:pointcut id="diyPonitcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
<aop:before pointcut-ref="diyPonitcut" method="before"/>
<aop:after pointcut-ref="diyPonitcut" method="after"/>
</aop:aspect>
</aop:config>
方法三:使用注解實現(xiàn)
第一步:編寫一個注解實現(xiàn)的增強類
@Aspect //標注這個類為一個切面
public class AnnotationPointCut {
@Before("execution(* com.hui.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("-----方法執(zhí)行前-----");
}
@After("execution(* com.hui.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("-----方法執(zhí)行后-----");
}
//在環(huán)繞增強中,我們可以給定一個參數(shù),代表我們要獲取處理切入的點
@Around("execution(* com.hui.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("環(huán)繞前");
//執(zhí)行方法
Object proceed = jp.proceed();
System.out.println("環(huán)繞后");
}
}
第二步:在Spring配置文件中,注冊bean,并增加支持注解的配置
<!-- 方式三-->
<bean id="annotationPointCut" class="com.hui.diy.AnnotationPointCut"/>
<!--開啟注解支持-->
<aop:aspectj-autoproxy/>
aop:aspectj-autoproxy說明:
通過aop命名空間的<aop:aspectj-autoproxy />聲明自動為spring容器中那些配置@aspectJ切面的bean創(chuàng)建代理,織入切面。當然,spring 在內部依舊采用AnnotationAwareAspectJAutoProxyCreator進行自動代理的創(chuàng)建工作,但具體實現(xiàn)的細節(jié)已經被<aop:aspectj-autoproxy />隱藏起來了
<aop:aspectj-autoproxy
/>有一個proxy-target-class屬性,默認為false,表示使用jdk動態(tài)代理織入增強,當配為<aop:aspectj-autoproxy
poxy-target-class=“true”/>時,表示使用CGLib動態(tài)代理技術織入增強。不過即使proxy-target-class設置為false,如果目標類沒有聲明接口,則spring將自動使用CGLib動態(tài)代理。
總結
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內容!
相關文章
spring?boot?validation參數(shù)校驗與分組嵌套各種類型及使用小結
參數(shù)校驗基本上是controller必做的事情,畢竟前端傳過來的一切都不可信,validation可以簡化這一操作,這篇文章主要介紹了spring?boot?validation參數(shù)校驗分組嵌套各種類型及使用小結,需要的朋友可以參考下2023-09-09
淺談Java循環(huán)中的For和For-each哪個更快
本文主要介紹了淺談Java循環(huán)中的For和For-each哪個更快,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08
Spring boot2X Consul如何使用Feign實現(xiàn)服務調用
這篇文章主要介紹了spring boot2X Consul如何使用Feign實現(xiàn)服務調用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12
SpringBoot整合Elasticsearch實現(xiàn)索引和文檔的操作方法
Elasticsearch 基于 Apache Lucene 構建,采用 Java 編寫,并使用 Lucene 構建索引、提供搜索功能,本文分步驟通過綜合案例給大家分享SpringBoot整合Elasticsearch的相關知識,感興趣的朋友跟隨小編一起看看吧2021-05-05

