Declarative transaction Management ASPECTJ-based XML configuration
Configure the notification (enhancement) by TransactionManager the transaction manager, and then configure the pointcuts and facets in detail in the applicationcontext.xml configuration file
This transaction management has no code modifications to the business layer and simplifies the setup in the XML configuration file, which is often used in real development.
DAO layer
/** * @authorAt * Transfer DAO layer*/ Public InterfaceAccountdao {/*** Transfer Money *@paramOuter *@param Money*/ Public voidRemove (String outer,double money); /*** Transfer Money *@paramInput *@param Money*/ Public voidAdd (String input,double money);}
DAO layer Implementation Class
/*** Transfer DAO layer Implementation*/ Public classAccountdaoimplextendsJdbcdaosupportImplementsaccountdao{/*** Transfer Money *@paramOuter *@param Money*/@Override Public voidRemove (string outer, Double money) {String SQL= "Update account Set money = money-?" WHERE name =? "; This. Getjdbctemplate (). Update (SQL, money,outer); } /*** Transfer Money *@paramInput *@param Money*/@Override Public voidAdd (string input, Double money) {String SQL= "Update account Set money = Money +?" WHERE name =? "; This. Getjdbctemplate (). Update (SQL, money,input); } }
Service Business Layer
/**@author*/Publicinterface Accountsevice { Public void Transfer (String input,string out,double money); // Consumption }
Service Business Layer Implementation class
/** * @authorAt * Programmatic transaction Management*/ Public classAccountserviceimplImplementsAccountsevice {@Resource (name= "Accountdao") PrivateAccountdao Accountdao; @Override Public voidTransferFinalString input,FinalString out,FinalDouble Money) {Accountdao.remove (out, money);//int a = 1/0;accountdao.add (input, money); } Public voidSetaccountdao (Accountdao Accountdao) { This. Accountdao =Accountdao; }}
Applicationcontext.xml configuration file
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans . xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-con Text.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd "> <!--introducing an external properties file - <Context:property-placeholder Location= "Classpath:jdbc.properties"/> <!--Configuring the C3P0 connection pool - <BeanID= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource"> < Propertyname= "Driverclass"value= "${jdbc.driverclass}"/> < Propertyname= "Jdbcurl"value= "${jdbc.url}"/> < Propertyname= "User"value= "${jdbc.username}"/> < Propertyname= "Password"value= "${jdbc.password}"/> </Bean> <!--DAO layers and classes of business - <BeanID= "Accountdao"class= "Com.sdf.spring02.AccountDaoimpl"> < Propertyname= "DataSource"ref= "DataSource"></ Property> </Bean> <BeanID= "Accountservice"class= "Com.sdf.spring02.AccountServiceImpl"> < Propertyname= "Accountdao"ref= "Accountdao"/> </Bean> <!--Configure transaction manager - <BeanID= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager"> < Propertyname= "DataSource"ref= "DataSource"/> </Bean> <!--Configuring Notifications for transactions: (Enhanced transaction) - <Tx:adviceID= "Txadvice"Transaction-manager= "TransactionManager"> <tx:attributes> <!--* Propagation Transaction propagation behavior * Isolation TRANSACTION ISOLATION LEVEL * Read-only Read-only * Rollback-for Exception Rollback * No-rollback-for exception does not roll back * Timeout Timeout information - <Tx:methodname= "Transfer"Propagation= "REQUIRED"/> </tx:attributes> </Tx:advice> <!--Configure Facets - <Aop:config> <!--Configure Pointcuts - <Aop:pointcutexpression= "Execution (* com.sdf.spring02.*.* (..))"ID= "POINTCUT1"/> <!--Configure Facets - <Aop:advisorAdvice-ref= "Txadvice"Pointcut-ref= "POINTCUT1"/> </Aop:config></Beans>
Test
/** * @authorAt * Test transfer Information declarative transaction management ASPECTJ-based XML configuration*/@RunWith (Springjunit4classrunner.class) @ContextConfiguration ("Classpath:applicationContext02.xml") Public classaccounttest {@Resource (name= "Accountservice") PrivateAccountsevice Accountservice; @Test Public voidtest01 () {Accountservice.transfer ("A", "B", 300d); } Public voidSetaccountservice (Accountsevice accountservice) { This. Accountservice =Accountservice; }}
Spring Transaction Management 4-----Declarative Transaction Management (2)