spring 事务配置

自己写的一个关于事务的配置例子,很简单~~!主要是spring的事务可以借助于aop直接环绕在target属性所对应操作的周围。无须为事务写多余的代码,即轻便又减少了耦合。

配置文件部分(bean.xml)由于是简单的例子所以就不写那么规范了

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name="driverClassName">
     <value>org.gjt.mm.mysql.Driver</value>
   </property>
   <property name="url">
   <value>jdbc:mysql://100.100.100.92:3306/f9inux</value>
  </property>
  <property name="username">
   <value>root</value>
  </property>
  <property name="password">
   <value>root</value>
  </property>
 </bean>
 <!-- 事务配置部分-->
 
 <bean id="Iinsert" class="com.f9inux.test.I_insert_impl">
  <property name="dataSource">
   <ref bean="dataSource"/>
  </property>
 </bean>
 
 <bean id="Transact_M" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource">
   <ref bean="dataSource"/>
  </property>
 </bean>

 <bean id="insertproxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager">
   <ref bean="Transact_M"/>
  </property>
  <property name="target">
   <ref bean="Iinsert"/>
  </property>
  <property name="transactionAttributes">
   <props>
    <!-- <prop key="insert*">PROPAGATION_REQUIRED</prop>-->
    <prop key="insert*">-MyCheckedException</prop>
   </props>
  </property>
  <property name="proxyInterfaces">
   <value>com.f9inux.test.I_insert</value>
  </property>
 </bean>
</beans>

class部分

package com.f9inux.test;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class I_insert_impl extends JdbcDaoSupport implements I_insert {

 public int insert_thing(String id,String name) {
  // TODO Auto-generated method stub
  String Sql_Str="insert into userinfo(id,name) values(?,?)";
  Object[] obj=new Object[2];
  obj[0]=Integer.valueOf(id);
  obj[1]=name;
  System.out.println(this.getJdbcTemplate().update(Sql_Str,obj));
  Sql_Str="insert into userinfo(id1,name) values(?,?)";
  return this.getJdbcTemplate().update(Sql_Str,obj);
 }}

interface部分

package com.f9inux.test;

public interface I_insert {
 public int insert_thing(String id,String name);
}

执行测试部分

package com.f9inux.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class NewTest {

 /**
  * @param args
  */
 public static void main(String[] args) {

ApplicationContext ctx=new FileSystemXmlApplicationContext("/src/Bean.xml");
  I_insert is=(I_insert)ctx.getBean("insertproxy");
  System.out.println(is.insert_thing("55","123"));
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值