每个 Spring 项目的核心配置文件 spring.xml
需包含以下基础声明:
<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.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
beans>
关键点:
beans
和 context
命名空间,用于定义 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"
:扫描该包及其子包下的所有组件。
:排除 @Controller
注解的类(由 Spring MVC 管理)。最佳实践:
cn.cjxy.service
、cn.cjxy.dao
),可细化扫描路径。include-filter
或 exclude-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
注意事项:
${key}
完全匹配(如 username
而非 name
)。作用:定义数据库连接池,管理连接资源。
你的配置:
<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 的 SqlSessionFactory
和 Mapper 接口扫描。
你的配置:
<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
)。扩展配置:
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
你的配置:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.cjxy.mapper"/>
bean>
作用:自动为 cn.cjxy.mapper
包下的接口生成代理实现类,无需手动编写实现。
注意事项:
UserMapper.java
对应 UserMapper.xml
)。作用:添加声明式事务支持(你的配置中未包含,建议补充)。
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"/>
bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
使用示例:在 Service 层添加 @Transactional
注解:
@Service
public class UserService {
@Transactional
public void updateUser(User user) {
userMapper.update(user);
}
}
整合后的 spring.xml
:
<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.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="cn.cjxy">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties"/>
<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>
<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>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"/>
bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
beans>
jdbc.properties
中的键名与 XML 中的 ${key}
是否一致。log4j.xml
中配置 com.alibaba.druid.pool
日志级别为 DEBUG
。MapperScannerConfigurer
的 basePackage
路径正确。@Transactional
。dataSource
与数据源 Bean 的 id
一致。通过本教程,你已掌握如何从零构建 spring.xml
并整合 MyBatis 与事务管理。配置文件的核心在于 分层清晰、职责明确,通过注解驱动和外部化配置,显著提升代码可维护性。