spring-Declarative Transaction Management

Source: Internet
Author: User

First, create a spring project
Project Name: spring101311
Ii. adding a jar package to the project
1. Create a Lib directory in your project
/lib
2. Add spring support under the Lib directory
Com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
Com.springsource.org.aopalliance-1.0.0.jar
Com.springsource.org.aspectj.weaver-1.6.8.release.jar
Commons-logging.jar
Junit-4.10.jar
Log4j.jar
Mysql-connector-java-5.1.18-bin.jar
Spring-aop-3.2.0.release.jar
Spring-aspects-3.2.0.release.jar
Spring-beans-3.2.0.release.jar
Spring-context-3.2.0.release.jar
Spring-core-3.2.0.release.jar
Spring-expression-3.2.0.release.jar
Spring-jdbc-3.2.0.release.jar
Spring-tx-3.2.0.release.jar
Iii. adding a profile and properties file to a project
1. Create the Conf directory in your project
2. Add a property file under the Conf directory
Property file name: Jdbc.properties
Properties File Contents:
Jdbc.url=jdbc:mysql://localhost:3306/spring
Jdbc.driver=com.mysql.jdbc.driver
Jdbc.username=root
Jdbc.password=root
2. Add the Spring core configuration file under the Conf directory
Configuration file name: Applicationcontext.xml
Configuration file Contents:
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:context= "Http://www.springframework.org/schema/context"
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-context.xsd " >

<!--1. Load Properties File---
<context:property-placeholder location= "Classpath:jdbc.properties"/>

<!--2. Configure the database connection pool--
<bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name= "Jdbcurl" value= "${jdbc.url}" ></property>
<property name= "Driverclass" value= "${jdbc.driver}" ></property>
<property name= "user" value= "${jdbc.username}" ></property>
<property name= "Password" value= "${jdbc.password}" ></property>
</bean>
</beans>
Four, implement bean design
1. Create a package for the entity bean in the SRC directory
Package Name: Cn.jbit.spring101310.domain
2. Create an entity bean under a package
public class Account {
Private Integer ID;
private String name;
Private Double money;
Omit Get and set
}
V. Design the DAO layer
1. Create a DAO layer package in the SRC directory
Package Name: Cn.jbit.spring101310.dao
2. Create the DAO layer's interface and implementation class under the package
1) interface Design
Interface Name: Iaccountdao.java
Interface content:
Public interface Iaccountdao {
/**
* Turn out
*/
public void Outmoney (account outaccount);
/**
* Transfer to
*/
public void Inmoney (account inaccount);
}
2) interface Implementation class design
Implementation class Name: Accountdaoimpl.java
Implementing the class content:
public class Accountdaoimpl extends Jdbcdaosupport implements Iaccountdao {
/**
* Transfer to
*/
@Override
public void Inmoney (account Inaccount) {
String sql = "Update account Set money = Money +?" WHERE id =? ";
This.getjdbctemplate (). Update (Sql,inaccount.getmoney (), Inaccount.getid ());
}

/**
* Turn out
*/
@Override
public void Outmoney (account Outaccount) {
String sql = "Update account Set money = money-?" WHERE id =? ";
This.getjdbctemplate (). Update (Sql,outaccount.getmoney (), Outaccount.getid ());
}
}
Vi. Configuring DAO in the core configuration file
<!--3. Configuring DAO--
<bean id= "Accountdao" class= "Cn.jbit.spring101310.dao.AccountDaoImpl" >
<property name= "DataSource" ref= "DataSource" ></property>
</bean>

Vii. Configuring transaction-related information in the core configuration file
<!--4. Configure Transaction Manager-
<bean id= "TransactionManager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "DataSource" ref= "DataSource" ></property>
</bean>
Viii. Business Layer Design
1. Create a package for the business layer in the SRC directory
Package Name: Cn.jbit.spring101310.service
2. Create the interface and implementation class for the business layer under the package
1) interface Design
Interface Name: Accountservice.java
Interface content:
Public interface Accountservice {
public void Transfer (account Outaccount,account Inaccount);
}
2) interface Implementation class design
Implementation class Name: Accountserviceimpl.java
Implementing the class content:
public class Accountserviceimpl implements Accountservice {

Private Iaccountdao Accountdao;
@Override
public void Transfer (final account Outaccount, final account Inaccount) {
Turn out
Accountdao.outmoney (Outaccount);
int a = 1/0;
Into
Accountdao.inmoney (Inaccount);
}
Omit Get and set
}
IX. Configuring the business layer in the core configuration file
<!--5. Configure Service--
<bean id= "Accountservice" class= "Cn.jbit.spring101310.service.AccountServiceImpl" >
<property name= "Accountdao" ref= "Accountdao" ></property>
</bean>
X. Configuring the agent in the core configuration file
<!--6. Create an agent--
<bean id= "Accountserviceproxy" class= "Org.springframework.transaction.interceptor.TransactionProxyFactoryBean ">
<!--
Referencing transaction manager
-
<property name= "TransactionManager" ref= "TransactionManager" ></property>
<!--
Which class to create the proxy for
-
<property name= "target" ref= "Accountservice" ></property>
<property name= "Transactionattributes" >
<props>
<!--
*: All methods
Propagation_required: Default transaction propagation behavior
-
<prop key= "*" >PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
XI. Testing
1. Create the test directory on the project
/test
2. Creating a test package under the test directory
Package Name: Cn.jbit.spring101310.service
3. Create a test class under a test package
Test class Name: Accountservicetest.java
Test the contents of the class:
public class Accountservicetest {
@Test
public void Testtranser () {
ApplicationContext context = new Classpathxmlapplicationcontext ("Classpath:applicationContext.xml");
Accountservice Accountservice = (accountservice) context.getbean ("Accountservice");
Account Outaccount = new account ();
Outaccount.setid (1);
Outaccount.setmoney (500D);
Account Inaccount = new account ();
Inaccount.setid (2);
Inaccount.setmoney (500D);
Accountservice.transfer (Outaccount, Inaccount);
}
}

This article is from the "Yan" blog, please be sure to keep this source http://suyanzhu.blog.51cto.com/8050189/1563387

spring-Declarative Transaction Management

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.