applicationContext.xml 是什么?Spring 容器 Bean 配置与注入详解
Spring 容器(IoC)的核心配置文件 applicationContext.xml,是用来声明 Bean、注入依赖、配置数据源与事务的 XML 文件。虽然注解已成为主流,但老项目、需要外部化配置的场景仍大量使用它。本文说明它是什么、核心语法、加载方式,以及注解与 XML 的选择边界。

一、它是什么
applicationContext.xml 是 Spring 应用上下文的配置入口,被 ClassPathXmlApplicationContext 或 Web 容器的 ContextLoaderListener 读取。文件名本身不是强制的(Spring 不认这个名字),它只是社区约定,同类的还有 spring-beans.xml、dao-context.xml。内容通常包含:
<bean>:定义对象、作用域、初始化方法、依赖注入<context:component-scan>:开启注解扫描<context:property-placeholder>:读取外部 properties<tx:annotation-driven>、<aop:config>:事务与切面<import resource="..."/>:拆分多文件
二、最小骨架与常见配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 读取外部配置 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 数据源(属性注入 + 初始化回调) -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 构造注入 + 引用其他 bean -->
<bean id="userDao" class="com.demo.dao.UserDao">
<constructor-arg ref="dataSource"/>
</bean>
<bean id="userService" class="com.demo.service.UserService" scope="singleton">
<property name="userDao" ref="userDao"/>
</bean>
<!-- 开启注解:与 XML 混用 -->
<context:component-scan base-package="com.demo"/>
</beans>
三、注入方式的三种写法
- setter(属性注入):
<property name="x" ref="y"/>或value="${...}"。 - 构造注入:
<constructor-arg>,可用index/name/type消歧,参数是集合时用<list><value>。 - 自动装配:
autowire="byType"/byName。它按类型或属性名自动找 bean,看似省事,但隐式依赖会让重构变得危险(改类型名即静默失败),新项目不建议使用。
<bean id="mailSender" class="...SimpleMailSender">
<property name="props">
<map>
<entry key="host" value="smtp.example.com"/>
<entry key="port" value="465"/>
</map>
</property>
<property name="targets">
<list><ref bean="a"/><ref bean="b"/></list>
</property>
</bean>
四、加载方式
4.1 独立程序
try (var ctx = new ClassPathXmlApplicationContext("applicationContext.xml")) {
UserService svc = ctx.getBean("userService", UserService.class);
}
用 FileSystemXmlApplicationContext 可以从外部目录读取配置,便于运维改配置而不重新打包。
4.2 Web 应用
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml,classpath:spring-mvc.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
五、和注解/Java 配置怎么选
- XML 的优势:配置与代码彻底分离(可运维期热改)、第三方库的类无法加注解时只能外部声明、批量看依赖关系清晰、适合“同一实现换不同 bean”的场景(策略切换、mock 注入)。
- 注解/JavaConfig 的优势:类型安全(编译期检查 ref/class 名,拼错在启动才炸)、重构友好、IDE 支持好、Spring Boot 生态主流。
- 折中:保留 XML 做环境相关的外部化配置,业务类用
@Component/@Autowired,是大量遗留系统的现实形态。
六、启动报错定位(高频四例)
BeanDefinitionStoreException: IOException parsing XML document:找不到文件或路径不对,检查classpath:前缀与 Maven 资源是否被<excludes>掉。Could not resolve placeholder 'jdbc.url':property-placeholder 没生效或文件未被打包;可用ignore-unresolvable="true"与多个 placeholder 共存排查。NoSuchBeanDefinitionException:id/class 拼错,或component-scan的 base-package 写漏。- XSD 版本不匹配导致启动加载 schema 失败:离线环境应使用
https://.../spring-beans.xsd(Spring 自身会做本地映射),不要用带版本号的路径。
七、常见问题 FAQ
问:文件名必须叫 applicationContext.xml 吗?
答:不必,只要加载时路径写对即可;多文件可用 <import> 汇总。
问:lazy-init 什么时候用?
答:lazy-init="true" 让 bean 首次被请求才创建,可缩短启动时间,但会把配置错误推迟到运行时暴露,慎用于关键路径。
问:Spring Boot 还需要它吗?
答:基本不需要,用 @Configuration + @Bean;如需引入遗留 XML,可用 @ImportResource("classpath:applicationContext.xml")。
一句话:applicationContext.xml 就是 Spring 容器的“装配说明书”——声明 Bean、注入依赖、读外部配置、开启注解扫描与事务/AOP;新项目首选 JavaConfig/注解,遗留系统与需运维改配置的场景保留 XML 依然合理。
生产内容,同时打通搜索引擎(SEO)与AI搜索引擎(GEO)流量入口的软件! 点击进入365建站器 »
