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á)式的引用。
//開(kāi)啟事務(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 配置文件中開(kāi)啟 spring 對(duì)注解 AOP 的支持
<!-- 開(kāi)啟 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();
//前置通知:開(kāi)啟事務(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();
//前置通知:開(kāi)啟事務(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實(shí)現(xiàn)帶附件的郵件發(fā)送功能
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)帶附件的郵件發(fā)送功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
java學(xué)生管理系統(tǒng)界面簡(jiǎn)單實(shí)現(xiàn)(全)
這篇文章主要為大家詳細(xì)介紹了java學(xué)生管理系統(tǒng)界面的簡(jiǎn)單實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Spring Cloud-Feign服務(wù)調(diào)用的問(wèn)題及處理方法
Feign 是一個(gè)聲明式的 REST 客戶端,它用了基于接口的注解方式,很方便實(shí)現(xiàn)客戶端配置。接下來(lái)通過(guò)本文給大家介紹Spring Cloud-Feign服務(wù)調(diào)用,需要的朋友可以參考下2021-10-10
SpringBoot+Spring Security基于內(nèi)存用戶認(rèn)證的實(shí)現(xiàn)
本文介紹了SpringBoot+Spring Security基于內(nèi)存用戶認(rèn)證的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11
詳談jpa中表的@OneToMany等關(guān)聯(lián)關(guān)系
這篇文章主要介紹了詳談jpa中表的@OneToMany等關(guān)聯(lián)關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java實(shí)現(xiàn)LeetCode(1.兩數(shù)之和)
這篇文章主要介紹了Java實(shí)現(xiàn)LeetCode(兩數(shù)之和),本文使用java采用多種發(fā)放實(shí)現(xiàn)了LeetCode的兩數(shù)之和題目,需要的朋友可以參考下2021-06-06
java WebSocket客戶端斷線重連的實(shí)現(xiàn)方法
在工作中是否會(huì)遇到實(shí)用websocket客戶端連接服務(wù)端的時(shí)候,網(wǎng)絡(luò)波動(dòng),服務(wù)端斷連的情況,本文可以直接使用的斷線重連,感興趣的可以了解一下2021-10-10

