spring事务-xml方式

本文详细介绍了Spring框架中事务管理的配置方法,包括beans.xml文件的设置,如何使用Spring读取外部属性文件,组件扫描,JDBC模板及数据源配置,以及声明式事务的实现。通过具体代码示例,展示了不同事务传播级别的方法命名规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

beans.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.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


	<!-- Spring 读取db.properties -->
	<context:property-placeholder location="classpath:db.properties" />
	<context:component-scan base-package="com.liuyun"></context:component-scan>

	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- spring-jdbc内置数据源 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driverClass}"></property>
		<property name="url" value="${jdbc.jdbcUrl}"></property>
		<property name="username" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	
	<!-- 声明式事务 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<tx:advice id="trans_adv" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="count*" propagation="SUPPORTS" read-only="true"/>
			<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
			<tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
			<tx:method name="insert*" propagation="REQUIRED" read-only="false"/>
			<tx:method name="save*" propagation="REQUIRED" read-only="false"/>
			<tx:method name="update*" propagation="REQUIRED" read-only="false"/>
			<tx:method name="modify*" propagation="REQUIRED" read-only="false"/>
			<tx:method name="delete*" propagation="REQUIRED" read-only="false"/>
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut expression="execution(* com.liuyun.service.impl.*.*(..))" id="servicePt"/>
		<aop:advisor advice-ref="trans_adv" pointcut-ref="servicePt"/>
	</aop:config>
	<aop:aspectj-autoproxy/>
</beans>

关键配置:
在这里插入图片描述
完整代码:https://github.com/liuyunHappy/spring-tx-xml.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值