论坛首页 入门技术论坛

spring声明式事务配置方法(四):BeanNameAutoProxyCreator注入Bean

浏览 2067 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (3)
作者 正文
   发表时间:2009-02-06   最后修改:2009-08-21
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<bean id="testAction" class="test.action.Stuts2ActionTest">
		<property name="service" ref="templatesService"></property>
	</bean>

	<bean id="templatesService" class="test.service.impl.TaoTemplatesServiceImpl">
		<property name="dao" ref="templatesDAO" />
	</bean>

	<bean id="templatesDAO" class="test.dao.impl.TaoTemplatesDAOImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<!--定义数据源-->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<!--   定义数据库驱动-->
		<property name="driverClassName">
			<value>oracle.jdbc.driver.OracleDriver
			</value>
		</property>
		<!--   定义数据库url-->
		<property name="url">
			<value>jdbc:oracle:thin:@192.168.1.96:1521:yxdb
			</value>
		</property>
		<!--   定义数据库用户名-->
		<property name="username">
			<value>yxuser</value>
		</property>
		<!--   定义数据库密码-->
		<property name="password">
			<value>yxuser</value>
		</property>
	</bean>

	<!--定义一个hibernate的SessionFactory-->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<!--   定义SessionFactory必须注入DataSource-->
		<property name="dataSource">
			<ref local="dataSource" />
		</property>
		<property name="mappingResources">
			<list>
				<!--以下用来列出所有的PO映射文件-->
				<value>test/mapping/Tao_Templates.hbm.xml
				</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.Oracle10gDialect
                </prop>
				<prop key="hibernate.show_sql">true</prop>
				<!--
					此处用来定义hibernate的SessionFactory的属性:
					不同数据库连接,启动时选择create,update,create-drop
				-->
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
	</bean>


	<!--   定义事务管理器,使用适用于Hibernte的事务管理器-->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<!--
			HibernateTransactionManager bean需要依赖注入一个SessionFactory bean的引用
		-->
		<property name="sessionFactory">
			<ref local="sessionFactory" />
		</property>
	</bean>

	<!--   配置事务拦截器-->
	<bean id="transactionInterceptor"
		class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<!--   事务拦截器bean需要依赖注入一个事务管理器 -->
		<property name="transactionManager" ref="transactionManager" />
		<property name="transactionAttributes">
			<!--   下面定义事务传播属性-->
			<props>
				<!--   所有以add开头的方法,采用required的事务策略,并且只读-->
				<prop key="add*">PROPAGATION_REQUIRED,readOnly
				</prop>
				<!--   所有以mod开头的方法,采用required的事务策略,并且只读-->
				<prop key="mod*">PROPAGATION_REQUIRED,readOnly
				</prop>
				<!--   所有以del开头的方法,采用required的事务策略,并且只读-->
				<prop key="del*">PROPAGATION_REQUIRED,readOnly
				</prop>
				<!--   其他方法,采用required的事务策略 -->
				<prop key="*">readOnly</prop>
			</props>
		</property>
	</bean>

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

跳转论坛:
Global site tag (gtag.js) - Google Analytics