全部学科
Python全栈
python
NodeJS全栈
nodejs
小程序首页
📅 2026-05-18 8 分钟 ✍️ juanwangdev

XML配置

XML配置是Spring最早支持的配置方式,通过XML文件定义Bean及其依赖关系。

基础XML配置

配置文件结构

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.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- Bean定义 -->
</beans>

Bean定义

基本定义

XML
<!-- 最简单的Bean定义 -->
<bean id="userService" class="com.example.service.UserService"/>

<!-- 带属性的Bean -->
<bean id="dataSource" class="com.example.DataSource">
    <property name="url" value="jdbc:mysql://localhost:3306/db"/>
    <property name="username" value="root"/>
    <property name="password" value="123456"/>
</bean>

依赖注入

XML
<!-- 构造器注入 -->
<bean id="orderService" class="com.example.OrderService">
    <constructor-arg ref="userService"/>
    <constructor-arg ref="paymentService"/>
</bean>

<!-- Setter注入 -->
<bean id="orderService" class="com.example.OrderService">
    <property name="userService" ref="userService"/>
    <property name="timeout" value="30"/>
</bean>

集合注入

XML
<bean id="configBean" class="com.example.ConfigBean">
    <!-- List -->
    <property name="servers">
        <list>
            <value>server1</value>
            <value>server2</value>
            <ref bean="server3"/>
        </list>
    </property>

    <!-- Map -->
    <property name="properties">
        <map>
            <entry key="timeout" value="30"/>
            <entry key="retry" value="3"/>
        </map>
    </property>

    <!-- Set -->
    <property name="tags">
        <set>
            <value>tag1</value>
            <value>tag2</value>
        </set>
    </property>
</bean>

自动装配

XML
<!-- byType:按类型自动装配 -->
<bean id="orderService" class="com.example.OrderService" autowire="byType"/>

<!-- byName:按名称自动装配 -->
<bean id="orderService" class="com.example.OrderService" autowire="byName"/>

<!-- constructor:构造器自动装配 -->
<bean id="orderService" class="com.example.OrderService" autowire="constructor"/>

Bean作用域

XML
<!-- singleton(默认) -->
<bean id="userService" class="com.example.UserService" scope="singleton"/>

<!-- prototype -->
<bean id="prototypeBean" class="com.example.PrototypeBean" scope="prototype"/>

<!-- request/session(Web环境) -->
<bean id="requestBean" class="com.example.RequestBean" scope="request"/>

生命周期回调

XML
<bean id="lifecycleBean"
      class="com.example.LifecycleBean"
      init-method="init"
      destroy-method="destroy"
      lazy-init="true"/>

导入其他配置文件

XML
<!-- 导入其他XML文件 -->
<import resource="classpath:service.xml"/>
<import resource="classpath:dao.xml"/>
<import resource="file:/external/config.xml"/>

注解扫描

XML
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.example"/>

<!-- 只扫描@Controller -->
<context:component-scan base-package="com.example">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>

加载XML配置

Java
// 从classpath加载
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

// 从文件系统加载
ApplicationContext context = new FileSystemXmlApplicationContext("/path/to/applicationContext.xml");

// 加载多个文件
ApplicationContext context = new ClassPathXmlApplicationContext(
    "service.xml", "dao.xml", "applicationContext.xml"
);

XML配置优缺点

优点缺点
配置集中管理配置冗长
修改无需重新编译类型不安全
与代码分离IDE支持较弱
适合遗留系统不符合现代开发习惯

要点总结

  1. XML配置通过标签定义Bean
  2. constructor-arg用于构造器注入,property用于Setter注入
  3. autowire属性可开启自动装配
  4. import标签可导入其他XML配置文件
  5. context:component-scan开启注解扫描,与注解配合使用

📝 发现内容有误?点击此处直接编辑

← 上一篇 Java配置
下一篇 → 注解配置
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库