SpringAOP 之 配置一个事务

本文介绍了如何在SpringAOP中配置事务管理,包括在applicationContext.xml中设置头文件,添加Maven pom.xml的必要依赖,以及创建相关环境。详细步骤涉及Dao层、Pojo实体类、Service业务层的配置,以及相应的XML文件配置。
摘要由CSDN通过智能技术生成

applicationContext.xml头文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd ">
</beans>

Maven:pom.xml所需依赖

<dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <!--springAOP及IOC所需依赖-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.5.4</version>
    </dependency>
    <!-- 阿里巴巴数据连接池-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.21</version>
    </dependency>
    <!--spring管理数据库依赖-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.2.1.RELEASE</version>
    </dependency>
    <!-- 连接oracle数据库驱动 -->
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>oracle-jdbc</artifactId>
        <version>11.2.0.1.0</version>
    </dependency>

创建所需环境

  • Dao
//接口
public interface AccountDao {
    boolean outMoney();
    boolean inMoney();
}
//实现
public class AccountDaoImpl implements AccountDao {

    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public boolean outMoney() {
        String sql = "update ACCOUNT SET MONEY = MONEY-1000 WHERE ID = 1";
        int update = jdbcTemplate.update(sql);
        if (update == 1)
            return true;
        return false;
    }

    @Override
    public boolean inMoney() {
        String sql = "update ACCOUNT SET MONEY = MONEY+1000 WHERE ID = 2";
        int update = jdbcTemplate.update(sql);
        if (update == 1)
            return true;
        return false;
    }
}
  • Pojo(实体类)
public class Account {
    //根据数据库定义属性
}
  • Service(业务层)
public class Application {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService = ac.getBean("accountService", AccountService.class);
//        accountService.inMoney();
//        accountService.outMoney();
        accountService.pay();
//        accountService.pay2();
    }
}
  • xml文件
<!--配置Dao-->
    <bean id="accountDao" class="com.dzqc.transcation.dao.impl.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
    <!--使用springjdbc连接数据库-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@10.211.55.14:1521:orcl"/>
        <property name="username" value="scott"/>
        <property name="password" value="12345"/>
    </bean>
    <!-- 这是spring提供的一个操作数据库的工具类 ,它要用到dataSource,所以要在属性上配上dataSource -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--将事务交予spring管理-->
    <bean id="accountService" class="com.dzqc.transcation.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>

    <!-- 配置事务增强 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!-- 配置事务增强 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!--
                name		:绑定事务的方法名,可以使用通配符,可以配置多个。
                propagation	:传播行为
                isolation	:隔离级别
                read-only	:是否只读
                timeout		:超时信息
                rollback-for:发生哪些异常回滚.
                no-rollback-for:发生哪些异常不回滚.
             -->
            <!-- 哪些方法加事务 -->
            <tx:method name="pay" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!-- 配置AOP切面产生代理 -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.dzqc.transcation.service.impl.AccountServiceImpl.pay())"/>
    </aop:config>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值