The way management transactions are configured in spring in addition to @transcational and using AOP, this article describes the @transcational approach, but it is recommended to use an AOP approach. Because if you have more than one transaction manager, you also need to indicate which transaction manager @transactional ("TransactionManager1") to use in the annotations.
First, spring must remember to load all the required beans
If you are using annotations, be sure to remember to scan the annotations, and the following example shows all annotations except @controller in all files under Scan xxx.xxx (including each level of subfolders).
<base-package= "xxx.xxx"> <type = "annotation" expression= "Org.springframework.stereotype.Controller"/ ></context:component-scan>
Second, while the SPRINGMVC only scans the controller
< context:component-scan base-package = "xxx.xxx" = "false" > < context:include-filter Span style= "color: #ff0000;" >type = "annotation" expression Span style= "color: #0000ff;" >= "Org.springframework.stereotype.Controller" /> </ context:component-scan >
Since it's a scan, the default scan is to get rid of those things. In summary, if you use context:include-filter (note that the upper two paragraphs are both include and exclude), besure not to forget the use-default-filters= "false".
Third, spring in addition to the data source must also have these:
<BeanID= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager"> < Propertyname= "DataSource"ref= "DataSource" /> </Bean> <!--Configuring transactions using annotation Annotations - <Tx:annotation-drivenTransaction-manager= "TransactionManager" />
The DataSource in TransactionManager is the commit and rollback that tells the transaction manager which database to invoke
The Tx:annotation-driven is in spring for all loaded (the one-step scan is the loading process), The bean with the @transcational annotation is given to the transaction manager written in Transaction-manager to manage the transaction. If you do not write which transaction manager you can write in the annotations, such as @transactional ("TransactionManager1")
Spring+springmvc the things that must be done to manage transactions in a @transcational way.