Pārlūkot izejas kodu

修改事务传播属性

zhang 2 gadi atpakaļ
vecāks
revīzija
81759e12aa

+ 12 - 0
mybatis_train/src/main/java/com/config/SpringConfig.java

@@ -0,0 +1,12 @@
1
+package com.config;
2
+
3
+import org.springframework.context.annotation.Bean;
4
+import org.springframework.context.annotation.Configuration;
5
+
6
+@Configuration
7
+public class SpringConfig {
8
+    @Bean
9
+    public String getUserName() {
10
+        return "zhang";
11
+    }
12
+}

+ 3 - 0
mybatis_train/src/main/java/com/service/impl/AccountServiceImpl.java

@@ -4,4 +4,7 @@ import org.springframework.stereotype.Service;
4 4
 
5 5
 @Service
6 6
 public class AccountServiceImpl {
7
+    public void sayHello() {
8
+        System.out.println("say");
9
+    }
7 10
 }

+ 0 - 23
mybatis_train/src/main/resources/SpringConfig.xml

@@ -1,23 +0,0 @@
1
-<?xml version="1.0" encoding="UTF-8"?>
2
-
3
-<beans xmlns="http://www.springframework.org/schema/beans"
4
-       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
-       xmlns:context="http://www.springframework.org/schema/context"
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/context
9
-       https://www.springframework.org/schema/context/spring-context.xsd">
10
-    <!--    <bean id="userService1" class="com.service.impl.UserServiceImpl">-->
11
-    <!--&lt;!&ndash;        <constructor-arg name="userDao" ref="UserDao"></constructor-arg>&ndash;&gt;-->
12
-    <!--    </bean>-->
13
-    <!--    加载外部 properties文件-->
14
-    <!--    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>-->
15
-    <!--    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">-->
16
-    <!--        <property name="driverClassName" value="${jdbc.driver}"></property>-->
17
-    <!--        <property name="url" value="${jdbc.url}"></property>-->
18
-    <!--        <property name="username" value="${jdbc.username}"></property>-->
19
-    <!--        <property name="password" value="${jdbc.password}"></property>-->
20
-    <!--    </bean>-->
21
-    <!--    组件扫描-->
22
-    <context:component-scan base-package="com"></context:component-scan>
23
-</beans>

+ 4 - 3
mybatis_train/src/test/java/Test2.java

@@ -7,11 +7,12 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
7 7
 public class Test2 {
8 8
     @Test
9 9
     public void testDemo() {
10
-//        ClassPathXmlApplicationContext classPathXmlApplicationContext =
11
-//                new ClassPathXmlApplicationContext("SpringConfig.xml");
12
-        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
10
+        AnnotationConfigApplicationContext annotationConfigApplicationContext
11
+                = new AnnotationConfigApplicationContext();
13 12
         annotationConfigApplicationContext.scan("com");
14 13
         annotationConfigApplicationContext.refresh();
15 14
         AccountServiceImpl bean = annotationConfigApplicationContext.getBean(AccountServiceImpl.class);
15
+        Object getUserName = annotationConfigApplicationContext.getBean("getUserName");
16
+        bean.sayHello();
16 17
     }
17 18
 }

+ 5 - 0
spring_aop/pom.xml

@@ -32,6 +32,11 @@
32 32
             <version>4.12</version>
33 33
             <scope>test</scope>
34 34
         </dependency>
35
+        <dependency>
36
+            <groupId>log4j</groupId>
37
+            <artifactId>log4j</artifactId>
38
+            <version>1.2.17</version>
39
+        </dependency>
35 40
         <dependency>
36 41
             <groupId>junit</groupId>
37 42
             <artifactId>junit</artifactId>

+ 1 - 1
spring_aop/src/main/java/com/TestTx.java

@@ -18,6 +18,6 @@ public class TestTx {
18 18
     @Test
19 19
     public void transformAnno() {
20 20
         // 交易测试
21
-        accountService.transfer(5,4,3.0);
21
+        accountService.transfer(16,17,3.0);
22 22
     }
23 23
 }

+ 11 - 4
spring_aop/src/main/java/com/transaction_anno/service/impl/AccountServiceImpl.java

@@ -20,20 +20,27 @@ public class AccountServiceImpl implements AccountService {
20 20
         accountEntity.setBalance(300.0);
21 21
         int i = accountDao.saveAccount(accountEntity);
22 22
     }
23
-    @Transactional(rollbackFor = Exception.class)
23
+    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
24 24
     @Override
25 25
     public boolean transfer(Integer fromId, Integer toId, Double balance) {
26 26
         // 转账操作
27
-        boolean b;
28 27
         AccountEntity fromEntity = new AccountEntity();
29 28
         AccountEntity toEntity = new AccountEntity();
30 29
         fromEntity.setId(fromId).setBalance(balance);
31 30
         toEntity.setId(toId).setBalance(balance);
32 31
         // 开启事务
33
-        accountDao.incomeAccount(toEntity,"to");
32
+        try {
33
+            toAccount(toEntity);
34
+        }catch (Exception e) {
35
+            System.out.println(e.getMessage());
36
+        }
37
+        int in = accountDao.incomeAccount(fromEntity, "in");
34 38
         int i = 1/0;
35
-        accountDao.incomeAccount(fromEntity,"in");
36 39
         // 提交事务
37 40
         return false;
38 41
     }
42
+    public int toAccount(AccountEntity toEntity) {
43
+        int to = accountDao.incomeAccount(toEntity, "to");
44
+        return to;
45
+    }
39 46
 }

+ 2 - 1
spring_aop/src/main/resources/context.xml

@@ -10,9 +10,10 @@
10 10
        https://www.springframework.org/schema/aop/spring-aop.xsd
11 11
        http://www.springframework.org/schema/context
12 12
        https://www.springframework.org/schema/context/spring-context.xsd
13
-
14 13
     ">
14
+
15 15
     <import resource="classpath:transaction_aop.xml"></import>
16
+
16 17
 <!--       1、配置目标对象 2、配置切面对象 3、配置织入告诉Spring 哪些方法(切点)需要增强 -->
17 18
     <bean id="target" class="com.aop.TargetImpl"></bean>
18 19
     <bean id="myaspect" class="com.aop.MyAspect">

+ 16 - 0
spring_aop/src/main/resources/log4j.properties

@@ -0,0 +1,16 @@
1
+log4j.rootCategory=INFO, stdout , R
2
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
3
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
4
+log4j.appender.stdout.layout.ConversionPattern=%d-[HL] %p %t %c - %m%n
5
+
6
+log4j.appender.R=org.apache.log4j.RollingFileAppender
7
+log4j.appender.R.File=${\u5E94\u7528\u540D.root}/WEB-INF/logs/xx.log
8
+log4j.appender.R.MaxFileSize=1024KB
9
+log4j.appender.R.MaxBackupIndex=10
10
+log4j.appender.R.layout=org.apache.log4j.PatternLayout
11
+log4j.appender.R.layout.ConversionPattern=%d-[HL] %p %t %c - %m%n
12
+
13
+log4j.logger.org.springframework=DEBUG
14
+log4j.logger.org.hibernate=DEBUG
15
+log4j.logger.org.hibernate.ps.PreparedStatementCache=WARN
16
+log4j.logger.com.lagooo.as=DEBUG