SSH事務配置全集

配置聲明式事務的方法如下:
<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager" ref="transactionManager"/>
		<property name="transactionAttributes">
			<props>
				<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="add*">PROPAGATION_REQUIRED</prop>
				<prop key="create*">PROPAGATION_REQUIRED</prop>
				<prop key="update*">PROPAGATION_REQUIRED</prop>
				<prop key="delete*">PROPAGATION_REQUIRED</prop>
				<prop key="save*">PROPAGATION_REQUIRED</prop>
				<prop key="regedit*">PROPAGATION_REQUIRED</prop> 
				<prop key="remove*">PROPAGATION_REQUIRED</prop>
				<prop key="do*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
	
	<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="interceptorNames">
			<list>
				<value>baseTransactionProxy</value>
			</list>
		</property>
		<property name="beanNames">
			<list>
				<value>*Service</value>
			</list>
		</property>
	</bean>

完整的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 id="helloService" class="com.tarena.service.HelloService">
		<property name="str" value="hello"></property>
	</bean>
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver">
		</property>
		<property name="url" value="jdbc:mysql://localhost:3306/cle">
		</property>
		<property name="username" value="root"></property>
		<property name="password" value="root1234"></property>
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation"
			value="classpath:hibernate.cfg.xml">
		</property>
		<property name="dataSource" ref="dataSource"></property>
		<!-- 
		<property name="mappingResources">
			<list>
				<value>com\tarena\entity\User.hbm.xml</value>
			</list>
		</property>
		 -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">none</prop>
			</props>
		</property>
	</bean>

	<bean id="hibernateTemplate"
		class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager" ref="transactionManager"/>
		<property name="transactionAttributes">
			<props>
				<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="add*">PROPAGATION_REQUIRED</prop>
				<prop key="create*">PROPAGATION_REQUIRED</prop>
				<prop key="update*">PROPAGATION_REQUIRED</prop>
				<prop key="delete*">PROPAGATION_REQUIRED</prop>
				<prop key="save*">PROPAGATION_REQUIRED</prop>
				<prop key="regedit*">PROPAGATION_REQUIRED</prop> 
				<prop key="remove*">PROPAGATION_REQUIRED</prop>
				<prop key="do*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
	
	<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="interceptorNames">
			<list>
				<value>baseTransactionProxy</value>
			</list>
		</property>
		<property name="beanNames">
			<list>
				<value>*Service</value>
			</list>
		</property>
	</bean>
	<bean id="userDao" class="com.tarena.dao.UserDaoImpl">
		<property name="hibernateTemplate" ref="hibernateTemplate"></property>
	</bean>
	
	<bean id="userService" class="com.tarena.service.UserServiceImpl">
		<property name="userDao" ref="userDao"></property>
	</bean>

	<bean id="loginAction" class="com.tarena.action.LoginAction">
		<property name="userService" ref="userService"></property>
	</bean>

</beans>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章