SpringBoot:@PropertySource&@ImportResource&@Bean

1. @PropertySource:加载指定的配置文件;

@ConfigurationProperties(prefix = “person”)默认从全局配置文件中获取值;

这样配置文件太大了,可以新建person.prpperties来存放与Person.java有关的配置文件
SpringBoot:@PropertySource&@ImportResource&@Bean
然后修改Person.java
SpringBoot:@PropertySource&@ImportResource&@Bean
运行Test
SpringBoot:@PropertySource&@ImportResource&@Bean

2. @ImportResource:导入Spring的配置文件,让配置文件里面的内容生效;

1.新建service.HelloService.java
2.新建beans.xml
SpringBoot:@PropertySource&@ImportResource&@Bean
SpringBoot:@PropertySource&@ImportResource&@Bean
3.编写test
SpringBoot:@PropertySource&@ImportResource&@Bean
Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;

想让Spring的配置文件生效,加载进来;把@ImportResource标注在一个配置类上
SpringBoot:@PropertySource&@ImportResource&@Bean

@ImportResource(locations = {“classpath:beans.xml”})
导入Spring的配置文件让其生效

再次运行test
SpringBoot:@PropertySource&@ImportResource&@Bean

3.SpringBoot推荐给容器中添加组件的方式;推荐使用全注解的方式

1、配置类@Configuration取代Spring配置文件
SpringBoot:@PropertySource&@ImportResource&@Bean
记得注释SpringBoot:@PropertySource&@ImportResource&@Bean
2、使用**@Bean给容器中添加组件
3、使用
test**查看
SpringBoot:@PropertySource&@ImportResource&@Bean