28spring_ Transaction Management-bank transfer business plus transaction control-declarative transaction management based on annotations

Source: Internet
Author: User

Give Applicationcontext.xml and Accountserviceimpl back a backup named Applicationcontext2.xml and Accountserviceimpl2.java

First step: Configure the transaction manager

Step Two: Configure note drivers

The above two steps are done in Applicationcontext2.xml.

The contents are as follows:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/sp Ring-beans-2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://w Ww.springframework.org/schema/aop/spring-aop-2.5.xsd Http://www.springframework.org/schema/conte                           XT Http://www.springframework.org/schema/context/spring-context-2.5.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx- 2.5.xsd "> <!--<bean class= "Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  -<!--Introducing peoperties Files -<!--<context:property-placeholder location= "classpath:db.properties"/> -<Beanclass= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">< Propertyname= "Locations"value= "Classpath:db.properties"/></Bean> <BeanID= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource">  < Propertyname= "Driverclass"value= "${driver}"/>  < Propertyname= "Jdbcurl"value= "${url}"></ Property> < Propertyname= "User"value= "${username}"></ Property>< Propertyname= "Password"value= "${password}"></ Property></Bean>     <BeanID= "JdbcTemplate"class= "Org.springframework.jdbc.core.JdbcTemplate">< Propertyname= "DataSource"ref= "DataSource"/></Bean><BeanID= "Accountdao"class= "Cn.itcast.dao.AccountDaoimpl">< Propertyname= "JdbcTemplate"ref= "JdbcTemplate"/></Bean><BeanID= "Accountservice"class= "CN.ITCAST.SERVICE.ACCOUNTSERVICEIMPL2"></Bean>


<!--Two configurations with annotations-- <!--The first step is to configure the transaction manager -<BeanID= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager">< Propertyname= "DataSource"ref= "DataSource"></ Property></Bean><!--Step Two: Configure annotation drivers, annotations for transaction management -<Tx:annotation-drivenTransaction-manager= "TransactionManager"/></Beans>

Step three: Complete in Accountserviceimpl2.java, add @transactional annotations on the Business Class (business method) that needs to manage transactions

Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.transaction.annotation.Transactional;ImportCn.itcast.dao.AccountDao;/** Implementation Class of Transfer interface*/ Public classAccountServiceimpl2ImplementsAccountservice {/*** This class is the implementation class for the transfer interface.   The question we have to consider is how to inject the DAO into it.   * Here @Autowired in annotated way. * Once in this way, before that Set/get way in Applicationcontext.xml * <bean id= "Accountservice" class= " Cn.itcast.service.AccountServiceimpl "> <!--<property name=" Accountdao "ref=" Accountdao "/>---&   Lt;/bean> inside the property name= "Accountdao" ref= "Accountdao"/> must be removed. *    *    */@AutowiredPrivateAccountdao Accountdao; @Transactional Public voidTransfer (String outaccount, String Inaccount,DoubleMoney) {Accountdao.addmoney (Inaccount, money);
After adding an exception here, it does not go down here, it will cause said Accountdao.addmoney (Inaccount, money) executed; Accountdao.reducemoney ( Outaccount, Money) did not execute. inta=1/0; Accountdao.reducemoney (Outaccount, money); }}

The configuration of the annotations is completed in three steps above.

Take a look at the result: Yes.

Actual work: In fact, the XML configuration method of the previous paper is more than the annotation configuration described in this article.

Description: There are three main frameworks for the integration of this series, which is not seen for the time being.

28spring_ Transaction Management-bank transfer business plus transaction control-declarative transaction management based on annotations

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.