6、【网摘】Spring Boot导入Spring配置

6、【网摘】Spring Boot导入Spring配置

默认情况下,Spring Boot 中是不包含任何的 Spring 配置文件的,即使我们手动添加 Spring 配置文件到项目中,也不会被识别。那么 Spring Boot 项目中真的就无法导入 Spring 配置吗?答案是否定的。

Spring Boot 为了我们提供了以下 2 种方式来导入 Spring 配置:

使用 @ImportResource 注解加载 Spring 配置文件使用全注解方式加载 Spring 配置

1、@ImportResource 导入 Spring 配置文件

在主启动类上使用 @ImportResource注解可以导入一个或多低价接各类项目系统搭建点我wcqh.cn个 Spring 配置文件,并使其中的内容生效。

1、以 helloworld 为例,在 net.biancheng.www.service 包下创建一个名为 PersonService 的接口,代码如下。

package net.biancheng.www.service;import net.biancheng.www.bean.Person;publicinterfacePersonService{publicPerson getPersonInfo();}

2、在 net.biancheng.www.service.impl 包下创建一个名为 PersonServiceImpl 的类,并实现 Pe低价接各类项目系统搭建点我wcqh.cnrsonService 接口,代码如下。

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;publicclassPersonServiceImplimplementsPersonService{@AutowiredprivatePerson person;@OverridepublicPe低价接各类项目系统搭建点我wcqh.cnrson getPersonInfo(){return person;}}

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

<?xml version=“1.0” encoding=“UTF-8”?><beansxmlns=“http://www.springframework.org/schema/beans”xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”xsi:schemaLocation=“http://www.springframework.org/schema/bea低价接各类项目系统搭建点我wcqh.cnns http://www.springframework.org/schema/beans/spring-beans.xsd”><beanid=“personService”class=“net.biancheng.www.service.impl.PersonServiceImpl”></bean></beans>

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

package net.biancheng.www;import net.biancheng.www.bean.Person;impo低价接各类项目系统搭建点我wcqh.cnrt org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.context.ApplicationContext;@SpringBootTestclassHelloworldApplicationTests{@AutowiredPerson person;//IOC 容器@AutowiredApplicatio低价接各类项目系统搭建点我wcqh.cnnContext ioc;@Testpublicvoid testHelloService(){//校验 IOC 容器中是否包含组件 personServiceboolean b = ioc.containsBean(“personService”);if(b){System.out.println(“personService 已经添加到 IOC 容器中”);}else{System.out.println(“personService 没添加到 IOC 容器中”);}}@Testvoid contextLoads(){System.out.println(person);}}

5、运行单元测试代码,结果低价接各类项目系统搭建点我wcqh.cn如下图。

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

6、在主启动程序类上使用 @ImportResource 注解,将 Spring 配置文件 beans.xml 加载到项目中,代码如下。 package net.biancheng.www;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.ImportReso低价接各类项目系统搭建点我wcqh.cnurce;//将 beans.xml 加载到项目中@ImportResource(locations ={“classpath:/beans.xml”})@SpringBootApplicationpublicclassHelloworldApplication{publicstaticvoid main(String[] args){SpringApplication.run(HelloworldApplication.class, args);}}

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

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

2、全注解方式加载 Spring 配置

Spri低价接各类项目系统搭建点我wcqh.cnng Boot 推荐我们使用全注解的方式加载 Spring 配置,其实现方式如下:

1、使用 @Configuration

注解定义配置类,替换 Spring 的配置文件;

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

以 helloworld 为例,删除主启动类的 @低价接各类项目系统搭建点我wcqh.cnImportResource 注解,在 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.annotati低价接各类项目系统搭建点我wcqh.cnon.Configuration;/*** @Configuration 注解用于定义一个配置类,相当于 Spring 的配置文件* 配置类中包含一个或多个被 @Bean 注解的方法,该方法相当于 Spring 配置文件中的 <bean> 标签定义的组件。*/@ConfigurationpublicclassMyAppConfig{/** * 与 <bean id=”personService” class=”PersonServiceImpl”></bean> 等价 * 该方法返回值以组件的形式添加到容器中 * 方法名是组件 id(相当于 <bean> 标签的属性 id) */@Beanpublic低价接各类项目系统搭建点我wcqh.cnPersonService personService(){System.out.println(“在容器中添加了一个组件:peronService”);returnnewPersonServiceImpl();}}

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

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

加入

QQ群:722461036

微信群:一起督促、学习、练习、温习、复习 ~ ~ ~
作者最新博文
2021-03-10 21:42:47

© 版权声明
THE END
喜欢就支持一下吧
点赞791 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容