您的位置:首页 > 编程语言 > Java开发

Spring 基于xml配置方式的事务

2016-07-01 15:28 609 查看
参考前面的声明式事务的例子:http://www.cnblogs.com/caoyc/p/5632198.html

我们做了相应的修改。在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切入点。指定需要为哪些类和方法采用事务
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: