Java Spring之XML的AOP怎么配置

发布时间:2023-04-08 17:21:03 作者:iii
来源:亿速云 阅读:111

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

1 环境搭建

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

1.2 第二步:拷贝必备的 jar 包到工程的 lib 目录

Java Spring之XML的AOP怎么配置

1.3 第三步:创建 spring 的配置文件并导入约束

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  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"></beans&gt;

1.4 第四步:配置 spring 的 ioc

<!-- 配置 service --><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"> <property name="accountDao" ref="accountDao"></property></bean><!-- 配置 dao --><bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"> <property name="dbAssit" ref="dbAssit"></property></bean><!-- 配置数据库操作对象 --><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>

1.5 第五步:抽取公共代码制作成通知

public class TransactionManager {
 
    //定义一个 DBAssit
    private DBAssit dbAssit ;
    
    public void setDbAssit(DBAssit dbAssit) {
        this.dbAssit = dbAssit;
    }
 
    //开启事务
    public void beginTransaction() {
 
    
        try {
        
            dbAssit.getCurrentConnection().setAutoCommit(false);
        } catch (SQLException e) {
 
            e.printStackTrace();
        }
    }
 
 
    //提交事务
    public void commit() {
 
        try {
            dbAssit.getCurrentConnection().commit();
        } catch (SQLException e) {
 
            e.printStackTrace();
        }
    }
 
    //回滚事务
    public void rollback() {
 
        try {
            dbAssit.getCurrentConnection().rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
 
    //释放资源
    public void release() {
 
        try {
            dbAssit.releaseConnection();
        } catch (Exception e) {
 
            e.printStackTrace();
        }
    }
}

2 配置步骤

2.1 第一步:把通知类用 bean 标签配置起来

<!-- 配置通知 --><bean id="txManager" class="com.itheima.utils.TransactionManager"> <property name="dbAssit" ref="dbAssit"></property></bean>

2.2 第二步:使用 aop:config 声明 aop 配置

<aop:config> <!-- 配置的代码都写在此处 --></aop:config>

2.3 第三步:使用 aop:aspect 配置切面

<aop:aspect id="txAdvice" ref="txManager"> <!--配置通知的类型要写在此处--></aop:aspect>

2.4 第四步:使用 aop:pointcut 配置切入点表达式

<aop:pointcut expression="execution( public void com.itheima.service.impl.AccountServiceImpl.transfer( java.lang.String, java.lang.String, java.lang.Float ))" id="pt1"/>

2.5 第五步:使用 aop:xxx 配置对应的通知类型

<aop:before method="beginTransaction" pointcut-ref="pt1"/>
<aop:after-returning method="commit" pointcut-ref="pt1"/>
<aop:after-throwing method="rollback" pointcut-ref="pt1"/>
<aop:after method="release" pointcut-ref="pt1"/>

3 切入点表达式说明

public void com.itheima.service.impl.AccountServiceImpl.saveAccount(com.itheima.domain.Account)

访问修饰符可以省略

void com.itheima.service.impl.AccountServiceImpl.saveAccount(com.itheima.domain.Account)

返回值可以使用*号,表示任意返回值

* com.itheima.service.impl.AccountServiceImpl.saveAccount(com.itheima.domain.Account)

包名可以使用*号,表示任意包,但是有几级包,需要写几个*

* *.*.*.*.AccountServiceImpl.saveAccount(com.itheima.domain.Account)

使用..来表示当前包,及其子包

* com..AccountServiceImpl.saveAccount(com.itheima.domain.Account)

类名可以使用*号,表示任意类

* com..*.saveAccount(com.itheima.domain.Account)

方法名可以使用*号,表示任意方法

* com..*.*( com.itheima.domain.Account)

参数列表可以使用*,表示参数可以是任意数据类型,但是必须有参数

* com..*.*(*)

参数列表可以使用..表示有无参数均可,有参数可以是任意类型

* com..*.*(..)

全通配方式:

* *..*.*(..)

注: 通常情况下,我们都是对业务层的方法进行增强,所以切入点表达式都是切到业务层实现类。

execution(* com.itheima.service.impl.*.*(..))

4 环绕通知

配置方式:

<aop:config>
    <aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))" id="pt1"/>
    <aop:aspect id="txAdvice" ref="txManager">
        <!-- 配置环绕通知 -->
        <aop:around method="transactionAround" pointcut-ref="pt1"/>
    </aop:aspect>
</aop:config>
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;
}

到此,相信大家对“Java Spring之XML的AOP怎么配置”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. Java面试中常问的Spring方面问题
  2. 怎么在Spring中利用Java配置@Configuration和@Bean

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java spring aop

上一篇:Lambda表达式的方法和构造器怎么引用

下一篇:Python常用技巧有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》