"Original link"
http://my.oschina.net/HuifengWang/blog/304188
Body
The transactions in spring are implemented by AOP, and when we write AOP interception ourselves, we encounter sequencing issues with spring's transactional AOP, such as the problem of dynamically switching data sources, and if the data source is switched on before the transaction, the data source switch is invalidated. So I used the order (sort) keyword.
Step 01:
We can define order by implementing the Org.springframework.core.Ordered interface in the @aspectj method, the smaller the order value, the more executed the first. For example the code is as follows:
1 /**2 * @authorHuifengwang3 * AOP-oriented tangent programming4 *5 */6 @Component7 @Aspect8 Public classAspectj4databaseImplementsordered{9 Ten //Interception of all service operations One@Pointcut ("Execution (* com.hc.shop.*.service.*.* (..))") A Public voidReadmethod () { -}//match all read operations - the@Before ("Readmethod ()") - Public voidOnlyreadpre () { - Datasourcecontextholder.setdatasourcetype (datasourcetype.mysql); -SYSTEM.OUT.PRINTLN ("Database switch MySQL"); + } -@After ("Readmethod ()") + Public voidOnlyreadpast () { A Datasourcecontextholder.setdatasourcetype (datasourcetype.oracle); atSYSTEM.OUT.PRINTLN ("Database switched back to Oracle"); - } - - @Override - Public intGetOrder () { - //TODO auto-generated Method Stub in return1; - } to}
Step 02:
The Order field is also configured where the transaction is configured, with the following code
1 <!-- -2<Transaction-manager = " TransactionManager " order=" 2 "/>
This enables the implementation of the AOP we write ourselves before the transaction is involved!
Go to spring in the sequencing of transactions and AOP