27spring_ Transaction Management-bank transfer Business Plus transaction control _ declarative transaction management based on TX.AOP

Source: Internet
Author: User

In the previous article, the bank transfer business did not use transactions, there will be problems, so this article on the previous article to modify the problem.

The transaction relies on AOP, and AOP needs to define the facets, which consist of advice (notification) and PointCut (tangent)!

Project Structure diagram:

This case and the case of the previous article is the same, we are only modifying the Applicationcontext.xml file, the other code will not change, so there is not much to explain.

Give the Applicationcontext.xml code directly. Take a good look at the notes inside.

<?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.AccountServiceimpl"></Bean><!--First Step: Configure the transaction manager -<!--the transaction manager mentioned before. The transaction manager has different classes for different persistence tiers, such as Spring's own springjdbc,hibernata and so on. This is Sping's own JDBC, so the class used here is Datasourcetransactionmanager -<BeanID= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager"><!--Why would you give DataSource to TransactionManager (transaction manager) because the transaction manager needs to get the connection from the data source to open the transaction, commit the transaction, roll back the transaction -  < Propertyname= "DataSource"ref= "DataSource"></ Property></Bean><!--Step Two: Configure notifications, -<!--requires notification (Advice), which is used to enhance the transaction. In fact, advice represents the front, surrounded by what to do, around the front, surrounded by what to do, not to open the transaction, commit the transaction? But what is the transaction done? It's not transactionmanager to do it.  -<Tx:adviceID= "Transactionadvice"Transaction-manager= "TransactionManager"><!--next to the configuration properties of the transaction, why do you want to match? In this case, you will know what method to apply and how to configure the transaction.                  These configuration information becomes the Transactiondefinition object transactiondefinition What do you remember? The previous paper mentioned four categories: 1. isolation_* with Isolation: Transaction ISOLATION LEVEL The propagation behavior of 2.propagation_* transactions, etc. four.  -<tx:attributes><!--Name Method Name isolation Isolation level propagation propagation behavior Timeout time-out read-only read-only (false means can change, true means cannot change) rollback-for Configure some exception types, These exceptions occur when the transaction is rolled back. No-rollback-for Configure some exception types that occur without rolling back the transaction. If you do not configure the above two, what happens by default will be rolled back, -  <Tx:methodname= "Transfer"Isolation= "DEFAULT"Propagation= "REQUIRED"Timeout= "-1"read-only= "false"/></tx:attributes></Tx:advice>




<!--the next third step is to configure the slice. -<Aop:config> <Aop:advisorAdvice-ref= "Transactionadvice"pointcut= "Execution (* cn.itcast.service.accountserviceimpl.* (..))"/></Aop:config></Beans>

Execution result: There is already control of the transaction. Either success or not together.

Attention! Attention!

Spring transaction management advice, is based on spring traditional AOP, so use aop:advisor configuration

27spring_ Transaction Management-bank transfer Business Plus transaction control _ declarative transaction management based on TX.AOP

Related Article

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.