默认情况下,Spring Boot 中是不包含任何的 Spring 配置文件的,即使我们手动添加 Spring 配置文件到项目中,也不会被识别。那么 Spring Boot 项目中真的就无法导入 Spring 配置吗?答案是否定的。
Spring Boot 为了我们提供了以下 2 种方式来导入 Spring 配置:
- 使用 @ImportResource 注解加载 Spring 配置文件
- 使用全注解方式加载 Spring 配置
@ImportResource 导入 Spring 配置文件
在主启动类上使用 @ImportResource 注解可以导入一个或多个 Spring 配置文件,并使其中的内容生效。
1. 以 helloworld 为例,在 net.biancheng.www.service 包下创建一个名为 PersonService 的接口,代码如下。
- package net.biancheng.www.service;
- import net.biancheng.www.bean.Person;
- public interface PersonService {
- public Person getPersonInfo();
- }
2. 在 net.biancheng.www.service.impl 包下创建一个名为 PersonServiceImpl 的类,并实现 PersonService 接口,代码如下。
- package net.biancheng.www.service.impl;
- import net.biancheng.www.bean.Person;
- import net.biancheng.www.service.PersonService;
- import org.springframework.beans.factory.annotation.Autowired;
- public class PersonServiceImpl implements PersonService {
- @Autowired
- private Person person;
- @Override
- public Person getPersonInfo() {
- return person;
- }
- }
3. 在该项目的 resources 下添加一个名为 beans.xml 的 Spring 配置文件,配置代码如下。
- <?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="persionService" class="net.biancheng.www.service.impl.PersonServiceImpl"></bean>
- </beans>
4. 修改该项目的单元测试类 HelloworldApplicationTests ,校验 IOC 容器是否已经 personService,代码如下。
- package net.biancheng.www;
- import net.biancheng.www.bean.Person;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.context.ApplicationContext;
- @SpringBootTest
- class HelloworldApplicationTests {
- @Autowired
- Person person;
- //IOC 容器
- @Autowired
- ApplicationContext ioc;
- @Test
- public void testHelloService() {
- //校验 IOC 容器中是否包含组件 personService
- boolean b = ioc.containsBean("personService");
- if (b) {
- System.out.println("personService 已经添加到 IOC 容器中");
- } else {
- System.out.println("personService 没添加到 IOC 容器中");
- }
- }
- @Test
- void contextLoads() {
- System.out.println(person);
- }
- }
6. 运行单元测试代码,结果如下图。
图1:直接添加 Spring 配置到 Spring Boot 项目中
7. 在主启动程序类上使用 @ImportResource 注解,将 Spring 配置文件 beans.xml 加载到项目中,代码如下。
- package net.biancheng.www;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.context.annotation.ImportResource;
- //将 beans.xml 加载到项目中
- @ImportResource(locations = {"classpath:/beans.xml"})
- @SpringBootApplication
- public class HelloworldApplication {
- public static void main(String[] args) {
- SpringApplication.run(HelloworldApplication.class, args);
- }
- }
8. 再次执行测试代码,结果如下图。
图2:使用 @ImportResource 注解加载 Spring 配置文件结果
全注解方式加载 Spring 配置
Spring Boot 推荐我们使用全注解的方式加载 Spring 配置,其实现方式如下:
- 使用 @Configuration 注解定义配置类,替换 Spring 的配置文件;
- 配置类内部可以包含有一个或多个被 @Bean 注解的方法,这些方法会被 AnnotationConfigApplicationContext 或 AnnotationConfigWebApplicationContext 类扫描,构建 bean 定义(相当于 Spring 配置文件中的<bean></bean>标签),方法的返回值会以组件的形式添加到容器中,组件的 id 就是方法名。
以 helloworld 为例,删除主启动类的 @ImportResource 注解,在 net.biancheng.www.config 包下添加一个名为 MyAppConfig 的配置类,代码如下。
- package net.biancheng.www.config;
- import net.biancheng.www.service.PersonService;
- import net.biancheng.www.service.impl.PersonServiceImpl;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- /**
- * @Configuration 注解用于定义一个配置类,相当于 Spring 的配置文件
- * 配置类中包含一个或多个被 @Bean 注解的方法,该方法相当于 Spring 配置文件中的 <bean> 标签定义的组件。
- */
- @Configuration
- public class MyAppConfig {
- /**
- * 与 <bean id="personService" class="PersonServiceImpl"></bean> 等价
- * 该方法返回值以组件的形式添加到容器中
- * 方法名是组件 id(相当于 <bean> 标签的属性 id)
- */
- @Bean
- public PersonService personService() {
- System.out.println("在容器中添加了一个组件:peronService");
- return new PersonServiceImpl();
- }
- }
执行测试代码,执行结果如下图。
图3:全注解方式添加 Spring 配置