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 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); intA = 1/0; Accountdao.add (input, money); } Public voidSetaccountdao (Accountdao Accountdao) { This. Accountdao =Accountdao; }}
Service Business Layer
/**@author*/Publicinterface accountsevice { publicvoid transfer (String input,string out,double money); // Consumption }
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.spring01.AccountDaoimpl"> < Propertyname= "DataSource"ref= "DataSource"></ Property> </Bean> <BeanID= "Accountservice"class= "Com.sdf.spring01.AccountServiceImpl"> < Propertyname= "Accountdao"ref= "Accountdao"/> </Bean> <!--Configure transaction manager - <BeanID= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager"> < Propertyname= "DataSource"ref= "DataSource"/> </Bean> <!--Configure agents for the business layer - <BeanID= "Accountserviceproxy"class= "Org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <!--To Configure the target object - < Propertyname= "Target"ref= "Accountservice"/> <!--inject transaction Management class - < Propertyname= "TransactionManager"ref= "TransactionManager"/> <!--Inject Transaction Properties - < Propertyname= "Transactionattributes"> <Props> <!--Prop Mode * Propagation: Transaction propagation Behavior * Isolation: Isolation level of the transaction * ReadOnly: Read-only (not allowed to execute DEL uodate modify SQL statement) *-exception: This exception occurs rollback * +exceeption: This exception occurs without rollback - <propKey= "Transfer">Propagation_required</prop> </Props> </ Property> </Bean></Beans>
Test class
/** * @authorAt * Test transfer Information declarative transaction management based on TX/AOP * Create business Layer proxy to agent enhancements*/@RunWith (Springjunit4classrunner.class) @ContextConfiguration ("Classpath:applicationContext01.xml") Public classAccounttest {//@Resource (name= "Accountservice")//private Accountsevice Accountservice; /*** Inject proxy class*/@Resource (Name= "Accountserviceproxy") PrivateAccountsevice Accountservice; @Test Public voidtest01 () {Accountservice.transfer ("A", "B", 300d); } Public voidSetaccountservice (Accountsevice accountservice) { This. Accountservice =Accountservice; }}
Spring Transaction Management 3----Declarative Transaction management (1)