spring+hibernate事务处理

本文介绍了Spring框架下整合Hibernate进行事务管理的配置方法,详细展示了配置文件applicationContext.xml的内容,并解释了如何通过设置proxyTargetClass解决动态代理时可能出现的类型转换错误。
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>
 注意 事务中的一段:
            <
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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值