Java_Spring之基于注解的 AOP 配置
更新時間:2023年04月06日 10:14:06 作者:JiangTao_xlili
這篇文章主要介紹了Java_Spring中基于注解的AOP配置,我們要先進行環(huán)境的搭建,在進行注解配置,感興趣的同學可以參考閱讀
1 環(huán)境搭建
1.1 第一步:準備必要的代碼和 jar 包
- 拷貝上一小節(jié)的工程即可。
1.2 第二步:在配置文件中導入 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ù)庫操作對象 --> <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ù)層實現(xiàn)類
@Service("accountService") public class AccountServiceImpl implements IAccountService { @Autowired private IAccountDao accountDao; }
- 賬戶的持久層實現(xiàn)類
@Repository("accountDao") public class AccountDaoImpl implements IAccountDao { @Autowired private DBAssit dbAssit ; }
1.4 第四步:在配置文件中指定 spring 要掃描的包
<!-- 告知 spring,在創(chuàng)建容器時要掃描的包 --> <context:component-scan base-package="com.itheima"></context:component-scan>
2 配置步驟
2.1 第一步:把通知類也使用注解配置
- 事務(wù)控制類
@Component("txManager") public class TransactionManager { //定義一個 DBAssit @Autowired private DBAssit dbAssit ; }
2.2 第二步:在通知類上使用@Aspect 注解聲明為切面
- 作用:
- 把當前類聲明為切面類。
- 事務(wù)控制類
@Component("txManager") @Aspect//表明當前類是一個切面類 public class TransactionManager { //定義一個 DBAssit @Autowired private DBAssit dbAssit ; }
2.3 第三步:在增強的方法上使用注解配置通知
2.3.1 @Before
- 作用:
- 把當前方法看成是前置通知。
- 屬性:
- value:用于指定切入點表達式,還可以指定切入點表達式的引用。
//開啟事務(wù) @Before("execution(* com.itheima.service.impl.*.*(..))") public void beginTransaction() { try { dbAssit.getCurrentConnection().setAutoCommit(false); } catch (SQLException e) { e.printStackTrace(); } }
2.3.2 @AfterReturning
- 作用:
- 把當前方法看成是后置通知。
- 屬性:
- value:用于指定切入點表達式,還可以指定切入點表達式的引用
//提交事務(wù) @AfterReturning("execution(* com.itheima.service.impl.*.*(..))") public void commit() { try { dbAssit.getCurrentConnection().commit(); } catch (SQLException e) { e.printStackTrace(); } }
2.3.3 @AfterThrowing
- 作用:
- 把當前方法看成是異常通知。
- 屬性:
- value:用于指定切入點表達式,還可以指定切入點表達式的引用
//回滾事務(wù) @AfterThrowing("execution(* com.itheima.service.impl.*.*(..))") public void rollback() { try { dbAssit.getCurrentConnection().rollback(); } catch (SQLException e) { e.printStackTrace(); } }
2.3.4 @After
- 作用:
- 把當前方法看成是最終通知。
- 屬性:
- value:用于指定切入點表達式,還可以指定切入點表達式的引用
//釋放資源 @After("execution(* com.itheima.service.impl.*.*(..))") public void release() { try { dbAssit.releaseConnection(); } catch (Exception e) { e.printStackTrace(); } }
2.4 第四步:在 spring 配置文件中開啟 spring 對注解 AOP 的支持
<!-- 開啟 spring 對注解 AOP 的支持 --> <aop:aspectj-autoproxy/>
3 環(huán)繞通知注解配置 @Around
- 作用:
- 把當前方法看成是環(huán)繞通知。
- 屬性:
- value:用于指定切入點表達式,還可以指定切入點表達式的引用。
/** * 環(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 切入點表達式注解 @Pointcut
- 作用:
- 指定切入點表達式
- 屬性:
- value:指定表達式的內(nèi)容
@Pointcut("execution(* com.itheima.service.impl.*.*(..))") private void pt1() {} /** * 引用方式: * 環(huán)繞通知 * @param pjp * @return */ @Around("pt1()")//注意:千萬別忘了寫括號 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Jenkins自動化部署SpringBoot項目的實現(xiàn)
本文主要介紹了Jenkins自動化部署SpringBoot項目的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2023-01-01Java org.w3c.dom.Document 類方法引用報錯
這篇文章主要介紹了Java org.w3c.dom.Document 類方法引用報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08關(guān)于spring循環(huán)依賴問題及解決方案
這篇文章主要介紹了關(guān)于spring循環(huán)依賴問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06