欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java_Spring之基于注解的 AOP 配置

 更新時(shí)間:2023年04月06日 10:14:06   作者:JiangTao_xlili  
這篇文章主要介紹了Java_Spring中基于注解的AOP配置,我們要先進(jìn)行環(huán)境的搭建,在進(jìn)行注解配置,感興趣的同學(xué)可以參考閱讀

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)文章

最新評(píng)論