Transaction management in spring (5)

Transaction management in spring (5)


Related configuration methods for transactions

 

1. Steps of Spring Container Transaction Configuration

General configuration steps

1 Introduce transaction pom dependency information

2 Configure the DataSourceTransactionManager transaction manager in the container

3 The method of the DataSourceTransactionManager transaction manager uses the aop method to manage the commit and rollback of the business layer

4 DataSourceTransactionManager is aop's notification

5 Configure the pointcut expression that informs the connection pointcut

 

AOP

1 Pointcuts and tangents (pointcuts are expressions used to represent connection points, and tangents are collections of pointcuts)

2 connection point (proxy method)

3 Notification (requires modifications to the proxied method in the proxy class)

 

 

2. Configuration of SpringMVC transactions

1. General transaction configuration

The transaction is configured in Spring, and the interface required by the business layer (@Service) is injected into the SpringMVC control layer (@Controller), so that the DataSourceTransactionManager container transaction can work. If the implementation class is used, the automatic injection will report an error, because after the transaction is added The implementation of the business layer is replaced by the aop proxy class. The implementation class of the generated business layer is the type of the proxy class. At this time, the interface is injected. The aop proxy class and the proxy class implement the same interface.

 

2. Transactions in other cases:

(1) In a project, on the premise of not using the Spring container transaction, the beans of the business layer can be placed in the mvc container or in the spring container, and the class injection using imp is no different from using the inf interface injection.

 

(2) In a project, under the premise of using Spring container transactions, if the bean of the business layer is placed in the mvc container, the injection of classes and interfaces can be implemented, but the transaction does not work. If it is placed in the spring container, it must be Using interface injection, the transaction will work. This is because Spring's container transaction manages the transaction of the business layer. The premise must be in the spring container. If the business layer is in the mvc, the mvc container is a private container (the spring container is a public container) , will cause Spring's container transaction manager DataSourceTransactionManager to not have the right to proxy the business layer methods in the mvc container

 

(3)在一个项目中,在使用Spring容器事务前提下,如果将spring配置文件的全部配置信息都放到springmvc配置文件中,也就是说,将事务配置也放到springmvc容器中,也可以实现接口的注入,并且事务也可以起作用,但不建议这样做

 

(4)在一个项目中,在使用Spring容器事务前提下,如果将spring配置文件的全部配置信息都放到springmvc配置文件中,也就是说,将事务配置也放到springmvc容器中,让切入点表达式也包含控制层的方法,直接调用Mapper中的方法,也可以为控制层方法添加事务,但不建议这样做

 

(5)如果在web.xml中不配置<load-on-startup>1</load-on-startup>当使用是实现类注入时,启动项目时不会报错,但发送请求时会报错,因为配置之后,在项目启动时,mvc的servlet启动时就会创建,因此这种方式并不能解决实现类注入问题

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325939633&siteId=291194637