熟悉并使用Spring框架 - XML篇
文章目录
- 前言
- 什么是Spring的XML配置
- 基本环境搭建
- XML配置文件的基本结构
- Bean的定义与注入
- 简单Bean定义
- 构造器注入
- Bean之间的依赖注入
- Bean的作用域
- 集合类型的注入
- 实际使用示例
- 小结
前言
Spring框架作为Java开发中最流行的框架之一,为我们提供了多种配置方式。虽然现在注解和Java配置越来越流行,但XML配置作为Spring的经典配置方式,对于初学者和对底层源码感兴趣的同学,学习XML配置仍然是非常重要的一环。
什么是Spring的XML配置
Spring的XML配置是通过XML文件来定义Bean以及它们之间关系的一种方式。这种配置方式直观明了,所有的配置信息都集中在XML文件中,便于管理和维护。
基本环境搭建
首先我们需要在项目中引入Spring的相关依赖。
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.21</version>
</dependency>
XML配置文件的基本结构
一个典型的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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- Bean的定义 --></beans>
Bean的定义与注入
简单Bean定义
public class User {private String name;public User() {System.out.println("UserService被创建了");}public void setName(String name) {this.name = name;}public void sayHello() {System.out.println("Hello, " + name);}
}
在XML中定义这个Bean:
<bean id="userService" class="com.coldscholor.UserService"><property name="name" value="张三"/>
</bean>
构造器注入
如果我们的类有带参数的构造器:
public class Order {private String orderName;public Order(String orderName) {this.orderName = orderName;}public void processOrder() {System.out.println("处理订单:" + orderName);}
}
XML配置如下:
<bean id="orderService" class="com.coldscholor.OrderService"><constructor-arg value="订单001"/>
</bean>
Bean之间的依赖注入
当一个Bean需要依赖另一个Bean时:
public class UserController {private UserService userService;public void setUserService(UserService userService) {this.userService = userService;}public void handleRequest() {userService.sayHello();}
}
XML配置:
<bean id="userService" class="com.coldscholor.UserService"><property name="name" value="张三"/>
</bean><bean id="userController" class="com.coldscholor.UserController"><property name="userService" ref="userService"/>
</bean>
Bean的作用域
Spring支持多种Bean作用域,最常用的是:
<!-- 单例模式(默认) -->
<bean id="singletonBean" class="com.coldscholor.MyBean" scope="singleton"/><!-- 原型模式,每次获取都创建新实例 -->
<bean id="prototypeBean" class="com.coldscholor.MyBean" scope="prototype"/>
集合类型的注入
有时候我们需要注入集合类型的数据:
public class ConfigService {private List<String> list;private Map<String, String> map;}
XML配置:
<bean id="configService" class="com.coldscholor.ConfigService"><property name="list"><list><value>aaa</value><value>bbb</value><value>ccc</value></list></property><property name="map"><map><entry key="database.url" value="jdbc:mysql://localhost:3306/test"/><entry key="database.user" value="root"/></map></property>
</bean>
实际使用示例
让我们看一个完整的例子。首先创建测试类:
public class Application {public static void main(String[] args) {// 加载XML配置文件ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");// 获取BeanUserController controller = context.getBean("userController", UserController.class);controller.handleRequest();// 关闭容器((ClassPathXmlApplicationContext) context).close();}
}
完整的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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="userService" class="com.coldscholor.UserService"><property name="name" value="coldscholor"/></bean><bean id="userController" class="com.coldscholor.UserController"><property name="userService" ref="userService"/></bean></beans>
小结
XML配置虽然看起来比较繁琐,但它提供了很好的灵活性和可配置性。对于一些配置较为复杂或者需要运行时修改配置的场景,XML配置仍然是一个不错的选择。当然,现在更多的项目是SpringBoot项目会选择注解配置,但是我认为我们还是需要了解XML配置,有助于我们更好地理解Spring的工作原理,也能帮助我们维护一些老项目。