spring事务配置参数分析
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"));
}
}