详解Spring MVC事务配置

网友投稿 326 2023-05-24


详解Spring MVC事务配置

要了解事务配置的所有方法,请看一下《Spring事务配置的5种方法》

本文介绍两种配置方法:

一、XML,使用tx标签配置拦截器实现事务

二、Annotation方式

以下所使用环境为Spring4.0.3、Hibernate4.3.5

一、 XML,使用tx标签配置拦截器实现事务

Entity类User.java,持久化类,对应数据库表user

package com.lei.demo.entity;

import javax.persistence.*;

@Entity(name="users")

public class Users {

public Users(){

super();

}

@Id

@GeneratedValue(strategy=GenerationType.AUTO)

@Column(name="id")

private Integer id;

@Column(name="user_name",length=32)

private String user_name;

@Column(name="age")

private Integer age;

@Column(name="nice_name",length=32)

private String nice_name;

//属性实现......

}

UserDAO.javar,表user的一些操作,其中属性sessionFactory应该由Spring注入,如下:

package com.lei.demo.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.stereotype.Repository;

import com.lei.demo.entity.Users;

public class UsersDAO {

private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {

this.sessionFactory = sessionFactory;

}

public SessionFactory getSessionFactory() {

return sessionFactory;

}

public List getAllUser(){

String hsql="from users";

Session session = sessionFactory.getCurrentSession();

Query query = session.createQuery(hsql);

return query.list();

}

}

UserService.java,业务实现类,如下

package com.lei.demo.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Isolation;

import org.springframework.transaction.annotation.Propagation;

import org.springframework.transaction.annotation.Transactional;

import com.lei.demo.dao.*;

public class UserService {

private UsersDAO userDao;

public int userCount(){

return userDao.getAllUser().size();

}

public UsersDAO getUserDao() {

return userDao;

}

public void setUserDao(UsersDAO userDao) {

this.userDao = userDao;

}

}

首先看一下xml配置,spring-hibernate.xml如下:

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-4.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-4.0.xsd

">

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

com.lei.demo.entity

${hibernate.hbm2ddl.auto}

${hibernate.dialect}

${hibernate.show_sql}

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-4.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-4.0.xsd

">

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

com.lei.demo.entity

${hibernate.hbm2ddl.auto}

${hibernate.dialect}

${hibernate.show_sql}

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

com.lei.demo.entity

${hibernate.hbm2ddl.auto}

${hibernate.dialect}

${hibernate.show_sql}

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

其中主要配置中是tx:advice和aop:config两个配置节,以Spring AOP的方式实现事务管理。

tx:advice配置了事务的管理者是transactionManager,同时tx:method也规定了如果方法名匹配“add*”和“get*”方法时使用事务,propagation是设定事务的传播级别。除了“add*”和“get*”方法,其他的方法的事务是只读的(典型地,对于只执行查询的事务你会将该属性设为true,如果出现了更新、插入或是删除语句时只读事务就会失败)

aop:config指定了一个aop:pointcut去引用上边的advice。

这样就通过AOP的拦截机制实现了事务,当然你还要用Spring的方式自己配置UserDAO和UserService。

二、Annotation方式

第一步,首先看一下web.xml,如下:

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:web="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

id="WebApp_ID" version="3.0">

Archetype Created Web Application

contextConfigLocation

classpath:/spring-*.xml

org.springframework.web.context.ContextLoaderListener

lei-dispatcher

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:/lei-dispatcher-servlet.xml

1

lei-dispatcher

/

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:web="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

id="WebApp_ID" version="3.0">

Archetype Created Web Application

contextConfigLocation

classpath:/spring-*.xml

org.springframework.web.context.ContextLoaderListener

lei-dispatcher

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:/lei-dispatcher-servlet.xml

1

lei-dispatcher

/

第二步,spring-hibernate配置,见以下spring-hibernate.xml配置

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-4.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-4.0.xsd

">

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

com.lei.demo.entity

${hibernate.hbm2ddl.auto}

${hibernate.dialect}

${hibernate.show_sql}

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-4.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-4.0.xsd

">

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

com.lei.demo.entity

${hibernate.hbm2ddl.auto}

${hibernate.dialect}

${hibernate.show_sql}

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

com.lei.demo.entity

${hibernate.hbm2ddl.auto}

${hibernate.dialect}

${hibernate.show_sql}

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

第一节中xml配置事务中需要通过配置tx:advice和aop:config来增加事务的功能。此处采用全注释方法,这两个配置节就不需要了。

相应的需要在视图解析配置中启用注释,如下lei-dispatcher-servlet.xml

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:p="http://springframework.org/schema/p"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-4.0.xsd

">

/WEB-INF/user/

.jsp

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:p="http://springframework.org/schema/p"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-4.0.xsd

">

/WEB-INF/user/

.jsp

UserDAO如下

package com.lei.demo.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.stereotype.Repository;

import com.lei.demo.entity.Users;

@Repository

public class UsersDAO {

@Resource(name="sessionFactory")

private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {

this.sessionFactory = sessionFactory;

}

public SessionFactory getSessionFactory() {

return sessionFactory;

}

public List getAllUser(){

String hsql="from users";

Session session = sessionFactory.getCurrentSession();

Query query = session.createQuery(hsql);

return query.list();

}

}

UserService.java如下

package com.lei.demo.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Isolation;

import org.springframework.transaction.annotation.Propagation;

import org.springframework.transaction.annotation.Transactional;

import com.lei.demo.dao.*;

@Service("userService")

public class UserService {

@Resource

private UsersDAO userDao;

@Transactional

public int userCount(){

return userDao.getAllUser().size();

}

public UsersDAO getUserDao() {

return userDao;

}

public void setUserDao(UsersDAO userDao) {

this.userDao = userDao;

}

}

这里,方法名userCount上加入@Transactional,说明这个方法要启用事务。如果类名UserService上加入@Transactional,则表明这个类中的所有方法都会启用事务。

如果配有多个transactionManager,例如配置有transactionManager1,和transactionManager2,则可以通过@Transactional(“transactionManager1”),的方式指定使用哪个数据源的事务。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java构造方法实例详解(动力节点java学院整理)
下一篇:Spring与Spring boot的区别介绍
相关文章

 发表评论

暂时没有评论,来抢沙发吧~