spring datasource 配置及事务管理

首先配置 datasource

 我的数据源配置 来自于tomcat下 conf/context.xml

   在这里你可以配置数据源信息 :

context.xml 配置

 

<Resource name="jdbc/testDB" auth="Container" type="javax.sql.DataSource"
	   maxActive="100" maxIdle="30" maxWait="10000"
	   username="root" password="" driverClassName="com.mysql.jdbc.Driver"
	   url="jdbc:mysql://192.168.1.246:3306/TestDB?useUnicode=true&amp;characterEncoding=utf-8"/>

 

dbonfig.properties文件 配置

jndiName=testDB

 

 

下面是applicationContextAction.xml配置文件

 

<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>
					WEB-INF/classes/config/dbonfig.properties
				</value>
			</list>
		</property>
	</bean>
<!--jndiName来自 配置文件-->
	<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName">
			<value>java:/comp/env/jdbc/${jndiName}</value>
		</property>
	</bean>

 事务配置

 

<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 将所有具有@Transactional注解的Bean自动配置为声明式事务支持 -->
	<tx:annotation-driven transaction-manager="transactionManager" />

 

具体的DAO 及Service的配置  :设置注入

 

  <bean id="sampleDAO" class="com.test.dao.SampleDAOImplBG"
      autowire="byName" />
   <bean id="sampleDAOProxy"
      class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      <property name="proxyInterfaces">
         <list>
            <value>com.test.dao.SampleDAO</value>
         </list>
      </property>
      <property name="target" ref="sampleDAO" />
      <property name="transactionManager" ref="transactionManager" />
      <property name="transactionAttributes">
         <props>
            <prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
         </props>
      </property>
   </bean>
   <bean id="sampleService"
      class="com.test.business.SampleServiceImpl">
      <property name="sampleDAO" ref="sampleDAOProxy" />
   </bean>

 

 struts2 Action的配置:构造器注入

 

<bean id="SampletAction" class="com.test.web.SampleAction"
		scope="prototype">
	<constructor-arg ref="sampleService" />
</bean>

 其他配置 可以参考下面超链

 

http://topinking.iteye.com/blog/248839

 

其实spring的 声明式事务管理配置,有5种方式 :下面是更为清晰的

 

根据代理机制的不同,总结了五种Spring事务的配置方式,配置文件如下

 

http://jiake.iteye.com/blog/599418

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值