TransactionProxyFactoryBean用继承简化配置

TransactionProxyFactoryBean用继承简化配置
2008年04月24日 星期四 16:18
------------------------------------------------------------------------------------------------------------------------
利用继承简化配置
大部分情况下,每个事务代理的事务属性大同小异,
事务代理的实现类都是TransactionProxyFactoryBean,事务代理bean都必须注入事务管理器。
对于这种情况,Spring提供了bean与bean之间的继承,可以简化配置。将大部分的通用配置,
配置成事务模板,而实际的事务代理bean,则继承事务模板。
这种配置方式可以减少部分配置代码,下面是采用继承的配置文件:

------------------------------------------------------------------------------------------------------------------------

这种配置方式,相比前面直接采用TransactionProxyFactoryBean的事务代理配置方式,
可以大大减少配置文件的代码量。每个事务代理的配置都继承事务模板,无需重复指定事务代理的实现类,
无需重复指定事务传播属性——当然,如果新的事务代理有额外的事务属性,也可指定自己的事务属性,
此时,子bean的属性覆盖父bean的属性。当然每个事务代理bean都必须配置自己的目标bean,这不可避免。
上面的配置可看出,事务代理的配置依然是增量式的,每个事务代理都需要单独配置——虽然增量已经减少,
但每个事务代理都需要单独配置。
------------------------------------------------------------------------------------------------------------------------
CustomersDAO.java
------------------------------------------------------------------------------------------------------------------------
package db;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import test.IUserDAO;

public class CustomersDAO extends HibernateDaoSupport implements IUserDAO {
private static final Log log = LogFactory.getLog(CustomersDAO.class);

protected void initDao() {
// do nothing
}
private TransactionTemplate transactionTemplate;
public void setTransactionManager(
PlatformTransactionManager transactionManager) {
this.transactionTemplate = new TransactionTemplate(transactionManager);
}
public void insertUser(Customers transientInstance) {
getHibernateTemplate().saveOrUpdate(transientInstance);
}
}
------------------------------------------------------------------------------------------------------------------------

applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">


<bean
>
<property
value="com.mysql.jdbc.Driver">
</property>
<property
value="jdbc:mysql://localhost:3306/sampledb">
</property>
<property value="root"></property>
</bean>
<bean
>
<property >
<ref bean="localMysql" />
</property>
<property >
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property >
<list>
<value>db/Orders.hbm.xml</value>
<value>db/Customers.hbm.xml</value>
</list>
</property>
</bean>

<!-- 定义事务管理器,使用适用于Hibernte的事务管理器-->

<bean
>
<property >
<ref bean="localSessionFactory" />
</property>
</bean>


<!-- 配置事务模板,模板bean被设置成abstract bean,保证不会被初始化-->
<bean

lazy-init="true" abstract="true">
<!-- 为事务模板注入事务管理器-->
<property >
<ref bean="transactionManager" />
</property>
<!-- 设置事务属性-->
<property >
<props>
<prop key="inserUser">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 实际的事务代理bean-->
<bean parent="txBase">
<!-- 采用嵌套bean配置目标bean -->
<property >
<bean >
<property >
<ref local="localSessionFactory" />
</property>
</bean>
</property>
</bean>

</beans>


------------------------------------------------------------------------------------------------------------------------

Demo .java
------------------------------------------------------------------------------------------------------------------------
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import db.Customers;
import db.CustomersDAO;
public class Demo {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
test.IUserDAO dao = (test.IUserDAO) ctx.getBean("userDAOTarget");
Customers c = new Customers();
c.setId(3l);
c.setName("xxx");
dao.insertUser(c);
System.out.println("END");
}
}


------------------------------------------------------------------------------------------------------------------------


IUserDAO .java
------------------------------------------------------------------------------------------------------------------------
package test;

import db.Customers;

public interface IUserDAO {

public void insertUser(Customers c);

}

------------------------------------------------------------------------------------------------------------------------


本篇日志被作者设置为禁止发表新评论


©2008 Baidu



引文来源 TransactionProxyFactoryBean用继承简化配置_熊熊之家
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值