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: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.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.itheima"/>
<!-- 1.配置数据源 -->
<bean id="dataSource" class=
"org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--数据库驱动 -->
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<!--连接数据库的url -->
<property name="url" value="jdbc:mysql://localhost/spring"/>
<!--连接数据库的用户名 -->
<property name="username" value="root"/>
<!--连接数据库的密码 -->
<property name="password" value="root"/>
</bean>
<!-- 2.配置事务管理器,依赖于数据源 -->
<bean id="transactionManager" class=
"org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 3.配置通知:对事务进行增强(通知),需要编写对切入点和具体执行事务细节 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 配置事务参数 -->
<tx:attributes>
<!-- name:*表示任意方法名称 -->
<tx:method name="*" propagation="REQUIRED"
isolation="DEFAULT" read-only="false" />
</tx:attributes>
</tx:advice>
<!-- 4.配置切入点和切面 -->
<aop:config>
<!-- 切入点 -->
<aop:pointcut id="txPointCut"
expression="execution(* com.itheima.dao.*(..))" />
<!-- 切面:将切入点与通知整合 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" />
</aop:config>
</beans>
这段代码是 Spring 框架的 XML 配置文件,主要用于配置组件扫描、数据源、事务管理器以及声明式事务,实现对数据库操作的事务管理。下面逐部分解释:
1. 组件扫描
xml
<context:component-scan base-package="com.itheima"/>
- 作用:开启 Spring 的组件扫描功能,扫描
com.itheima包及其子包下的类。 - 当类上标注
@Component、@Service、@Repository、@Controller等注解时,Spring 会自动将其识别为 Bean 并纳入容器管理。
2. 配置数据源(DataSource)
xml
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/spring"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
- 作用:配置数据库连接信息,Spring 通过数据源获取数据库连接。
- 细节:
class:使用 Spring 提供的DriverManagerDataSource(简单数据源,适合测试,生产环境常用 Druid、Hikari 等)。driverClassName:MySQL 数据库驱动类(com.mysql.cj.jdbc.Driver是 MySQL 8.x 后的驱动)。url:数据库连接地址,localhost是数据库服务器地址,spring是数据库名。username和password:数据库登录的用户名和密码。
3. 配置事务管理器(TransactionManager)
xml
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
- 作用:定义事务管理器,负责管理事务的开启、提交、回滚等操作。
- 细节:
class:使用DataSourceTransactionManager(基于数据源的事务管理器,适用于 JDBC、MyBatis 等持久层框架)。dataSource:依赖上面配置的dataSource(通过ref引用 Bean 的 id),事务管理器需要通过数据源获取数据库连接来控制事务。
4. 配置事务通知(tx:advice)
xml
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
</tx:attributes>
</tx:advice>
- 作用:定义事务的具体规则(通知),指定哪些方法需要事务支持,以及事务的传播行为、隔离级别等属性。
- 细节:
transaction-manager:关联上面配置的事务管理器transactionManager。tx:attributes:配置事务参数,通过tx:method指定方法的事务规则。name="*":对所有方法生效(*是通配符)。propagation="REQUIRED":事务传播行为为 "必须有事务"(如果当前没有事务,则创建新事务;如果已有事务,则加入当前事务)。isolation="DEFAULT":事务隔离级别使用数据库默认值(如 MySQL 默认是可重复读,Oracle 默认是读已提交)。read-only="false":事务不是只读(适用于增删改操作,查询操作可设为true提升性能)。
5. 配置切入点和切面(AOP)
xml
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* com.itheima.dao.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
- 作用:通过 AOP(面向切面编程)将事务通知(规则)应用到指定的方法上,实现声明式事务(无需手动写事务代码,由 Spring 自动管理)。
- 细节:
aop:pointcut:定义切入点,指定哪些方法需要被事务管理。expression="execution(* com.itheima.dao.*(..))":匹配com.itheima.dao包下的所有类的所有方法(*表示任意返回值、任意类、任意方法名,(..)表示任意参数)。
aop:advisor:将切入点(txPointCut)和事务通知(txAdvice)关联,形成切面 ------ 即 "对com.itheima.dao包下的所有方法,应用txAdvice定义的事务规则"。
总结
这段配置的核心是声明式事务管理 :通过 AOP 将事务规则(传播行为、隔离级别等)自动应用到com.itheima.dao包下的方法,Spring 会在方法执行前开启事务,执行成功后提交事务,出现异常时回滚事务,无需手动编写事务控制代码,简化了开发。
<bean>中ref是干嘛用的
在 Spring 的<bean>配置中,ref(即ref属性)的作用是引用容器中已经定义的其他 Bean,实现 Bean 之间的依赖注入(Dependency Injection)。
简单来说,当一个 Bean 需要依赖另一个 Bean 时,就可以通过ref来指定要引用的目标 Bean 的id,从而让 Spring 将目标 Bean 注入到当前 Bean 中。
举个例子,在之前的事务管理器配置中:
xml
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
这里的ref="dataSource"表示:事务管理器transactionManager依赖于id为dataSource的数据源 Bean,Spring 会将dataSource这个 Bean 注入到transactionManager的dataSource属性中,从而让事务管理器能够通过数据源获取数据库连接,实现事务控制。
总结一下,ref是 Spring 实现 Bean 之间依赖关系的关键配置,它让 Bean 可以按需引用和组合其他 Bean,是依赖注入的核心手段之一。