Spring包含Hibernate事务的配置文件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"
default-autowire ="byName" default-lazy-init ="true" >
< bean id ="dataSource"
class ="org.apache.commons.dbcp.BasicDataSource"
destroy-method ="close" >
< property name ="driverClassName" >
< value > org.gjt.mm.mysql.Driver </ value >
</ property >
< property name ="url" >
< value > jdbc:mysql://localhost:3306/rencaiwang </ value >
</ property >
< property name ="username" >
< value > rencaiwang </ value >
</ property >
< property name ="password" >
< value > rencaiwang </ value >
</ property >
</ bean >
< bean id ="sessionFactory"
class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
< property name ="dataSource" >
< ref local ="dataSource" />
</ property >
< property name ="mappingResources" >
< value > test/customer.hbm.xml </ value >
</ property >
< property name ="hibernateProperties" >
< props >
< prop key ="hibernate.dialect" >
org.hibernate.dialect.MySQLDialect
</ prop >
</ props >
</ property >
</ bean >
<!-- Transaction配置开始 -->
< bean id ="transactionManager"
class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
< property name ="sessionFactory" >
< ref bean ="sessionFactory" />
</ property >
</ bean >
<!-- Transaction配置结束 -->
< bean id ="custManageServiceTarget" class ="test.CustomerDao" >
< property name ="sessionFactory" >
< ref bean ="sessionFactory" />
</ property >
</ bean >
< bean id ="customerDao"
class ="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
< property name ="transactionManager" >
< ref bean ="transactionManager" />
</ property >
< property name ="target" >
< ref bean ="custManageServiceTarget" />
</ property >
< property name ="proxyTargetClass" >
< value > true </ value >
</ property >
< property name ="transactionAttributes" >
< props >
< prop key ="get*" > PROPAGATION_REQUIRED,readOnly </ prop >
< prop key ="*" > PROPAGATION_REQUIRED </ prop >
</ props >
</ property >
</ bean >
<!-- Action及其Service被配置在applicationContext-XXX中 -->
<!-- 示例Customer action and dao -->
< bean id ="customerAction" scope ="prototype"
class ="test.CustomerAction" >
< property name ="custDao" >
< ref bean ="customerDao" />
</ property >
</ bean >
</ beans >
<? 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"
default-autowire ="byName" default-lazy-init ="true" >
< bean id ="dataSource"
class ="org.apache.commons.dbcp.BasicDataSource"
destroy-method ="close" >
< property name ="driverClassName" >
< value > org.gjt.mm.mysql.Driver </ value >
</ property >
< property name ="url" >
< value > jdbc:mysql://localhost:3306/rencaiwang </ value >
</ property >
< property name ="username" >
< value > rencaiwang </ value >
</ property >
< property name ="password" >
< value > rencaiwang </ value >
</ property >
</ bean >
< bean id ="sessionFactory"
class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
< property name ="dataSource" >
< ref local ="dataSource" />
</ property >
< property name ="mappingResources" >
< value > test/customer.hbm.xml </ value >
</ property >
< property name ="hibernateProperties" >
< props >
< prop key ="hibernate.dialect" >
org.hibernate.dialect.MySQLDialect
</ prop >
</ props >
</ property >
</ bean >
<!-- Transaction配置开始 -->
< bean id ="transactionManager"
class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
< property name ="sessionFactory" >
< ref bean ="sessionFactory" />
</ property >
</ bean >
<!-- Transaction配置结束 -->
< bean id ="custManageServiceTarget" class ="test.CustomerDao" >
< property name ="sessionFactory" >
< ref bean ="sessionFactory" />
</ property >
</ bean >
< bean id ="customerDao"
class ="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
< property name ="transactionManager" >
< ref bean ="transactionManager" />
</ property >
< property name ="target" >
< ref bean ="custManageServiceTarget" />
</ property >
< property name ="proxyTargetClass" >
< value > true </ value >
</ property >
< property name ="transactionAttributes" >
< props >
< prop key ="get*" > PROPAGATION_REQUIRED,readOnly </ prop >
< prop key ="*" > PROPAGATION_REQUIRED </ prop >
</ props >
</ property >
</ bean >
<!-- Action及其Service被配置在applicationContext-XXX中 -->
<!-- 示例Customer action and dao -->
< bean id ="customerAction" scope ="prototype"
class ="test.CustomerAction" >
< property name ="custDao" >
< ref bean ="customerDao" />
</ property >
</ bean >
</ beans >
< property name ="proxyTargetClass" >
< value > true </ value >
</ property >
如果没有这一段,动态注入的时候就会出现conversion错误。类似如下:
org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy12 to required type
[com.brilliance.struts.service.impl.InvestServiceImpl] for property 'fundService'; nested exception is
java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy1] to required type
[com.brilliance.struts.service.impl.InvestServiceImpl] for property 'fundService': no matching editors or conversion strategy
found