Spring 基于xml配置方式的事务
2016-07-01 15:28
609 查看
参考前面的声明式事务的例子:http://www.cnblogs.com/caoyc/p/5632198.html
我们做了相应的修改。在dao中和service中的各个类中,去掉所有注解标签。然后为为每个字段提供一个setXxx()方法
最后就是配置applicationContext.xml文件了。内容如下:
这样基于xml方式的事务就配置好了。
代码分析:
事务采用的是AOP的方式。所以需要配置AOP切入点。指定需要为哪些类和方法采用事务
我们做了相应的修改。在dao中和service中的各个类中,去掉所有注解标签。然后为为每个字段提供一个setXxx()方法
最后就是配置applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 读取db.properties配置信息 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置一个C3P0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/> </bean> <!-- 配置一个JdbcTemplate,用来操作数据库 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置dao --> <bean id="bookDao" class="com.proc.dao.BookDao"> <property name="jdbcTemplate" ref="jdbcTemplate"/> </bean> <bean id="storeDao" class="com.proc.dao.StoreDao"> <property name="jdbcTemplate" ref="jdbcTemplate"/> </bean> <bean id="userDao" class="com.proc.dao.UserDao"> <property name="jdbcTemplate" ref="jdbcTemplate"/> </bean> <!-- 配置service --> <bean id="bookShopService" class="com.proc.service.BookShopServiceJdbcImps"> <property name="bookDao" ref="bookDao"/> <property name="storeDao" ref="storeDao"/> <property name="userDao" ref="userDao"/> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事务属性 --> <tx:advice id="advice"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="find*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 配置事务的切入点: AOP切入 --> <aop:config> <!-- 配置切入表达式 --> <aop:pointcut expression="execution(* com.proc.service.*.*(..))" id="pointcut"/> <aop:advisor pointcut-ref="pointcut" advice-ref="advice"></aop:advisor> </aop:config> </beans>
这样基于xml方式的事务就配置好了。
代码分析:
事务采用的是AOP的方式。所以需要配置AOP切入点。指定需要为哪些类和方法采用事务
相关文章推荐
- Java中的数组和方法
- 关于java UTF-8中文与Unicode编码之间转换,以及将浏览器地址栏编码过的中文转换UTF-8形式
- 安卓4.0开发平台搭建 图文详细 教程
- 35 个 Java 代码性能优化总结
- java泛型之通配符的使用。
- 使用java.net.URLClassLoader加载类
- 中文乱码解决方法
- 《Spring In Action》 读书笔记(5) -- @autowire,@Qualifier减少xml配置
- CAS单点登录(SSO)快速应用于企业项目开发(一)前言
- spring quartz 定时器
- 身为大神的你知道为什么Java要把字符串设计成不可变的嘛!
- DTrace Probes in HotSpot VM----java
- 去除字符串末尾指定的字符实例
- java bytes & string conversion
- java Web表示层技术1
- Java Spring-Spring与Quartz整合
- java 日期转换函数
- Java操作Excel之下载模板
- (五)spring 自动装配
- java中的数据库事务处理