Spring(15):为业务层Service 配置声明式事务(下)-- @Transactional注解配置
2018-01-03 20:46
429 查看
2018/1/3
承接《Spring(15):为业务层Service 配置声明式事务(上)-- AOP配置》,项目介绍使用注解实现声明式事务的功能:
【1】对工程的修改并不大,只是利用aop的注解代替aop的xml配置,在xml文件的配置作以下修改:
解释:
(1)仍然需要在Spring配置文件中配置 事务管理器(类),并添加对注解配置事务的支持;
【2】对实现类UserServiceImpl.java作以下修改:
解释:
(1)使用@Transactional 注解即可为该类的所以业务方法统一添加事务处理;
(2)如果有例外,某个方法需要采用不同的事务规则,可以在改业务方法上添加@Transactional注解,单独设置;
(3)@Transactional 也是可以设置事务属性的值。
【3】工程说明:
综上,这里使用了@Transactional注解代替xml的配置,利用xml文件配置aop其实也不难,不妨参考上一篇博文:《Spring(15):为业务层Service
配置声明式事务(上)-- AOP配置》。
$(".MathJax").remove();
承接《Spring(15):为业务层Service 配置声明式事务(上)-- AOP配置》,项目介绍使用注解实现声明式事务的功能:
【1】对工程的修改并不大,只是利用aop的注解代替aop的xml配置,在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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 数据源配置 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url"> <value><![CDATA[jdbc:mysql://127.0.0.1:3306/test? useUnicode=true&characterEncoding=utf-8]]></value> </property> <property name="username" value="root"></property> <property name="password" value=""></property> </bean> <!-- SqlSessionFactoryBean 配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 引用数据源组件 --> <property name="dataSource" ref="dataSource"></property> <!-- 引用Mybatis配置文件的配置 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- 配置MapperScannerConfigurer,扫描生成mapper --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.smbms.dao"></property> </bean> <!-- 扫描注解配置 --> <context:component-scan base-package="com.smbms.service"></context:component-scan> <!-- 事务管理器组件,以提供对事务处理的全面支持和统一管理,在切面中相当于增强处理的角色 ,需要注入数据源组件--> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean>
<tx:annotation-driven transaction-manager="txManager"/> </beans>
解释:
(1)仍然需要在Spring配置文件中配置 事务管理器(类),并添加对注解配置事务的支持;
【2】对实现类UserServiceImpl.java作以下修改:
package com.smbms.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.smbms.dao.UserMapper; import com.smbms.entities.User; @Transactional @Service("userService") public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public boolean addNewUser(User user) { boolean result = false; try{ if(userMapper.add(user) == 1){ result = true; } }catch (RuntimeException e){ e.printStackTrace(); throw e; } return result; } @Transactional(propagation = Propagation.SUPPORTS) @Override public List<User> findUserByUsername(String username) { return findUserByUsername(username); } public UserServiceImpl(UserMapper userMapper) { super(); this.userMapper = userMapper; } public UserServiceImpl() { super(); // TODO 自动生成的构造函数存根 } public UserMapper getUserMapper() { return userMapper; } public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } }
解释:
(1)使用@Transactional 注解即可为该类的所以业务方法统一添加事务处理;
(2)如果有例外,某个方法需要采用不同的事务规则,可以在改业务方法上添加@Transactional注解,单独设置;
(3)@Transactional 也是可以设置事务属性的值。
【3】工程说明:
(1)这是Mybatis和Spring整合练习的工程; (2)实际使用有3个包:dao、entities、service; (3)Mybatis配置文件内容很简洁,Spring完成大部分配置管理; (4)通过SQLSessionTemplate的实现类对数据库进行操作; (5)配置DAO组件并进入SqlSessionTemplate实例; (6)配置业务Bean并注入DAO实例 ; (7)完成的功能是:查询provider的列表,模糊查询供应商信息; (8)与smbms05MybatisSpring的区别:去掉了实现类ProviderMapperImpl;仅仅保留ProviderMapper 接口和相关SQL映射文件,通过MapperFactoryBean注入给业务组件。 (9)此外,也新增了一个User查询,照壶画瓢; (10)smbms06是使用MapperFactoryBean注入映射器, 而smbms07是更加方便的使用MapperScannerConfigurer注入映射器。 (11)smbms08增加新功能,实现按条件查询订单表Bill;使用resultMap做显示列表字段的自定义映射, 使用Map传参, 采用MapperFactoryBean注册映射器实现。 [bill 和 provider 表] (12) smbms09 在 smbms08 的基础上,进行拓展,用MapperScannerConfigurer注入映射器。 (13) smbms10,声明式事务,提高在XML配置文件注册org.springframework.jdbc.datasource.DataSourceTransactionManager, 然后利用AOP切面对事务进行注入时。 (14) smbms11,使用注解去实现声明式事务,@Transaction。
综上,这里使用了@Transactional注解代替xml的配置,利用xml文件配置aop其实也不难,不妨参考上一篇博文:《Spring(15):为业务层Service
配置声明式事务(上)-- AOP配置》。
$(".MathJax").remove();
相关文章推荐
- Spring(15):为业务层Service 配置声明式事务(上)-- AOP配置
- 04 Spring 注解配置,简化xml配置,@Service、@Autowired简介
- Spring 自定义属性编辑(CustomEditorConfigurer)和类型转换器(ConversionServiceFactoryBean)一起配置问题
- spring配置 配置service上 不用加任何注释
- Spring事务配置笔记(实现不同Service间调用事务)
- Spring声明式事务配置管理方法
- Spring声明式事务配置管理方法
- Spring声明式事务配置管理方法
- 关于搭建restful web service + springMVC + ibatis/hibernate的整合和XML配置意思和ssh+springMVC框架搭建
- SpringBoot+ZooKeeper+ZKUI+Drools 实现应用配置中心及业务规则动态加载
- 04 Spring 注解配置,简化xml配置,@Service、@Autowired简介
- Spring声明式事务配置管理方法
- Spring声明式事务配置管理方法
- spring配置出错at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:244
- Spring的事务到底该给Dao配置还是给Service配置?
- Spring配置事务在SERVICE层 我认为是经典
- Spring声明式事务配置
- Spring(四)基于注解配置IOC容器&基于注解实现声明式事务
- Spring声明式事务配置管理方法
- Spring声明式事务配置管理方法