七
30
24_传智播客Spring2.5视频教程_使用Spring配置文件实现事务管理
采用基于XML方式配置事务
<bean id=”txManager” class=”org.springframework.jdbc.datasourceTransactionManager”>
<property name=”dataSource” ref=”dataSource”>
</bean>
<aop:config>
<aop:pointcut id=”transactionPointcut” expression=”execution(* cn.itcast.service..*.*(..))” />
<aop:advisor advice-ref=”txAdvice” pointcut-ref=”transactionPointcut” />
</aop:config>
<tx:advice id=”txAdvice” transaction-manager=”txManager”>
<tx:advice id=”txAdvice” transaction-manager=”txManager”>
<tx:attributes>
<tx:method name=”get*” read-only=”true” propagation=”NOT_SUPPORTED” />
<tx:method name=”*” />
</tx:attributes>
</tx:advice>
———————–
上一讲说的是使用注解的方式来管理事务,这一讲讲解采用配置文件的方式管理事务
在切入点定义事务通知:<aop:advisor advice-ref=”txAdvice” pointcut-ref=”transactionPointcut” />
为所有查询方法指定只读且不需要事务管理<tx:method name=”get*” read-only=”true” propagation=”NOT_SUPPORTED” />
其他的,看代码吧。easy的;
上代码:
beans.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:tx=”http://www.springframework.org/schema/tx”
xmlns:aop=”http://www.springframework.org/schema/aop”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd”>
<context:property-placeholder location=”classpath:jdbc.properties”/>
<bean id=”dataSource” class=”org.apache.commons.dbcp.BasicDataSource” destroy-method=”close”>
<property name=”driverClassName” value=”${driverClassName}” />
<property name=”url” value=”${url}” />
<property name=”username” value=”${username}” />
<property name=”password” value=”${password}”/>
<!– 连接池启动时的初始值 –>
<property name=”initialSize” value=”${initialSize}” />
<!– 连接池的最大值 –>
<property name=”maxActive” value=”${maxActive}”/>
<!– 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 –>
<property name=”maxIdle” value=”${maxIdle}”/>
<!– 最小空闲值,当空闲的连接数少于阀值时,连接池就会去申请一些连接,以免洪峰来时来不及申请 –>
<property name=”minIdle” value=”${minIdle}” /></bean>
<bean id=”txManager” class=”org.springframework.jdbc.datasource.DataSourceTransactionManager”>
<property name=”dataSource” ref=”dataSource” />
</bean>
<!– 采用@Transactional注解方式使用事务 –>
<tx:annotation-driven transaction-manager=”txManager” />
<aop:config>
<aop:pointcut id=”transactionPointcut” expression=”execution(* cn.itcast.service..*.*(..))” />
<aop:advisor advice-ref=”txAdvice” pointcut-ref=”transactionPointcut” />
</aop:config>
<tx:advice id=”txAdvice” transaction-manager=”txManager”>
<tx:attributes>
<tx:method name=”get*” read-only=”true” propagation=”NOT_SUPPORTED” />
<tx:method name=”*” />
</tx:attributes>
</tx:advice><bean id=”personService” class=”cn.itcast.service.impl.PersonServiceBean” >
<property name=”dataSource” ref=”dataSource”></property>
</bean></beans>
PersonServiceBean.java
package cn.itcast.service.impl;import cn.itcast.bean.Person;
import cn.itcast.service.PersonService;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;/**
*
* @author kang.cunhua
*/public class PersonServiceBean implements PersonService {private JdbcTemplate jdbcTemplate;@Override
public void save(Person person) {
System.out.println(“俺是save(Person person)方法!”);
jdbcTemplate.update(“insert into person(name) values(?)”,
new Object[]{person.getName()},
new int[]{java.sql.Types.VARCHAR});}@Override
public void update(Person person) {
System.out.println(“俺是update(Person person)方法!”);
jdbcTemplate.update(“update person set name=? where id=?”,
new Object[]{person.getName(), person.getId()},
new int[]{java.sql.Types.VARCHAR, java.sql.Types.INTEGER});}@Override
public Person getPerson(Integer personid) {
System.out.println(“俺是getPerson(Integer personid)方法!”);
return (Person) jdbcTemplate.queryForObject(“select * from person where id=?”, new Object[]{personid},
new int[]{java.sql.Types.INTEGER},
new PersonRowMapper());
}@Override
public List<Person> getPersons() {
System.out.println(“俺是getPersons()方法!”);
return (List<Person>) jdbcTemplate.query(“select * from person”, new PersonRowMapper());
}@Override
public void delete(Integer personid) {
System.out.println(“俺是delete(Integer personid)方法!”);
jdbcTemplate.update(“delete from person where id=?”,
new Object[]{personid},
new int[]{java.sql.Types.INTEGER});
jdbcTemplate.update(“delete from personss where id=10″); // 此处故意写错表名,测试两个方法是否在同一个事务中执行
// throw new RuntimeException(“运行期例外”); // 抛出运行期例外,测试事务回滚!
}/**
* @param dataSource the dataSource to set
*/
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
}
PersonServiceBeanTest.java
package cn.itcast.service.impl;import cn.itcast.bean.Person;
import cn.itcast.service.PersonService;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/**
*
* @author kang.cunhua
*/
public class PersonServiceBeanTest {private static PersonService personService;public PersonServiceBeanTest() {
}@BeforeClass
public static void setUpClass() throws Exception {
ApplicationContext cxt = new ClassPathXmlApplicationContext(“beans.xml”);
personService = (PersonService) cxt.getBean(“personService”);
}@AfterClass
public static void tearDownClass() throws Exception {
}@Before
public void setUp() {
}/**
* Test of save method, of class PersonServiceBean.
*/
@Test
public void testSave() {
for (int i = 10; i < 15; i++) {
personService.save(new Person(“传智播客” + i));
}}/**
* Test of getPerson method, of class PersonServiceBean.
*/
@Test
public void testGetPerson() {
Person person = personService.getPerson(2);
System.out.println(” testGetPerson() :” + person.getName());
}/**
* Test of update method, of class PersonServiceBean.
*/
@Test
public void testUpdate() {
Person person = personService.getPerson(2);
person.setName(“张**”);
personService.update(person);}/**
* Test of getPersons method, of class PersonServiceBean.
*/
@Test
public void testGetPersons() {
for (Person person:personService.getPersons()) {
System.out.println(“testGetPersons():” + person.getName());
}
}/**
* Test of delete method, of class PersonServiceBean.
*/
@Test
public void testDelete() {
personService.delete(7);
}
}
注:其余代码和前两讲一样;
本文到目前为止还暂无回复