@ConfigurationProperties(prefix = “person”)默认从全局配置文件中获取值;(application.properties中写好与类相关的属性 然后两者进行绑定 获取值)
1. 问题:但是把所有的东西都配在全局配置文件中,那么配置文件就太大了

2. 解决办法:
可以把与SpringBoot无关的配置给提取出来 我们在resources包下创建一个新的File配置文件,名为person.properties,把全局配置文件中与person有关的注释掉,然后复制粘贴到person.properties中
结果如下图:

person.last-name=王五
person.age=12
person.boos=false
person.birth=2017/12/12
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=xiaosan
person.dog.age=2

在容器中安装桌面 容器中如何添加组件_配置文件


3. 存在问题:

@ConfigurationProperties(prefix = “person”)的含义是从全局配置文件中以person开头的值 ,但是全局配置文件中的内容注释掉了 加载不到内容

4. 解决办法:

使用@PropertySource(参数:路径) : 读取指定的配置文件

注意:@ConfigurationProperties(prefix = “person”)语句不能删除

@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person {

    private String lastName;
    private Integer age;
    private Boolean boos;
    private Date birth;

    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

5. 测试结果显示:

在容器中安装桌面 容器中如何添加组件_在容器中安装桌面_02


6. @ImportResource:

作用: 导入Spring的配置文件,让配置文件里面的内容生效;

比如: 给容器中加入一个组件,除了那些注解的方式,以前写的都是Spring配置文件,SpringMVC配置文件。

步骤如下:

6.1

resources-》new-》XML Configuration File -》 Spring Config -》名为:beans.xml

在容器中安装桌面 容器中如何添加组件_spring_03


6.2

在这个配置文件中加入一个组件,比如在study包下创建一个类名为:service.HelloService组件(自动创建service包)6.3

然后在Spring配置文件中加载进来,输入Hello 点击第一个即可

在容器中安装桌面 容器中如何添加组件_xml_04


6.4

然后在bean中给一个id=HelloService
那么,现在容器中有没有这个helloService,是不是写一个Spring配置文件就自动识别?
那么我们写一个代码测试一下:

@Autowired
    ApplicationContext ioc;

	@Test
    public void testHelloService(){
        boolean b = ioc.containsBean("helloService");
        System.out.println(b);
    }

6.5

结果显示:

在容器中安装桌面 容器中如何添加组件_配置文件_05


意味着:

Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别

问题:

如何让Spring的配置文件生效,加载进来

解决办法:

@ImportResource标注在一个配置类上(就标注在主配置类上,main)
@ImportResource(locations = {“classpath:beans.xml”})

@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class SpringBoot01HellowolrdQuickApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBoot01HellowolrdQuickApplication.class, args);
    }

}

然后在到Test类中测试结果 去看看容器中有没有这个beans
结果显示:true

在容器中安装桌面 容器中如何添加组件_spring_06


6.6

通过上述操作内容:@ImportResource导入Spring的配置文件让其生效

6.7

后来产生的问题:

但是在后来开发的时候,我们不可能给容器中加组件写一个配置文件然后在把配置文件加载进来,这样显得很麻烦,很繁琐。

编写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="helloService" class="com.study.springboot01hellowolrdquick.service.HelloService">  </bean>
</beans>

7 优化:

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

7.1 首先写一个配置类(类似于Spring配置文件)

步骤如下:

study包下-》new class-》类名:config.MyAppConfig(自动创建包config)

在容器中安装桌面 容器中如何添加组件_spring_07

但是要成为一个配置类:要加上一个注解@Configuration(指名当前类是一个配置类;就是来替代之前的Spring配置文件)
添加的方法:在配置文件中用 标签添加组件

7.2 使用@Bean给容器中添加组件
写一个方法,将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名;

@Configuration
public class MyAppConfig {
    @Bean
    public HelloService helloService(){
        System.out.println("配置类@Bean给容器中添加组件了");
        return new HelloService();
    }
}

我们把前面的ImportResuorce注释掉 ,相当于不导入配置文件了
目的是:测试使用的配置类给容器是否添加了一个组件

7.3 测试运行一下结果:

在容器中安装桌面 容器中如何添加组件_xml_08