初学Springboot2(四)---配置文件后续之@PropertySource&@ImportResource和配置文件占位符

1.@PropertySource

@PropertySource:加载指定配置文件

上篇所讲的@ConfigurationProperties默认是从全局配置文件来获取值的,但是如果我将所有配置信息都写在全局配置文件中,就会变得很臃肿,所以把和springboot无关的配置提取出来

在resources资源文件夹下,新建一个person.properties

person.last-name=李四
person.age=18
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=12
person.lists=a,b,c
person.dog.name=dog,
person.dog.age=15
@PropertySource(value={"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
//@Validated
public class Person {
    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;

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

运行测试类:发现输出的是全局配置文件中的person信息,而不是person.properties的信息,当我们删除全局配置中的person信息,才能正确输出。
总结:在没有添加@PropertySource注解时,@ConfigurationProperties会从全局配置文件获取值,当全局配置文件中没有对应的值时,就会赋值为null

Person{lastName='null', age=null, boss=null, birth=null, maps=null, lists=null, dog=null}

当添加了@PropertySource后,如果全局配置文件中有person的值,它不会管@PropertySource指定配置文件中person的值,优先将全局配置文件的值先获得,赋给person,如果有缺少,再从@PropertySource指定配置文件中寻找,就相当于全局配置文件的值覆盖了指定的值。如果全局配置文件没有person的信息,那当然就直接从指定的配置文件中获取

2.@ImportResource

@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效;SpringBoot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别。想让Spring的配置文件生效,加载进来,要把@ImportResource标注再一个配置类上

例:
创建一个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.example.service.HelloService"></bean>
</beans>

创建一个service

package com.example.service;

public class HelloService {

}

再测试类定义容器,判断这个service是否被注入容器

    @Autowired
    ApplicationContext ioc;

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

执行,输出false

添加@ImportResource注解

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

    public static void main(String[] args) {

        SpringApplication.run(Helloworld01Application.class, args);
    }

}

再执行测试类,就会输出true

但是这种方式太麻烦了,先编写一个组件,再导进来。
SpringBoot推荐给容器添加组件的方式:
不来编写Spring的配置文件,推荐使用注解**#Bean**
先将@ImportResource注释掉

例:
创建一个config包,创建MyAppConfig类

package com.example.config;

import com.example.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Configuration:  指明当前类是一个配置类;就是来替代之前的Spring配置文件
 * 之前在配置文件中使用<bean></bean>标签来添加组件
 */
@Configuration
public class MyAppConfig {

    //@Bean:将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
    @Bean
    public HelloService helloService02(){
        System.out.println("配置类@Bean给容器添加组件");
        return new HelloService();
    }

}

测试类

    @Autowired
    ApplicationContext ioc;

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

输出true

3.配置文件占位符

  • RandomValuePropertySource:配置文件可以使用随机数
    ${random.value}…
  • 属性配置占位符
person.last-name=李四${random.uuid}
person.dog.name=${person.last-name}_dog,
--------
person.dog.name=${person.test:aa}_dog,

当 person.test取不到值时,就取aa,输出aa_dog

person.dog.name=${person.test:aa}_dog,

输出 ${person.test}_dog

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值