Java Spring之基于注解的AOP怎么配置

其他教程   发布日期:2024年11月26日   浏览次数:190

本篇内容主要讲解“Java Spring之基于注解的AOP怎么配置”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Java Spring之基于注解的AOP怎么配置”吧!

    1 环境搭建

    1.1 第一步:准备必要的代码和 jar 包

    • 拷贝上一小节的工程即可。

    1.2 第二步:在配置文件中导入 context 的名称空间

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:aop="http://www.springframework.org/schema/aop"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans
    7. http://www.springframework.org/schema/beans/spring-beans.xsd
    8. http://www.springframework.org/schema/aop
    9. http://www.springframework.org/schema/aop/spring-aop.xsd
    10. http://www.springframework.org/schema/context
    11. http://www.springframework.org/schema/context/spring-context.xsd">
    12. <!-- 配置数据库操作对象 -->
    13. <bean id="dbAssit" class="com.itheima.dbassit.DBAssit">
    14. <property name="dataSource" ref="dataSource"></property>
    15. <!-- 指定 connection 和线程绑定 -->
    16. <property name="useCurrentConnection" value="true"></property>
    17. </bean>
    18. <!-- 配置数据源 -->
    19. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    20. <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    21. <property name="jdbcUrl" value="jdbc:mysql:///spring_day02"></property>
    22. <property name="user" value="root"></property>
    23. <property name="password" value="1234"></property>
    24. </bean>
    25. </beans>

    1.3 第三步:把资源使用注解配置

    • 账户的业务层实现类

    1. @Service("accountService")
    2. public class AccountServiceImpl implements IAccountService {
    3. @Autowired
    4. private IAccountDao accountDao;
    5. }
    • 账户的持久层实现类

    1. @Repository("accountDao")
    2. public class AccountDaoImpl implements IAccountDao {
    3. @Autowired
    4. private DBAssit dbAssit ;
    5. }

    1.4 第四步:在配置文件中指定 spring 要扫描的包

    1. <!-- 告知 spring,在创建容器时要扫描的包 -->
    2. <context:component-scan base-package="com.itheima"></context:component-scan>

    2 配置步骤

    2.1 第一步:把通知类也使用注解配置

    • 事务控制类

    1. @Component("txManager")
    2. public class TransactionManager {
    3. //定义一个 DBAssit
    4. @Autowired
    5. private DBAssit dbAssit ;
    6. }

    2.2 第二步:在通知类上使用@Aspect 注解声明为切面

    • 作用:

      • 把当前类声明为切面类。

    • 事务控制类

    1. @Component("txManager")
    2. @Aspect//表明当前类是一个切面类
    3. public class TransactionManager {
    4. //定义一个 DBAssit
    5. @Autowired
    6. private DBAssit dbAssit ;
    7. }

    2.3 第三步:在增强的方法上使用注解配置通知

    2.3.1 @Before

    • 作用:

      • 把当前方法看成是前置通知。

    • 属性:

      • value:用于指定切入点表达式,还可以指定切入点表达式的引用。

    1. //开启事务
    2. @Before("execution(* com.itheima.service.impl.*.*(..))")
    3. public void beginTransaction() {
    4. try {
    5. dbAssit.getCurrentConnection().setAutoCommit(false);
    6. } catch (SQLException e) {
    7. e.printStackTrace();
    8. }
    9. }

    2.3.2 @AfterReturning

    • 作用:

      • 把当前方法看成是后置通知。

    • 属性:

      • value:用于指定切入点表达式,还可以指定切入点表达式的引用

    1. //提交事务
    2. @AfterReturning("execution(* com.itheima.service.impl.*.*(..))")
    3. public void commit() {
    4. try {
    5. dbAssit.getCurrentConnection().commit();
    6. } catch (SQLException e) {
    7. e.printStackTrace();
    8. }
    9. }

    2.3.3 @AfterThrowing

    • 作用:

      • 把当前方法看成是异常通知。

    • 属性:

      • value:用于指定切入点表达式,还可以指定切入点表达式的引用

    1. //回滚事务
    2. @AfterThrowing("execution(* com.itheima.service.impl.*.*(..))")
    3. public void rollback() {
    4. try {
    5. dbAssit.getCurrentConnection().rollback();
    6. } catch (SQLException e) {
    7. e.printStackTrace();
    8. }
    9. }

    2.3.4 @After

    • 作用:

      • 把当前方法看成是最终通知。

    • 属性:

      • value:用于指定切入点表达式,还可以指定切入点表达式的引用

    1. //释放资源
    2. @After("execution(* com.itheima.service.impl.*.*(..))")
    3. public void release() {
    4. try {
    5. dbAssit.releaseConnection();
    6. } catch (Exception e) {
    7. e.printStackTrace();
    8. }
    9. }

    2.4 第四步:在 spring 配置文件中开启 spring 对注解 AOP 的支持

    1. <!-- 开启 spring 对注解 AOP 的支持 -->
    2. <aop:aspectj-autoproxy/>

    3 环绕通知注解配置 @Around

    • 作用:

      • 把当前方法看成是环绕通知。

    • 属性:

      • value:用于指定切入点表达式,还可以指定切入点表达式的引用。

    1. /**
    2. * 环绕通知
    3. * @param pjp
    4. * @return
    5. */
    6. @Around("execution(* com.itheima.service.impl.*.*(..))")
    7. public Object transactionAround(ProceedingJoinPoint pjp) {
    8. //定义返回值
    9. Object rtValue = null;
    10. try {
    11. //获取方法执行所需的参数
    12. Object[] args = pjp.getArgs();
    13. //前置通知:开启事务
    14. beginTransaction();
    15. //执行方法
    16. rtValue = pjp.proceed(args);
    17. //后置通知:提交事务
    18. commit();
    19. }catch(Throwable e) {
    20. //异常通知:回滚事务
    21. rollback();
    22. e.printStackTrace();
    23. }finally {
    24. //最终通知:释放资源
    25. release();
    26. }
    27. return rtValue;
    28. }

    4 切入点表达式注解 @Pointcut

    • 作用:

      • 指定切入点表达式

    • 属性:

      • value:指定表达式的内容

    1. @Pointcut("execution(* com.itheima.service.impl.*.*(..))")
    2. private void pt1() {}
    3. /**
    4. * 引用方式:
    5. * 环绕通知
    6. * @param pjp
    7. * @return
    8. */
    9. @Around("pt1()")//注意:千万别忘了写括号
    10. public Object transactionAround(ProceedingJoinPoint pjp) {
    11. //定义返回值
    12. Object rtValue = null;
    13. try {
    14. //获取方法执行所需的参数
    15. Object[] args = pjp.getArgs();
    16. //前置通知:开启事务
    17. beginTransaction();
    18. //执行方法
    19. rtValue = pjp.proceed(args);
    20. //后置通知:提交事务
    21. commit();
    22. }catch(Throwable e) {
    23. //异常通知:回滚事务
    24. rollback();
    25. e.printStackTrace();
    26. }finally {
    27. //最终通知:释放资源
    28. release();
    29. }
    30. return rtValue;
    31. }

    5 不使用 XML 的配置方式

    1. @Configuration
    2. @ComponentScan(basePackages="com.itheima")
    3. @EnableAspectJAutoProxy
    4. public class SpringConfiguration {
    5. }

    以上就是Java Spring之基于注解的AOP怎么配置的详细内容,更多关于Java Spring之基于注解的AOP怎么配置的资料请关注九品源码其它相关文章!