一篇文章從無(wú)到有詳解Spring中的AOP
前言
AOP (Aspect Orient Programming),直譯過(guò)來(lái)就是 面向切面編程。AOP 是一種編程思想,是面向?qū)ο缶幊蹋∣OP)的一種補(bǔ)充。面向?qū)ο缶幊虒⒊绦虺橄蟪筛鱾€(gè)層次的對(duì)象,而面向切面編程是將程序抽象成各個(gè)切面。
從《Spring實(shí)戰(zhàn)(第4版)》圖書中扒了一張圖:

從該圖可以很形象地看出,所謂切面,相當(dāng)于應(yīng)用對(duì)象間的橫切點(diǎn),我們可以將其單獨(dú)抽象為單獨(dú)的模塊。
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置Service -->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<!-- 注入dao -->
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 通知方法 -->
<!-- 配置事務(wù)管理器-->
<bean id="txManager" class="com.itheima.utils.TransactionManager">
<!-- 注入ConnectionUtils -->
<property name="connectionUtils" ref="connectionUtils"></property>
</bean>
<!-- 切面 -->
<aop:config>
<aop:pointcut id="pc" expression="execution(* com.itheima.service.impl.*.*(..))"/>
<aop:aspect ref="txManager">
<aop:before method="beginTransaction" pointcut-ref="pc"/>
<aop:after-returning method="commit" pointcut-ref="pc"/>
<aop:after-throwing method="rollback" pointcut-ref="pc"/>
<aop:after method="release" pointcut-ref="pc"/>
</aop:aspect>
</aop:config>
AOP(Aspect Oriented Programming)
面向切面編程。橫向重復(fù),縱向抽取。
簡(jiǎn)單的說(shuō)它就是把我們程序重復(fù)的代碼抽取出來(lái),在需要執(zhí)行的時(shí)候,使用動(dòng)態(tài)代理的技術(shù),在不修改源碼的基礎(chǔ)上,對(duì)我們的已有方法進(jìn)行增強(qiáng)。
實(shí)現(xiàn)原理:動(dòng)態(tài)代理
事務(wù)處理方式
動(dòng)態(tài)代理之前
在我們?cè)嫉目刂剖聞?wù)中,
ConnectionUtils類:控制單線程內(nèi)只使用一個(gè)數(shù)據(jù)庫(kù)連接(connection)--->TransactionManager類 ,書寫方法:
1) 打開手動(dòng)提交事務(wù)conn.setAutoCommit(false);
2) 提交事務(wù)conn.commit();
3) 回滾事務(wù)conn.rollback();
4) 釋放當(dāng)前數(shù)據(jù)庫(kù)連接(手寫)。--->在業(yè)務(wù)層中,
try{
1)
業(yè)務(wù)方法
2)
}
catch(Throws t){
3)
} finally{
4)
}
在每個(gè)需要事務(wù)控制的方法都像這樣加上事務(wù)控制。
這樣書寫的業(yè)務(wù)層的代碼,過(guò)于臃腫,重復(fù)代碼過(guò)多。
動(dòng)態(tài)代理實(shí)現(xiàn)
事先寫一個(gè)生成創(chuàng)建Service的代理對(duì)象的工廠類
/**
* 用于創(chuàng)建Service的代理對(duì)象的工廠
*/
public class BeanFactory {
private IAccountService accountService;
private TransactionManager txManager;
public void setTxManager(TransactionManager txManager) {
this.txManager = txManager;
}
public final void setAccountService(IAccountService accountService) {
this.accountService = accountService;
}
/**
* 獲取Service代理對(duì)象
* @return
*/
public IAccountService getAccountService() {
return (IAccountService)Proxy.newProxyInstance(accountService.getClass().getClassLoader(),
accountService.getClass().getInterfaces(),
new InvocationHandler() {
/**
* 添加事務(wù)的支持
*
* @param proxy
* @param method
* @param args
* @return
* @throws Throwable
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object rtValue = null;
try {
//1.開啟事務(wù)
txManager.beginTransaction();
//2.執(zhí)行操作
rtValue = method.invoke(accountService, args);
//3.提交事務(wù)
txManager.commit();
//4.返回結(jié)果
return rtValue;
} catch (Exception e) {
//5.回滾操作
txManager.rollback();
throw new RuntimeException(e);
} finally {
//6.釋放連接
txManager.release();
}
}
});
}
BeanFactory 使用動(dòng)態(tài)代理返回一個(gè) IAccountService 對(duì)象并調(diào)用相應(yīng)方法。
把這個(gè)創(chuàng)建的對(duì)象存入Spring容器中,并注入原來(lái)的 accountService 和 txManager(事務(wù)管理工具類);
<!--配置beanfactory-->
<bean id="beanFactory" class="com.itheima.factory.BeanFactory">
<!-- 注入service -->
<property name="accountService" ref="accountService"></property>
<!-- 注入事務(wù)管理器 -->
<property name="txManager" ref="txManager"></property>
</bean>
把動(dòng)態(tài)代理創(chuàng)建的 IAccountService ,也存入到 Spring 容器中。
<!--配置代理的service-->
<bean id="proxyAccountService" factory-bean="beanFactory" factory-method="getAccountService"></bean>
把動(dòng)態(tài)代理創(chuàng)建的 IAccountService ,也存入到 Spring 容器中。
<!--配置代理的service--> <bean id="proxyAccountService" factory-bean="beanFactory" factory-method="getAccountService"></bean>
測(cè)試方法:
/**
* 使用Junit單元測(cè)試:測(cè)試我們的配置
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {
@Autowired
@Qualifier("proxyAccountService")
private IAccountService as;
@Test
public void testTransfer(){
as.transfer("aaa","bbb",100f);
}
}
在做業(yè)務(wù)層的事務(wù)控制時(shí),可直接調(diào)用工廠類創(chuàng)建出的代理對(duì)象,實(shí)現(xiàn)事務(wù)控制,從而也使程序員在寫業(yè)務(wù)層時(shí)只管寫業(yè)務(wù),而不用管事務(wù)代碼。
當(dāng)然我們能想到這樣的辦法,spring也早就封裝好了,在書寫xml配置文件時(shí)更加簡(jiǎn)潔,可觀。
AOP的使用
通過(guò)配置的方式實(shí)現(xiàn)上述功能,不用再自己書寫工廠類。
AOP的相關(guān)術(shù)語(yǔ)
- Joinpoint(連接點(diǎn)):
所謂連接點(diǎn)是指那些被攔截到的點(diǎn)。在spring中,這些點(diǎn)指的是方法,因?yàn)閟pring只支持方法類型的連接點(diǎn)。 - Pointcut(切入點(diǎn)):
所謂切入點(diǎn)是指我們要對(duì)哪些Joinpoint進(jìn)行攔截的定義 - Advice(通知/增強(qiáng)):
所謂通知是指攔截到Joinpoint之后所要做的事情就是通知。
通知的類型:前置通知,后置通知,異常通知,最終通知,環(huán)繞通知。 - Introduction(引介):
引介是一種特殊的通知在不修改類代碼的前提下, Introduction可以在運(yùn)行期為類動(dòng)態(tài)地添加一些方法或Field。 - Target(目標(biāo)對(duì)象):
代理的目標(biāo)對(duì)象。 - Weaving(織入):
是指把增強(qiáng)應(yīng)用到目標(biāo)對(duì)象來(lái)創(chuàng)建新的代理對(duì)象的過(guò)程。
spring采用動(dòng)態(tài)代理織入,而AspectJ采用編譯期織入和類裝載期織入。 - Proxy(代理):
一個(gè)類被AOP織入增強(qiáng)后,就產(chǎn)生一個(gè)結(jié)果代理類。 - Aspect(切面):
是切入點(diǎn)和通知(引介)的結(jié)合。
學(xué)習(xí)Spring中的AOP要明確的事
a、開發(fā)階段(我們做的)
編寫核心業(yè)務(wù)代碼(開發(fā)主線):大部分程序員來(lái)做,要求熟悉業(yè)務(wù)需求。
把公用代碼抽取出來(lái),制作成通知。(開發(fā)階段最后再做):AOP編程人員來(lái)做。
在配置文件中,聲明切入點(diǎn)與通知間的關(guān)系,即切面。:AOP編程人員來(lái)做。
b、運(yùn)行階段(Spring框架完成的)
Spring框架監(jiān)控切入點(diǎn)方法的執(zhí)行。一旦監(jiān)控到切入點(diǎn)方法被運(yùn)行,使用代理機(jī)制,動(dòng)態(tài)創(chuàng)建目標(biāo)對(duì)象的代理對(duì)象,根據(jù)通知類別,在代理對(duì)象的對(duì)應(yīng)位置,將通知對(duì)應(yīng)的功能織入,完成完整的代碼邏輯運(yùn)行。
步驟
1、導(dǎo)包
2、書寫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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置Service -->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<!-- 注入dao -->
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 通知方法 -->
<!-- 配置事務(wù)管理器-->
<bean id="txManager" class="com.itheima.utils.TransactionManager">
<!-- 注入ConnectionUtils -->
<property name="connectionUtils" ref="connectionUtils"></property>
</bean>
<!-- 切面 -->
<aop:config>
<aop:pointcut id="pc" expression="execution(* com.itheima.service.impl.*.*(..))"/>
<aop:aspect ref="txManager">
<aop:before method="beginTransaction" pointcut-ref="pc"/>
<aop:after-returning method="commit" pointcut-ref="pc"/>
<aop:after-throwing method="rollback" pointcut-ref="pc"/>
<aop:after method="release" pointcut-ref="pc"/>
</aop:aspect>
</aop:config>
3.業(yè)務(wù)層代碼
public void transfer(String sourceName, String targetName, Float money) {
System.out.println("transfer....");
//2.1根據(jù)名稱查詢轉(zhuǎn)出賬戶
Account source = accountDao.findAccountByName(sourceName);
//2.2根據(jù)名稱查詢轉(zhuǎn)入賬戶
Account target = accountDao.findAccountByName(targetName);
//2.3轉(zhuǎn)出賬戶減錢
source.setMoney(source.getMoney()-money);
//2.4轉(zhuǎn)入賬戶加錢
target.setMoney(target.getMoney()+money);
//2.5更新轉(zhuǎn)出賬戶
accountDao.updateAccount(source);
// int i=1/0;
//2.6更新轉(zhuǎn)入賬戶
accountDao.updateAccount(target);
}
4.測(cè)試
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {
@Autowired
@Qualifier("accountService")
private IAccountService as;
@Test
public void testTransfer(){
as.transfer("aaa","bbb",100f);
}
}
使用spring的AOP基于動(dòng)態(tài)代理開發(fā),簡(jiǎn)潔的實(shí)現(xiàn)了該對(duì)象方法的增強(qiáng),也就是實(shí)現(xiàn)了對(duì)轉(zhuǎn)賬的事務(wù)控制。
/**
* 使用Junit單元測(cè)試:測(cè)試我們的配置
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {
@Autowired
@Qualifier("proxyAccountService")
private IAccountService as;
@Test
public void testTransfer(){
as.transfer("aaa","bbb",100f);
}
}
總結(jié)
到此這篇關(guān)于詳解Spring中AOP的文章就介紹到這了,更多相關(guān)詳解Spring中AOP內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot自定義MessageConverter與內(nèi)容協(xié)商管理器contentNegotiationManag
這篇文章主要介紹了SpringBoot自定義MessageConverter與內(nèi)容協(xié)商管理器contentNegotiationManager的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-10-10
Java實(shí)現(xiàn)簡(jiǎn)單員工管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單員工管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
在SpringBoot中實(shí)現(xiàn)線程池并行處理任務(wù)的方法詳解
在使用Spring Boot開發(fā)應(yīng)用程序時(shí),我們經(jīng)常需要處理一些耗時(shí)的任務(wù),例如網(wǎng)絡(luò)請(qǐng)求、數(shù)據(jù)庫(kù)操作或者其他需要花費(fèi)一定時(shí)間的計(jì)算任務(wù),本文將介紹如何在Spring Boot中使用線程池來(lái)實(shí)現(xiàn)任務(wù)的并行處理2023-06-06
8個(gè)簡(jiǎn)單部分開啟Java語(yǔ)言學(xué)習(xí)之路 附j(luò)ava學(xué)習(xí)書單
8個(gè)簡(jiǎn)單部分開啟Java語(yǔ)言學(xué)習(xí)之路,附j(luò)ava學(xué)習(xí)書單,這篇文章主要向大家介紹了學(xué)習(xí)java語(yǔ)言的方向,感興趣的小伙伴們可以參考一下2016-09-09
如何使用@AllArgsConstructor和final 代替 @Autowired
這篇文章主要介紹了使用@AllArgsConstructor和final 代替 @Autowired方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Idea中mapper注入報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了Idea中mapper注入報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Spring Boot 項(xiàng)目做性能監(jiān)控的操作流程
這篇文章主要介紹了Spring Boot 項(xiàng)目如何做性能監(jiān)控,本文通過(guò)實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Spring?MVC?前端控制器?(DispatcherServlet)處理流程解析
DispatcherServlet是前置控制器,配置在web.xml文件中的,這篇文章主要介紹了Spring?MVC?前端控制器?(DispatcherServlet)處理流程,需要的朋友可以參考下2022-05-05

