• javaee spring aop实现事务 项目结构


    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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
           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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <!-- 1.开启 注解 -->
        <context:component-scan base-package="com.test" />
        <!-- 2.创建数据源对象-->
        <context:property-placeholder location="db.properties" />
        <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${driverClass}" />
            <property name="jdbcUrl" value="${url}" />
            <property name="user" value="${user}" />
            <property name="password" value="${password}" />
        </bean>
        <!-- 3.创建JdbcTemplate对象-->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <constructor-arg name="dataSource" ref="comboPooledDataSource" />
        </bean>
    
        <!-- 4 创建一个事务管理器 -->
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="comboPooledDataSource" />
        </bean>
    
        <!-- 5.定义事务方法 (增强代码) -->
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="transfer" isolation="DEFAULT"
                propagation="REQUIRED" rollback-for="MyException"/>
            </tx:attributes>
        </tx:advice>
        <!-- 6.织入 -->
        <aop:config>
            <!-- 定义切点-->
            <aop:pointcut id="pc" expression="execution(* com..CardInfoService.transfer(..))"></aop:pointcut>
            <!-- 织入-->
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pc" />
        </aop:config>
    
    
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    实现类

    package com.test.service.impl;
    
    import com.test.dao.ICardInfoDao;
    import com.test.exception.MyException;
    import com.test.service.ICardInfoService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class CardInfoService implements ICardInfoService {
    
        @Autowired
        private ICardInfoDao cardInfoDao;
    
        public ICardInfoDao getCardInfoDao() {
            return cardInfoDao;
        }
    
        public void setCardInfoDao(ICardInfoDao cardInfoDao) {
            this.cardInfoDao = cardInfoDao;
        }
    
        //实现转账方法
        @Override
        public void transfer(int from, int to, float money) throws Exception {
    
            //一方减钱
            cardInfoDao.decreaseMoney(from,money);
    
            if(true)
                throw  new MyException("转账异常");
            //一方加钱
            cardInfoDao.increaseMoney(to,money);
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    异常类

    package com.test.exception;
    
    //模拟一个异常类
    public class MyException extends Exception {
    
        public MyException()
        {
            super();
        }
    
        public MyException(String message)
        {
            super(message);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    pom依赖

    
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0modelVersion>
    
      <groupId>org.examplegroupId>
      <artifactId>testSpring11artifactId>
      <version>1.0-SNAPSHOTversion>
      <packaging>warpackaging>
    
      <name>testSpring11 Maven Webappname>
      
      <url>http://www.example.comurl>
    
      <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.7maven.compiler.source>
        <maven.compiler.target>1.7maven.compiler.target>
      properties>
    
      <dependencies>
        <dependency>
          <groupId>junitgroupId>
          <artifactId>junitartifactId>
          <version>4.11version>
          <scope>testscope>
        dependency>
    
        
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-coreartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-beansartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-contextartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-context-supportartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-expressionartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
    
        <dependency>
          <groupId>mysqlgroupId>
          <artifactId>mysql-connector-javaartifactId>
          <version>5.1.37version>
        dependency>
        <dependency>
          <groupId>com.mchangegroupId>
          <artifactId>c3p0artifactId>
          <version>0.9.5.2version>
        dependency>
        
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-jdbcartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
    
        
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-txartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
    
        
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-aspectsartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
    
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-aopartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
    
      dependencies>
    
      <build>
        <finalName>testSpring11finalName>
        <pluginManagement>
          <plugins>
            <plugin>
              <artifactId>maven-clean-pluginartifactId>
              <version>3.1.0version>
            plugin>
            
            <plugin>
              <artifactId>maven-resources-pluginartifactId>
              <version>3.0.2version>
            plugin>
            <plugin>
              <artifactId>maven-compiler-pluginartifactId>
              <version>3.8.0version>
            plugin>
            <plugin>
              <artifactId>maven-surefire-pluginartifactId>
              <version>2.22.1version>
            plugin>
            <plugin>
              <artifactId>maven-war-pluginartifactId>
              <version>3.2.2version>
            plugin>
            <plugin>
              <artifactId>maven-install-pluginartifactId>
              <version>2.5.2version>
            plugin>
            <plugin>
              <artifactId>maven-deploy-pluginartifactId>
              <version>2.8.2version>
            plugin>
          plugins>
        pluginManagement>
      build>
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133

    测试类

    package com.test.service;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestCardInfoService {
    
        @Test
        public void test()
        {
            ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
    
            ICardInfoService cardInfoServiceProxy= applicationContext.getBean("cardInfoService",ICardInfoService.class);
    
            try {
                cardInfoServiceProxy.transfer(2,1,1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述

    项目结构

    在这里插入图片描述

  • 相关阅读:
    智能音箱中采用的数字音频功放
    sherpa + ncnn 离线语音识别
    实用的数据集成方式
    CDN工作原理
    websocket
    哈希表的介绍和内存布局 [数据结构][Java]
    如何给网站或者后端服务免费且快速的进行配置HTTPS,配置ssl证书
    文献阅读01_基于深度学习的个性化新闻推荐方法研究_20221114
    Vue3 + TypeSciprt+Vant 项目框架构建
    2022秋招阿里巴巴最新发布Java岗面试题,扛不住啊
  • 原文地址:https://blog.csdn.net/Rockandrollman/article/details/132665929