Spring Transaction Management 5-----Declarative Transaction Management (3)

Source: Internet
Author: User
Tags aop

Declarative transaction management based on annotations

Note Driver <tx:annotation-driven transaction-manager= "TransactionManager"/>is required in the configuration file, plus @ in the business layer class Transactional Annotations

This kind of transaction management method is very simple, but the note-loading business layer class, for late maintenance does not give the AspectJ Way of transaction management simple.

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 {    publicvoid transfer (String input,string out,double money); // Consumption }

Service Business Layer Implementation class

/** * @authorAt * Programmatic Transaction Management * * Propagation Transaction propagation behavior * Isolation TRANSACTION ISOLATION LEVEL * ReadOnly         Read-only * rollbackfor This exception occurs rollback * Norollbackfor The exception does not roll back * norollbackforclassname Occurrence of the specified exception does not roll back*/@Transactional (Propagation=propagation.required,isolation=isolation.default,readonly=false) 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

<?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.spring03.AccountDaoimpl">        < Propertyname= "DataSource"ref= "DataSource"></ Property>    </Bean>    <BeanID= "Accountservice"class= "Com.sdf.spring03.AccountServiceImpl">        < Propertyname= "Accountdao"ref= "Accountdao"/>    </Bean>    <!--Configure transaction manager -    <BeanID= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager">        < Propertyname= "DataSource"ref= "DataSource"/>    </Bean>        <!--Opening annotation Transactions -    <Tx:annotation-drivenTransaction-manager= "TransactionManager"/></Beans>

Test class

/** * @authorat * Test transfer information declarative transaction management based on annotation management*/@RunWith (Springjunit4classrunner.class) @ContextConfiguration ("Classpath:applicationContext03.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 5-----Declarative Transaction Management (3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.