spring-spitter.xml
<?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"
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-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="username" value="spring" />
<property name="password" value="spring" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>
<bean id="spitterDao"
class="com.springinaction.spitter.transaction.code.JdbcSpitterDAO">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<bean id="spitterService"
class="com.springinaction.spitter.transaction.code.SpitterServiceImpl">
<property name="spitterDao" ref="spitterDao" />
<property name="transactionTemplate">
<bean class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager" />
</bean>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
JdbcSpitterDAO.java
package com.springinaction.spitter.transaction.code;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
public class JdbcSpitterDAO implements SpitterDAO
{
private SimpleJdbcTemplate jdbcTemplate;
public void setJdbcTemplate(SimpleJdbcTemplate jdbcTemplate)
{
this.jdbcTemplate = jdbcTemplate;
}
public void addSpitter(Spitter spitter)
{
jdbcTemplate.update(
"INSERT INTO SPITTER(ID,USERNAME,PASSWORD,FULLNAME,EMAIL) VALUES(?,?,?,?,?)",
spitter.getId(), spitter.getUsername(), spitter.getPassword(), spitter.getFullname(),
spitter.getEmail());
}
}
SpitterServiceImpl.java
package com.springinaction.spitter.transaction.code;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
public class SpitterServiceImpl implements SpitterService
{
private SpitterDAO spitterDao;
private TransactionTemplate transactionTemplate;
public void setSpitterDao(SpitterDAO spitterDao)
{
this.spitterDao = spitterDao;
}
public void setTransactionTemplate(TransactionTemplate transactionTemplate)
{
this.transactionTemplate = transactionTemplate;
}
public void saveSpitter(final Spitter spitter)
{
transactionTemplate.execute(new TransactionCallback<Void>()
{
public Void doInTransaction(TransactionStatus txStatus)
{
try
{
spitterDao.addSpitter(spitter);
}
catch (RuntimeException e)
{
txStatus.setRollbackOnly();
throw e;
}
return null;
}
});
}
}
Spitter.java
package com.springinaction.spitter.transaction.code;
public class Spitter
{
private int id;
private String username;
private String password;
private String fullname;
private String email;
/**
* @return Returns the id.
*/
public int getId()
{
return id;
}
/**
* @param id
* The id to set.
*/
public void setId(int id)
{
this.id = id;
}
/**
* @return Returns the username.
*/
public String getUsername()
{
return username;
}
/**
* @param username
* The username to set.
*/
public void setUsername(String username)
{
this.username = username;
}
/**
* @return Returns the password.
*/
public String getPassword()
{
return password;
}
/**
* @param password
* The password to set.
*/
public void setPassword(String password)
{
this.password = password;
}
/**
* @return Returns the fullname.
*/
public String getFullname()
{
return fullname;
}
/**
* @param fullname
* The fullname to set.
*/
public void setFullname(String fullname)
{
this.fullname = fullname;
}
/**
* @return Returns the email.
*/
public String getEmail()
{
return email;
}
/**
* @param email
* The email to set.
*/
public void setEmail(String email)
{
this.email = email;
}
@Override
public String toString()
{
return "Spitter [id=" + id + ", username=" + username + ", password=" + password
+ ", fullname=" + fullname + ", email=" + email + "]";
}
}
Test Demo
@Test
public void test1()
{
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"com/springinaction/spitter/transaction/code/spring-spitter.xml");
SpitterService service = (SpitterService)ctx.getBean("spitterService");
Spitter spitter = new Spitter();
spitter.setId(19);
spitter.setUsername("Jagger");
spitter.setPassword("123456");
spitter.setFullname("Jagger S Y CHEN");
spitter.setEmail("43970615@XX.com");
service.saveSpitter(spitter);
}
如果你想完全控制事务的边界,编码式事务是很好的。但是,你从程序清单可以看出它是侵入性的。你必须修改saveSpitter()实现来得到Spring的编程式事务的支持。