用BeanNameAutoProxyCreator自动创建事务代理
2008年04月24日 星期四 16:28
------------------------------------------------------------------------------------------------------------------------
下面介绍一种优秀的事务代理配置策略:采用这种配置策略,
完全可以避免增量式配置,所有的事务代理由系统自动创建。容器中的目标bean自动消失,
避免需要使用嵌套bean来保证目标bean不可被访问。
这种配置方式依赖于Spring提供的bean后处理器,该后处理器用于为每个bean自动创建代理,
此处的代理不仅可以是事务代理,也可以是任意的代理,只需要有合适的拦截器即可。
这些是AOP框架的概念,笔者在此处不对AOP进行深入介绍。读者只需了解这种事务代理的配置方式即可。
下面是采用BeanNameAutoProxyCreator配置事务代理的配置文件:
------------------------------------------------------------------------------------------------------------------------
用BeanNameAutoProxyCreator自动创建事务代理
------------------------------------------------------------------------------------------------------------------------
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
>
<!-- 事务拦截器bean需要依赖注入一个事务管理器 -->
<property ref="transactionManager" />
<property >
<!-- 下面定义事务传播属性-->
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 定义BeanNameAutoProxyCreator,该bean是个bean后处理器,无需被引用,因此没有id属性
这个bean后处理器,根据事务拦截器为目标bean自动创建事务代理
<bean >
指定对满足哪些bean name的bean自动生成业务代理 -->
<bean
>
<property >
<!-- 下面是所有需要自动创建事务代理的bean-->
<list>
<value>userDAOTarget</value>
</list>
<!-- 此处可增加其他需要自动创建事务代理的bean-->
</property>
<!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
<property >
<list>
<value>transactionInterceptor</value>
<!-- 此处可增加其他新的Interceptor -->
</list>
</property>
</bean>
<!--定义DAO Bean ,由于BeanNameAutoProxyCreator自动生成事务代理-->
<bean >
<property >
<ref local="localSessionFactory" />
</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);
}
------------------------------------------------------------------------------------------------------------------------
下面介绍一种优秀的事务代理配置策略:采用这种配置策略,
完全可以避免增量式配置,所有的事务代理由系统自动创建。容器中的目标bean自动消失,
避免需要使用嵌套bean来保证目标bean不可被访问。
这种配置方式依赖于Spring提供的bean后处理器,该后处理器用于为每个bean自动创建代理,
此处的代理不仅可以是事务代理,也可以是任意的代理,只需要有合适的拦截器即可。
这些是AOP框架的概念,笔者在此处不对AOP进行深入介绍。读者只需了解这种事务代理的配置方式即可。
下面是采用BeanNameAutoProxyCreator配置事务代理的配置文件:
------------------------------------------------------------------------------------------------------------------------
用BeanNameAutoProxyCreator自动创建事务代理
------------------------------------------------------------------------------------------------------------------------
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
>
<!-- 事务拦截器bean需要依赖注入一个事务管理器 -->
<property ref="transactionManager" />
<property >
<!-- 下面定义事务传播属性-->
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 定义BeanNameAutoProxyCreator,该bean是个bean后处理器,无需被引用,因此没有id属性
这个bean后处理器,根据事务拦截器为目标bean自动创建事务代理
<bean >
指定对满足哪些bean name的bean自动生成业务代理 -->
<bean
>
<property >
<!-- 下面是所有需要自动创建事务代理的bean-->
<list>
<value>userDAOTarget</value>
</list>
<!-- 此处可增加其他需要自动创建事务代理的bean-->
</property>
<!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
<property >
<list>
<value>transactionInterceptor</value>
<!-- 此处可增加其他新的Interceptor -->
</list>
</property>
</bean>
<!--定义DAO Bean ,由于BeanNameAutoProxyCreator自动生成事务代理-->
<bean >
<property >
<ref local="localSessionFactory" />
</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
引文来源 用BeanNameAutoProxyCreator自动创建事务代理_熊熊之家