Spring Boot 配置文件(一)

Springboot的默认配置文件是application.properties,这个后缀很长,也可以设置成application.yml,同样的效果

  注意,后缀可以改,文件名“application”不能改,并且要遵循yml语法,在冒号后必须要有个空格(语法要求)

my.name=Isea533
my.port=8080
my.servers[0]=dev.bar.com
my.servers[1]=foo.bar.com
@ConfigurationProperties(prefix="my")
public class Config {
    private String name;
    private Integer port;
    private List<String> servers = new ArrayList<String>();

    public String geName(){
        return this.name;
    }

    public Integer gePort(){
        return this.port;
    }
    public List<String> getServers() {
        return this.servers;
    }
}
//Spring Boot有如下几种注入方式
    //注入普通字符串
    @Value("Testing String inject")
    private String normal;

    //注入系统属性
    @Value("#{systemProperties['os.name']}")
    private String osName;

    //注入表达式结果
    @Value("#{ T(java.lang.Math).random()}")
    private double randomNumber;

    //注入其他bean属性
    @Value("#{demoService.another}")
    private String fromAnother;

    //注入文件资源
    @Value("classpath:test2.txt")
    private Resource testFile;

    //注入网址资源
    @Value("http://www.baidu.com")
    private Resource testUrl;

    //指定Properties文件注入并注入值
    @Value("${book.name}")
    private String bookName;

    //PropertySource注入需要指定
    @Autowired
    private Environment environment;

    /**
     * 1.多种配置注入,包括了注入普通字符串,注入系统属性,注入表达式结果,注入其他bean属性,注入文件资源,注入网址资源,指定Properties文件注入并注入值,PropertySource注入需要指定,PropertySource注入需要实现。
     *
     * 2. 根据需求,可将应用配置配置到txt文件中,再已注入的方式注入到Service中,供后期代码使用。
     */

 

posted @ 2018-01-19 14:03  猴子1  阅读(192)  评论(0编辑  收藏  举报