当前位置: 首页 > news >正文

Spring 核心配置文件(spring.xml)构建指南


一、配置文件基础结构

每个 Spring 项目的核心配置文件 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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!-- 配置内容 -->
</beans>

关键点

  • 声明 XML 版本和编码。
  • 引入 beanscontext 命名空间,用于定义 Bean 和注解驱动配置。

二、组件扫描与过滤

作用:自动扫描并注册带有 @Component@Service 等注解的类为 Spring Bean。

你的配置

<context:component-scan base-package="cn.cjxy"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

解释

  • base-package="cn.cjxy":扫描该包及其子包下的所有组件。
  • <exclude-filter>:排除 @Controller 注解的类(由 Spring MVC 管理)。

最佳实践

  • 分层扫描:若项目分层明确(如 cn.cjxy.servicecn.cjxy.dao),可细化扫描路径。
  • 过滤策略:通过 include-filterexclude-filter 精准控制扫描范围。

三、加载外部属性文件

作用:从 jdbc.properties 加载数据库连接信息,避免硬编码。

你的配置

<context:property-placeholder location="classpath:jdbc.properties"/>

关键点

  • location="classpath:jdbc.properties":从类路径(如 src/main/resources)加载文件。
  • 属性文件示例 jdbc.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC
username=root
password=your_password

注意事项

  • 确保属性键名与 XML 中 ${key} 完全匹配(如 username 而非 name)。

四、配置数据源(Druid)

作用:定义数据库连接池,管理连接资源。

你的配置

<bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource"><property name="username" value="${username}"/><property name="password" value="${password}"/><property name="url" value="${url}"/><property name="driverClassName" value="${driver}"/>
</bean>

优化建议

  • 连接池参数:添加初始连接数、最大活跃数等配置。
<property name="initialSize" value="5"/>
<property name="maxActive" value="20"/>
<property name="minIdle" value="5"/>

五、整合 MyBatis

作用:配置 MyBatis 的 SqlSessionFactory 和 Mapper 接口扫描。

1. 配置 SqlSessionFactory

你的配置

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="datasource"/><property name="typeAliasesPackage" value="cn.cjxy.domain"/>
</bean>

解释

  • dataSource:引用上一步定义的 Druid 数据源。
  • typeAliasesPackage:自动为 cn.cjxy.domain 包下的类注册别名(如 User 对应 User)。

扩展配置

  • 指定 MyBatis 配置文件
<property name="configLocation" value="classpath:mybatis-config.xml"/>
  • 加载 XML 映射文件
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
2. 扫描 Mapper 接口

你的配置

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="cn.cjxy.mapper"/>
</bean>

作用:自动为 cn.cjxy.mapper 包下的接口生成代理实现类,无需手动编写实现。

注意事项

  • 确保 Mapper 接口与 XML 映射文件的路径一致(如 UserMapper.java 对应 UserMapper.xml)。

六、事务管理(补充)

作用:添加声明式事务支持(你的配置中未包含,建议补充)。

<!-- 1. 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="datasource"/>
</bean>
<!-- 2. 启用注解驱动事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

使用示例:在 Service 层添加 @Transactional 注解:

@Service
public class UserService {@Transactionalpublic void updateUser(User user) {userMapper.update(user);}
}

七、完整配置参考

整合后的 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:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 1. 组件扫描 --><context:component-scan base-package="cn.cjxy"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!-- 2. 加载属性文件 --><context:property-placeholder location="classpath:jdbc.properties"/><!-- 3. 数据源配置 --><bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${driver}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/><property name="initialSize" value="5"/><property name="maxActive" value="20"/></bean><!-- 4. MyBatis 整合 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="datasource"/><property name="typeAliasesPackage" value="cn.cjxy.domain"/><property name="mapperLocations" value="classpath:mapper/*.xml"/></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="cn.cjxy.mapper"/></bean><!-- 5. 事务管理 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="datasource"/></bean><tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

八、常见问题与调试技巧
  1. 数据源连接失败
    • 检查 jdbc.properties 中的键名与 XML 中的 ${key} 是否一致。
    • 查看 Druid 日志:在 log4j.xml 中配置 com.alibaba.druid.pool 日志级别为 DEBUG
  2. Mapper 接口未注入
    • 确保 MapperScannerConfigurerbasePackage 路径正确。
    • 检查 Mapper 接口是否在指定包下,且未被其他扫描策略排除。
  3. 事务不生效
    • 确认 Service 类和方法上添加了 @Transactional
    • 确保事务管理器 Bean 的 dataSource 与数据源 Bean 的 id 一致。

通过本教程,你已掌握如何从零构建 spring.xml 并整合 MyBatis 与事务管理。配置文件的核心在于 分层清晰、职责明确,通过注解驱动和外部化配置,显著提升代码可维护性。

http://www.xdnf.cn/news/649027.html

相关文章:

  • Vue 核心技术与实战day04
  • anaconda环境变量+vscode汉化配置
  • Unity 3D AssetBundle加密解密教程
  • 【后端高阶面经:Elasticsearch篇】38、Elasticsearch 高可用架构解析:分片容灾、Translog 调优与双集群
  • UDP和TCP特征的详解
  • 鸿蒙OSUniApp 制作自定义的进度条组件#三方框架 #Uniapp
  • 上海市计算机学会竞赛平台2025年5月月赛丙组手机充电
  • TCP协议原理与Java编程实战:从连接建立到断开的完整解析
  • 计算机网络】深入解析 TCP 协议:从三次握手到拥塞控制
  • java高级 -动态代理
  • 华为云Flexus+DeepSeek征文 | DeepSeek-V3/R1商用服务开通体验全流程及使用评测
  • 项目部署一次记录
  • 第7章:Zephyr 的低功耗机制
  • 在 ElementUI 中实现 Table 单元格合并
  • 【Android】SharePreference原理
  • 【ARTS】【LeetCode-59】螺旋矩阵
  • 【HarmonyOS 5应用架构详解】深入理解应用程序包与多Module设计机制
  • 深度解析 8086 处理器:x86 架构的奠基者
  • 【后端高阶面经:架构篇】46、分布式架构:如何应对高并发的用户请求
  • 2025社区团购系统开发:未来趋势、核心技术与落地解决方案
  • Python - 文件部分
  • 【React】- React-RND 深度使用指南:实现自由拖拽、避坑受控陷阱!
  • Hadoop架构与核心模块解析
  • 【每日渲美学】3ds Max橱柜材质教程:厨房高光烤漆、木纹、亚克力、亚光板材渲染优化指南
  • 洪水危险性评价与风险防控全攻略:从HEC-RAS数值模拟到ArcGIS水文分析,一键式自动化工具实战,助力防洪减灾与应急管理
  • 探索数据结构之顺序表:从入门到精通
  • 「读书报告」Spark实时大数据分析
  • 数据结构-图的应用,实现环形校验和拓扑排序
  • redis五种数据结构详解(java实现对应的案例)
  • 高阶数据结构——哈希表的实现