spring3.2+hibernate4.1采用声明式的事务处理
2016-08-02 16:25
369 查看
http://wosyingjun.iteye.com/blog/1839858
部分重要代码:
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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-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/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<!-- 采用的annotation的方式进行AOP和 IOC -->
<!-- 配置了component-scan可以移除 -->
<context:annotation-config />
<!-- 启动对@AspectJ注解的支持 -->
<aop:aspectj-autoproxy />
<!-- 自动扫描包(自动注入) -->
<context:component-scan base-package="yingjun" />
<!-- 配置数据源 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:jdbc.properties"/>
</bean>
<bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- Hibernate配置 -->
<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="dataSource" ref="DataSource"/>
<property name="packagesToScan">
<list>
<value>yingjun.model</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
</value>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory"/>
</bean>
<!-- 声明试事务管理 -采用annotation方式-->
<tx:annotation-driven transaction-manager="txManager"/>
<!-- 声明式事务管理 -采用XML方式-->
<!--常见几张几种事务传播行为类型
PROPAGATION_REQUIRED 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。
PROPAGATION_SUPPORTS 支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY 使用当前的事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW 新建事务,如果当前存在事务,把当前事务挂起。
-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="DoAdd*" propagation="REQUIRED"/>
<tx:method name="someOtherBusinessMethod" propagation="REQUIRES_NEW"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="productServiceMethods"
expression="execution(* yingjun.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods"/>
</aop:config>
</beans>
Java代码
package yingjun.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import yingjun.dao.LogDaoI;
import yingjun.dao.UserDaoI;
import yingjun.model.Log;
import yingjun.model.User;
@Component("userService")
public class UserService {
private UserDaoI userdao;
private LogDaoI logdao;
//@Transactional()
public void DoAddUser(User user,Log log){
userdao.AddUser(user);
logdao.addLog(log);
}
public UserDaoI getUserdao() {
return userdao;
}
@Resource(name="userDao")
public void setUserdao(UserDaoI userdao) {
this.userdao = userdao;
}
public LogDaoI getLogdao() {
return logdao;
}
@Resource(name="logDao")
public void setLogdao(LogDaoI logdao) {
this.logdao = logdao;
}
}
Java代码
package yingjun.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import yingjun.model.Log;
import yingjun.model.User;
import yingjun.service.UserService;
public class SpringHibernateTest {
@Test
public void test(){
ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
UserService us=(UserService)ac.getBean("userService");
User user=new User();
Log log=new Log();
us.DoAddUser(user,log);
}
}
部分重要代码:
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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-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/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<!-- 采用的annotation的方式进行AOP和 IOC -->
<!-- 配置了component-scan可以移除 -->
<context:annotation-config />
<!-- 启动对@AspectJ注解的支持 -->
<aop:aspectj-autoproxy />
<!-- 自动扫描包(自动注入) -->
<context:component-scan base-package="yingjun" />
<!-- 配置数据源 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:jdbc.properties"/>
</bean>
<bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- Hibernate配置 -->
<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="dataSource" ref="DataSource"/>
<property name="packagesToScan">
<list>
<value>yingjun.model</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
</value>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory"/>
</bean>
<!-- 声明试事务管理 -采用annotation方式-->
<tx:annotation-driven transaction-manager="txManager"/>
<!-- 声明式事务管理 -采用XML方式-->
<!--常见几张几种事务传播行为类型
PROPAGATION_REQUIRED 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。
PROPAGATION_SUPPORTS 支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY 使用当前的事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW 新建事务,如果当前存在事务,把当前事务挂起。
-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="DoAdd*" propagation="REQUIRED"/>
<tx:method name="someOtherBusinessMethod" propagation="REQUIRES_NEW"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="productServiceMethods"
expression="execution(* yingjun.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods"/>
</aop:config>
</beans>
Java代码
package yingjun.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import yingjun.dao.LogDaoI;
import yingjun.dao.UserDaoI;
import yingjun.model.Log;
import yingjun.model.User;
@Component("userService")
public class UserService {
private UserDaoI userdao;
private LogDaoI logdao;
//@Transactional()
public void DoAddUser(User user,Log log){
userdao.AddUser(user);
logdao.addLog(log);
}
public UserDaoI getUserdao() {
return userdao;
}
@Resource(name="userDao")
public void setUserdao(UserDaoI userdao) {
this.userdao = userdao;
}
public LogDaoI getLogdao() {
return logdao;
}
@Resource(name="logDao")
public void setLogdao(LogDaoI logdao) {
this.logdao = logdao;
}
}
Java代码
package yingjun.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import yingjun.model.Log;
import yingjun.model.User;
import yingjun.service.UserService;
public class SpringHibernateTest {
@Test
public void test(){
ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
UserService us=(UserService)ac.getBean("userService");
User user=new User();
Log log=new Log();
us.DoAddUser(user,log);
}
}
相关文章推荐
- Spring+Hibernate延迟和声明式事务处理最终解决方案
- 在Spring中采用声明式方法对Hibernate和JDBC进行统一的事务配置(AOP)
- spring声明式事务处理(hibernate)
- Spring不同事务管理方式与声明式事务管理局部回滚处理方案
- 《Spring》------声明式事务处理
- Spring 声明式事务处理....XML
- 关于Spring 声明式事务处理时,throws exception不回滚的问题
- spring与Hibernate的整合(以及spring的声明式事务处理)
- Spring源代码解析(六):Spring声明式事务处理
- Spring学习心得(23)-- spring声明式事务处理(hibernate)
- 在Spring中采用声明式方法对Hibernate和JDBC进行统一的事务配置(AOP)
- Spring源代码解析(六):Spring声明式事务处理
- spring 声明式事务处理及测试用类,其他未列出。
- Spring 的声明式事务处理
- spring声明式事务处理-注解
- Spring自学教程-声明式事务处理(六)
- Spring源代码解析(六):Spring声明式事务处理
- Spring学习心得(22)-- spring声明式事务处理(使用jdbc操作数据库)
- Spring的声明式事务处理
- Spring3声明式事务处理事务无法回滚rollback分析(annotation与xml配置混用)