Java_Spring之基于注解的 AOP 配置
1 環(huán)境搭建
1.1 第一步:準(zhǔn)備必要的代碼和 jar 包
- 拷貝上一小節(jié)的工程即可。
1.2 第二步:在配置文件中導(dǎo)入 context 的名稱空間
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置數(shù)據(jù)庫(kù)操作對(duì)象 --> <bean id="dbAssit" class="com.itheima.dbassit.DBAssit"> <property name="dataSource" ref="dataSource"></property> <!-- 指定 connection 和線程綁定 --> <property name="useCurrentConnection" value="true"></property> </bean> <!-- 配置數(shù)據(jù)源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///spring_day02"></property> <property name="user" value="root"></property> <property name="password" value="1234"></property> </bean> </beans>
1.3 第三步:把資源使用注解配置
- 賬戶的業(yè)務(wù)層實(shí)現(xiàn)類
@Service("accountService") public class AccountServiceImpl implements IAccountService { @Autowired private IAccountDao accountDao; }
- 賬戶的持久層實(shí)現(xiàn)類
@Repository("accountDao") public class AccountDaoImpl implements IAccountDao { @Autowired private DBAssit dbAssit ; }
1.4 第四步:在配置文件中指定 spring 要掃描的包
<!-- 告知 spring,在創(chuàng)建容器時(shí)要掃描的包 --> <context:component-scan base-package="com.itheima"></context:component-scan>
2 配置步驟
2.1 第一步:把通知類也使用注解配置
- 事務(wù)控制類
@Component("txManager") public class TransactionManager { //定義一個(gè) DBAssit @Autowired private DBAssit dbAssit ; }
2.2 第二步:在通知類上使用@Aspect 注解聲明為切面
- 作用:
- 把當(dāng)前類聲明為切面類。
- 事務(wù)控制類
@Component("txManager") @Aspect//表明當(dāng)前類是一個(gè)切面類 public class TransactionManager { //定義一個(gè) DBAssit @Autowired private DBAssit dbAssit ; }
2.3 第三步:在增強(qiáng)的方法上使用注解配置通知
2.3.1 @Before
- 作用:
- 把當(dāng)前方法看成是前置通知。
- 屬性:
- value:用于指定切入點(diǎn)表達(dá)式,還可以指定切入點(diǎn)表達(dá)式的引用。
//開啟事務(wù) @Before("execution(* com.itheima.service.impl.*.*(..))") public void beginTransaction() { try { dbAssit.getCurrentConnection().setAutoCommit(false); } catch (SQLException e) { e.printStackTrace(); } }
2.3.2 @AfterReturning
- 作用:
- 把當(dāng)前方法看成是后置通知。
- 屬性:
- value:用于指定切入點(diǎn)表達(dá)式,還可以指定切入點(diǎn)表達(dá)式的引用
//提交事務(wù) @AfterReturning("execution(* com.itheima.service.impl.*.*(..))") public void commit() { try { dbAssit.getCurrentConnection().commit(); } catch (SQLException e) { e.printStackTrace(); } }
2.3.3 @AfterThrowing
- 作用:
- 把當(dāng)前方法看成是異常通知。
- 屬性:
- value:用于指定切入點(diǎn)表達(dá)式,還可以指定切入點(diǎn)表達(dá)式的引用
//回滾事務(wù) @AfterThrowing("execution(* com.itheima.service.impl.*.*(..))") public void rollback() { try { dbAssit.getCurrentConnection().rollback(); } catch (SQLException e) { e.printStackTrace(); } }
2.3.4 @After
- 作用:
- 把當(dāng)前方法看成是最終通知。
- 屬性:
- value:用于指定切入點(diǎn)表達(dá)式,還可以指定切入點(diǎn)表達(dá)式的引用
//釋放資源 @After("execution(* com.itheima.service.impl.*.*(..))") public void release() { try { dbAssit.releaseConnection(); } catch (Exception e) { e.printStackTrace(); } }
2.4 第四步:在 spring 配置文件中開啟 spring 對(duì)注解 AOP 的支持
<!-- 開啟 spring 對(duì)注解 AOP 的支持 --> <aop:aspectj-autoproxy/>
3 環(huán)繞通知注解配置 @Around
- 作用:
- 把當(dāng)前方法看成是環(huán)繞通知。
- 屬性:
- value:用于指定切入點(diǎn)表達(dá)式,還可以指定切入點(diǎn)表達(dá)式的引用。
/** * 環(huán)繞通知 * @param pjp * @return */ @Around("execution(* com.itheima.service.impl.*.*(..))") public Object transactionAround(ProceedingJoinPoint pjp) { //定義返回值 Object rtValue = null; try { //獲取方法執(zhí)行所需的參數(shù) Object[] args = pjp.getArgs(); //前置通知:開啟事務(wù) beginTransaction(); //執(zhí)行方法 rtValue = pjp.proceed(args); //后置通知:提交事務(wù) commit(); }catch(Throwable e) { //異常通知:回滾事務(wù) rollback(); e.printStackTrace(); }finally { //最終通知:釋放資源 release(); } return rtValue; }
4 切入點(diǎn)表達(dá)式注解 @Pointcut
- 作用:
- 指定切入點(diǎn)表達(dá)式
- 屬性:
- value:指定表達(dá)式的內(nèi)容
@Pointcut("execution(* com.itheima.service.impl.*.*(..))") private void pt1() {} /** * 引用方式: * 環(huán)繞通知 * @param pjp * @return */ @Around("pt1()")//注意:千萬(wàn)別忘了寫括號(hào) public Object transactionAround(ProceedingJoinPoint pjp) { //定義返回值 Object rtValue = null; try { //獲取方法執(zhí)行所需的參數(shù) Object[] args = pjp.getArgs(); //前置通知:開啟事務(wù) beginTransaction(); //執(zhí)行方法 rtValue = pjp.proceed(args); //后置通知:提交事務(wù) commit(); }catch(Throwable e) { //異常通知:回滾事務(wù) rollback(); e.printStackTrace(); }finally { //最終通知:釋放資源 release(); } return rtValue; }
5 不使用 XML 的配置方式
@Configuration @ComponentScan(basePackages="com.itheima") @EnableAspectJAutoProxy public class SpringConfiguration { }
到此這篇關(guān)于Java_Spring之基于注解的 AOP 配置的文章就介紹到這了,更多相關(guān)Java Spring AOP配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用Java反射機(jī)制實(shí)現(xiàn)對(duì)象相同字段的復(fù)制操作
這篇文章主要介紹了利用Java反射機(jī)制實(shí)現(xiàn)對(duì)象相同字段的復(fù)制操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08java中棧和隊(duì)列的實(shí)現(xiàn)和API的用法(詳解)
下面小編就為大家?guī)?lái)一篇java中棧和隊(duì)列的實(shí)現(xiàn)和API的用法(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05Jenkins自動(dòng)化部署SpringBoot項(xiàng)目的實(shí)現(xiàn)
本文主要介紹了Jenkins自動(dòng)化部署SpringBoot項(xiàng)目的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2023-01-01Java org.w3c.dom.Document 類方法引用報(bào)錯(cuò)
這篇文章主要介紹了Java org.w3c.dom.Document 類方法引用報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Idea2020 無(wú)法share項(xiàng)目到svn的解決方法
這篇文章主要介紹了Idea2020 無(wú)法share項(xiàng)目到svn的解決方法,需要的朋友可以參考下2020-09-09關(guān)于spring循環(huán)依賴問(wèn)題及解決方案
這篇文章主要介紹了關(guān)于spring循環(huán)依賴問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06非常適合新手學(xué)生的Java線程池優(yōu)化升級(jí)版
作者是一個(gè)來(lái)自河源的大三在校生,以下筆記都是作者自學(xué)之路的一些淺薄經(jīng)驗(yàn),如有錯(cuò)誤請(qǐng)指正,將來(lái)會(huì)不斷的完善筆記,幫助更多的Java愛(ài)好者入門2022-03-03