spring的事务配置Xml方式

本文介绍了一个基于Spring框架的应用实例,详细展示了如何通过Spring整合Hibernate进行数据库操作,并运用AOP实现事务管理。文中配置了核心组件如SessionFactory、事务管理器及AOP切面,确保所有增删改操作都在事务保护下执行。

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

​
<?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:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/p 
  http://www.springframework.org/schema/p/spring-p-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/aop 
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

	<!-- 配置hibernate.cfg.xml的外部引用 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml">
		</property>
	</bean>

	<!-- 配置事物管理 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref local="sessionFactory" />
		</property>
	</bean>

	<!-- 事务拦截 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="mod*" propagation="REQUIRED" />
			<tx:method name="*" read-only="false" />
		</tx:attributes>
	</tx:advice>
	<!-- 配置AOP(Aspect Oriented Programming(面向切面编程)) -->
	<aop:config proxy-target-class="true">
		<!-- aop:pointcut切入点,advice通知,切入代码advisor -->
		<aop:pointcut id="point" expression="execution(* com.szdp.action.*.*(..))" />
		<aop:pointcut id="point2" expression="execution(* com.szdp.imp.*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="point" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="point2" />
	</aop:config>

	<!-- 配置(IOC(Inversion of Control(控制反转,依赖注入))) -->
	<bean id="dbImp" class="com.szdp.imp.DBImp">
		<!-- property(属性注入方式) -->
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<bean id="test" class="com.szdp.action.Test">
		<property name="dbImp" ref="dbImp"></property>
	</bean>
</beans>

​

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2023年Java面试宝典

您的鼓励是对我的肯定,共建希望

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值