Spring配置声明式事务--xml配置文件方式
2016-08-09 10:31
501 查看
配置文件结构:
applicationContext-db.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/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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:property-placeholder location="classpath:db/db.properties"/>
<!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${url}"></property>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate/hibernate.cfg.xml"></property>
<property name="mappingLocations" value="classpath:cn/lfd/oa/domain/*.hbm.xml"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务属性 -->
<tx:advice transaction-manager="transactionManager" id="txAdvice">
<tx:attributes>
<tx:method name="save*" read-only="false"/>
<tx:method name="update*" read-only="false"/>
<tx:method name="delete*" read-only="false"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 关联事务 -->
<aop:config>
<aop:pointcut expression="execution(* cn.lfd.oa.service.impl.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
</beans>execution(* cn.lfd.oa.service.impl.*.*(..)) 解释:
第一个*:所有返回值,所有访问权限,然后是你要加入事务的包
第二个*:这个包下面的所有类
第三个*:这个类里面的所有方法
括号里面的点点代表所有参数
applicationContext-db.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/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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:property-placeholder location="classpath:db/db.properties"/>
<!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${url}"></property>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate/hibernate.cfg.xml"></property>
<property name="mappingLocations" value="classpath:cn/lfd/oa/domain/*.hbm.xml"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务属性 -->
<tx:advice transaction-manager="transactionManager" id="txAdvice">
<tx:attributes>
<tx:method name="save*" read-only="false"/>
<tx:method name="update*" read-only="false"/>
<tx:method name="delete*" read-only="false"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 关联事务 -->
<aop:config>
<aop:pointcut expression="execution(* cn.lfd.oa.service.impl.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
</beans>execution(* cn.lfd.oa.service.impl.*.*(..)) 解释:
第一个*:所有返回值,所有访问权限,然后是你要加入事务的包
第二个*:这个包下面的所有类
第三个*:这个类里面的所有方法
括号里面的点点代表所有参数
相关文章推荐
- Spring入门之事务管理【一】XML配置文件方式
- Spring之AOP声明式事务管理(采用xml的配置方式更好)
- Spring 声明式事务管理 Annotation配置 xml文件
- Spring中加载XML配置文件的方式
- spring中读取xml配置文件、获取bean的几种方式
- spring+hibernate xml和注解方式配置事务
- spring 中加载xml配置文件的方式.
- spring配置文件application.xml中的事务总结
- Spring 声明式事务的配置方式(四)
- SSH整合-关于Spring中applicationContext.xml文件对于事务的一些配置
- spring中读取xml配置文件、获取bean的几种方式
- Spring中加载xml配置文件的方式
- Spring 声明式事务的配置方式(一)
- spring采用基于xml配置方式进行事务的处理
- Spring配置文件applicationContext.xml之事务管理
- Spring配置文件applicationContext.xml(3)开启事务和注解
- spring配置文件applicationContext.xml中的事务总结
- spring 中加载xml配置文件的方式.
- spring中加载xml配置文件的方式
- Spring中加载XML配置文件的方式