欢迎您来到,李雷博客 | PHP博客        登录  |  注册

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

更新:2023-04-06 18:37:32
人气:253
来源:互联网转载
A+

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

    1 环境搭建

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

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

    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">
     
        <!-- 配置数据库操作对象 -->
        <bean id="dbAssit" class="com.itheima.dbassit.DBAssit">
            <property name="dataSource" ref="dataSource"></property>
            
            <!-- 指定 connection 和线程绑定 -->
            <property name="useCurrentConnection" value="true"></property>
        </bean>
     
        <!-- 配置数据源 -->
        <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 第三步:把资源使用注解配置

    • 账户的业务层实现类

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

    @Repository("accountDao")
    public class AccountDaoImpl implements IAccountDao {
        @Autowired
        private DBAssit dbAssit ;
    }

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

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

    2 配置步骤

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

    • 事务控制类

    @Component("txManager")
    public class TransactionManager {
     
        //定义一个 DBAssit
        @Autowired
        private DBAssit dbAssit ;
    }

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

    • 作用:

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

    • 事务控制类

    @Component("txManager")
    @Aspect//表明当前类是一个切面类
    public class TransactionManager {
     
        //定义一个 DBAssit
        @Autowired
        private DBAssit dbAssit ;
    }

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

    2.3.1 @Before

    • 作用:

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

    • 属性:

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

    //开启事务
    @Before("execution(* com.itheima.service.impl.*.*(..))")
    public void beginTransaction() {
     
        try {
            dbAssit.getCurrentConnection().setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    2.3.2 @AfterReturning

    • 作用:

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

    • 属性:

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

    //提交事务
    @AfterReturning("execution(* com.itheima.service.impl.*.*(..))")
    public void commit() {
     
        try {
            dbAssit.getCurrentConnection().commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    2.3.3 @AfterThrowing

    • 作用:

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

    • 属性:

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

    //回滚事务
    @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 环绕通知注解配置 @Around

    • 作用:

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

    • 属性:

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

    /**
    * 环绕通知
    * @param pjp
    * @return
    */
    @Around("execution(* com.itheima.service.impl.*.*(..))")
    public Object transactionAround(ProceedingJoinPoint pjp) {
     
        //定义返回值
        Object rtValue = null;
     
        try {
        
            //获取方法执行所需的参数
            Object[] args = pjp.getArgs();
        
            //前置通知:开启事务
            beginTransaction();
     
            //执行方法
            rtValue = pjp.proceed(args);
     
            //后置通知:提交事务
            commit();
            }catch(Throwable e) {
     
            //异常通知:回滚事务
            rollback();
            e.printStackTrace();
        }finally {
     
        //最终通知:释放资源
        release();
        }
     
        return rtValue;
    }

    4 切入点表达式注解 @Pointcut

    • 作用:

      • 指定切入点表达式

    • 属性:

      • value:指定表达式的内容

    @Pointcut("execution(* com.itheima.service.impl.*.*(..))")
    private void pt1() {}
     
        /**
        * 引用方式:
        * 环绕通知
        * @param pjp
        * @return
        */
        @Around("pt1()")//注意:千万别忘了写括号
        public Object transactionAround(ProceedingJoinPoint pjp) {
     
        //定义返回值
        Object rtValue = null;
     
        try {
            //获取方法执行所需的参数
            Object[] args = pjp.getArgs();
     
            //前置通知:开启事务
            beginTransaction();
            
            //执行方法
            rtValue = pjp.proceed(args);
     
            //后置通知:提交事务
            commit();
        }catch(Throwable e) {
     
            //异常通知:回滚事务
            rollback();
            e.printStackTrace();
        }finally {
     
            //最终通知:释放资源
            release();
        }
        
        return rtValue;
    }

    5 不使用 XML 的配置方式

    @Configuration
    @ComponentScan(basePackages="com.itheima")
    @EnableAspectJAutoProxy
    public class SpringConfiguration {
     
    }
    推荐的文章
    网站地图
    会员服务
    关于我们
    QQ:858353007
     
    广告服务
    加我微信
    移动端访问
     
     
    Copyright © 2014- 2024 www.mdaima.com All Rights Reserved.
    李雷博客,专注PHP经验、PHP教程及PHP源代码开源下载分享的PHP博客!   ICP备案号:京ICP备10202169号-4