spring 基于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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 用于注解 -->
<context:annotation-config/> <!-- 方式一:基于注解声明事务:如果事务采用注解方式,就用该句 -->
<!--tx:annotation-driven transaction-manager="transactionManager"/--> <!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
<property name="username" value="lesson"/>
<property name="password" value="lesson"/>
</bean>
<!-- 实例化sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocations">
<list>
<value>classpath:hibernate.cfg.xml</value>
</list>
</property>
</bean>
<!-- 配置事务管理 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean> <!-- 方式二:基于xml的事务配置 开始 -->
<aop:config proxy-target-class="true">
<!-- expression(*)执行的实现类 -->
<aop:pointcut id="serviceMethods" expression="execution(* com.spring.jdbc.service.impl.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="get*" propagation="SUPPORTS"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 基于xml的事务配置 结束 --> <bean id="userDao" class="com.spring.jdbc.dao.impl.UserDaoImpl"></bean>
<bean id="userService" class="com.spring.jdbc.service.impl.UserServiceImpl"></bean> </beans>
上一篇:win 10 升级远程连接服务器 要求的函数不受支持


下一篇:Java日志使用slf4j 配置log4j后,有日志文件 但日志文件内容为空