Home > Back-end >  About the Spring transaction - driven/> <tx: an annotation of understanding
About the Spring transaction - driven/> <tx: an annotation of understanding

Time:11-23

About the Spring transaction & lt; Tx: annotation - driven/& gt; The understanding of the

At the time of use for SpringMvc, we often see the annotation in the configuration file - driven such annotations, its meaning is to support annotations, generally based on the prefix tx, such as the MVC can also very straightforward to understand the role of respectively, & lt; Tx: annotation - driven/& gt; Is to support transaction annotations (@ Transactional), & lt; MVC: annotation - driven> Is to support the MVC annotations, namely make the Controller can be used in the various annotations of MVC,
First of all, & lt; Tx: annotation - driven/& gt; There will be an attribute is used to specify which transaction manager, such as: & lt; Tx: annotation - driven transaction - manager="transactionManager"/& gt; , and then the transaction manager transactionManager can refer to the dataSource (if we use the JPA or Hibernate, also need to specify a entityManagerFactory), dataSouce must be directly to the database,
Such references one by one down, so we use the @ Transactionl annotations can control the transaction is easy to understand, in addition to mention is spring is using aop asm Java bytecode operation ways to transaction management before and after the implementation of the method,
Speaking of which, has built up to & lt; Tx: annotation - driven/& gt; Simple to understand, that if we can all be spring in the program management can use @ Transactional annotation on the class, can use @ Transactional annotation on the Service that is for sure, that there are always some people also want to know whether used in the Controller? The answer is clearly "not necessarily" configuration (and time) of the following do explain:
In the spring - framework - reference. There are such a PDF document:
Means: & lt; Tx: annoation - driven/& gt; Can only search up and down and its application in the same file defined in the bean on @ Transactional annotation above, if you put it on the Dispatcher application context, it only check on the Controller (Controller) @ Transactional annotation, rather than your @ Transactional services annotations,
So, to be sure we can use transaction annotations on the Controller, but we don't recommend to do so (I never do so), it is merely to say that the spring to & lt; Tx: annotation - driven/& gt; The use of the


General configuration method is to let the Spring management besides Controller annotation annotations, and allow for SpringMVC pure management Controller annotations,
Spring has a configuration file that is to say, it is configured to scan the Controller bean, there is a configuration file for SpringMVC, only scanning Controller inside,
That has two context, namely the Spring context and context for SpringMVC, both of them separately with different pile of bean,
This time you are in the Spring configuration file added a tx: annoation - driven, is actually tell Spring, you manage these beans contain transaction support, there might be
Then in the Spring a beans and add a note to the scope of control @ transactional, the bean is used to help the Spring identification is required for transaction management,
Also add a tx: you are in the Spring configuration file annoation - driven, is don't care for SpringMVC, he only CARES about what did you give him the configuration and what is his annotations management on the bean

Reference: https://blog.csdn.net/catoop/article/details/50067785
  • Related