Spring Boot导入Spring配置

默认情况下,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 的接口,代码如下。

 
  1. package net.biancheng.www.service;
  2. import net.biancheng.www.bean.Person;
  3. public interface PersonService {
  4. public Person getPersonInfo();
  5. }

 
2. 在 net.biancheng.www.service.impl 包下创建一个名为 PersonServiceImpl 的类,并实现 PersonService 接口,代码如下。

 
  1. package net.biancheng.www.service.impl;
  2. import net.biancheng.www.bean.Person;
  3. import net.biancheng.www.service.PersonService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. public class PersonServiceImpl implements PersonService {
  6. @Autowired
  7. private Person person;
  8. @Override
  9. public Person getPersonInfo() {
  10. return person;
  11. }
  12. }


3. 在该项目的 resources 下添加一个名为 beans.xml 的 Spring 配置文件,配置代码如下。

 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean id="persionService" class="net.biancheng.www.service.impl.PersonServiceImpl"></bean>
  6. </beans>


4. 修改该项目的单元测试类 HelloworldApplicationTests ,校验 IOC 容器是否已经 personService,代码如下。

 
  1. package net.biancheng.www;
  2. import net.biancheng.www.bean.Person;
  3. import org.junit.jupiter.api.Test;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import org.springframework.context.ApplicationContext;
  7. @SpringBootTest
  8. class HelloworldApplicationTests {
  9. @Autowired
  10. Person person;
  11. //IOC 容器
  12. @Autowired
  13. ApplicationContext ioc;
  14. @Test
  15. public void testHelloService() {
  16. //校验 IOC 容器中是否包含组件 personService
  17. boolean b = ioc.containsBean("personService");
  18. if (b) {
  19. System.out.println("personService 已经添加到 IOC 容器中");
  20. } else {
  21. System.out.println("personService 没添加到 IOC 容器中");
  22. }
  23. }
  24. @Test
  25. void contextLoads() {
  26. System.out.println(person);
  27. }
  28. }


6. 运行单元测试代码,结果如下图。
 

Spring Boot  添加 Spring 项目


图1:直接添加 Spring 配置到 Spring Boot 项目中


7. 在主启动程序类上使用 @ImportResource 注解,将 Spring 配置文件 beans.xml 加载到项目中,代码如下。

 
  1. package net.biancheng.www;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.context.annotation.ImportResource;
  5. //将 beans.xml 加载到项目中
  6. @ImportResource(locations = {"classpath:/beans.xml"})
  7. @SpringBootApplication
  8. public class HelloworldApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(HelloworldApplication.class, args);
  11. }
  12. }


8. 再次执行测试代码,结果如下图。

Spring Boot 添加配置文件


图2:使用 @ImportResource 注解加载 Spring 配置文件结果

全注解方式加载 Spring 配置

Spring Boot 推荐我们使用全注解的方式加载 Spring 配置,其实现方式如下:

  1. 使用 @Configuration 注解定义配置类,替换 Spring 的配置文件;
  2. 配置类内部可以包含有一个或多个被 @Bean 注解的方法,这些方法会被 AnnotationConfigApplicationContext 或 AnnotationConfigWebApplicationContext 类扫描,构建 bean 定义(相当于 Spring 配置文件中的<bean></bean>标签),方法的返回值会以组件的形式添加到容器中,组件的 id 就是方法名。


以 helloworld 为例,删除主启动类的 @ImportResource 注解,在 net.biancheng.www.config 包下添加一个名为  MyAppConfig 的配置类,代码如下。

 
  1. package net.biancheng.www.config;
  2. import net.biancheng.www.service.PersonService;
  3. import net.biancheng.www.service.impl.PersonServiceImpl;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. /**
  7. * @Configuration 注解用于定义一个配置类,相当于 Spring 的配置文件
  8. * 配置类中包含一个或多个被 @Bean 注解的方法,该方法相当于 Spring 配置文件中的 <bean> 标签定义的组件。
  9. */
  10. @Configuration
  11. public class MyAppConfig {
  12. /**
  13. * 与 <bean id="personService" class="PersonServiceImpl"></bean> 等价
  14. * 该方法返回值以组件的形式添加到容器中
  15. * 方法名是组件 id(相当于 <bean> 标签的属性 id)
  16. */
  17. @Bean
  18. public PersonService personService() {
  19. System.out.println("在容器中添加了一个组件:peronService");
  20. return new PersonServiceImpl();
  21. }
  22. }


执行测试代码,执行结果如下图。
 

Spring Boot 全注解方式添加Spring 配置


图3:全注解方式添加 Spring 配置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值