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支持较弱 |
| 适合遗留系统 | 不符合现代开发习惯 |
要点总结
- XML配置通过标签定义Bean
- constructor-arg用于构造器注入,property用于Setter注入
- autowire属性可开启自动装配
- import标签可导入其他XML配置文件
- context:component-scan开启注解扫描,与注解配合使用
📝 发现内容有误?点击此处直接编辑