spring中hibernate及事务配置

hibernate的sessionFactory配置:

<!-- hibernate sessionFactory配置 -->
<bean id="sessionFactory"
	class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<property name="dataSource" ref="dataSource" />
	<property name="mappingResources">
		<list>
			<value>xxx.hbm.xml</value>
		</list>
	</property>
	<property name="hibernateProperties">
		<props>
			<prop key="hibernate.dialect">${hibernate.dialect}</prop>
			<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
			<prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
			<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
			<prop key="hibernate.connection.characterEncoding">${hibernate.connection.characterEncoding}</prop>
			<prop key="hibernate.connection.autocommit">${hibernate.connection.autocommit}</prop>
			<prop key="hibernate.connection.release_mode">${hibernate.connection.release_mode}</prop>
			<prop key="hibernate.autoReconnect">${hibernate.autoReconnect}</prop>
			<prop key="hibernate.cglib.use_reflection_optimizer">${hibernate.cglib.use_reflection_optimizer}</prop>
		</props>
	</property>
</bean>

定义事务管理器及事务拦截器:

<!-- 定义事务管理器  -->
<bean id="transactionManager"
	class="org.springframework.orm.hibernate3.HibernateTransactionManager">
	<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--  配置事务拦截器 -->
<bean id="transactionInterceptor"
	class="org.springframework.transaction.interceptor.TransactionInterceptor">
	<!--  事务拦截器bean需要依赖注入一个事务管理器 -->
	<property name="transactionManager" ref="transactionManager" />
	<property name="transactionAttributes">
		<!--  下面定义事务传播属性 -->
		<props>
				<prop key="save*">PROPAGATION_REQUIRED</prop>
				<prop key="create*">PROPAGATION_REQUIRED</prop>
				<prop key="createDomain">PROPAGATION_REQUIRES_NEW</prop>
				<prop key="update*">PROPAGATION_REQUIRED</prop>
				<prop key="Update*">PROPAGATION_REQUIRED</prop>
				<prop key="modify*">PROPAGATION_REQUIRED</prop>
				<prop key="cancel*">PROPAGATION_REQUIRED</prop>
				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="search*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="Create*">PROPAGATION_REQUIRED</prop>
				<prop key="sub*">PROPAGATION_REQUIRED</prop>
				<prop key="finance*">PROPAGATION_REQUIRED</prop>
				<prop key="start*">PROPAGATION_REQUIRED</prop>
				<prop key="stop*">PROPAGATION_REQUIRED</prop>
				<prop key="delete*">PROPAGATION_REQUIRED</prop>
				<prop key="transfer">PROPAGATION_REQUIRED</prop>
				<prop key="subCash*">PROPAGATION_REQUIRED</prop>
				<prop key="send*">PROPAGATION_REQUIRED</prop>
				<prop key="xPF*">PROPAGATION_REQUIRED</prop>
				<prop key="pay*">PROPAGATION_REQUIRED</prop>
				<prop key="deal*">PROPAGATION_REQUIRED</prop>
				<prop key="transfer*">PROPAGATION_REQUIRED</prop>
				<prop key="defaultPhone">PROPAGATION_REQUIRED</prop>
				<prop key="open*">PROPAGATION_REQUIRED</prop>
				<prop key="close*">PROPAGATION_REQUIRED</prop>
				<prop key="viewUploadFile">PROPAGATION_REQUIRED</prop>
				<prop key="page*">PROPAGATION_REQUIRED</prop>
		</props>
	</property>
</bean>

定义需要处理的bean即dao:

<!--
	定义BeanNameAutoProxyCreator,该bean是个bean后处理器,无需被引用,因此没有id属性
	这个bean后处理器,根据事务拦截器为目标bean自动创建事务代理 指定对满足哪些bean name的bean自动生成业务代理
-->
<bean
	class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
	<!--  下面定义BeanNameAutoProxyCreator所需的事务拦截器 -->
	<property name="interceptorNames">
		<list>
			<value>transactionInterceptor</value>
		</list>
	</property>
	<!--  下面是所有需要自动创建事务代理的bean -->
	<property name="beanNames">
		<list>
			<value></value>
		</list>
	</property>
</bean>

相关推荐