使用自定义配置文件

来源:互联网 发布:炉石传说 for mac 编辑:程序博客网 时间:2024/05/18 02:13
有时候我们不希望把所有配置都放在application.properties里面,这时候我们可以另外定义一个,这里我明取名为test.properties,路径跟也放在src/main/resources下面。
com.aa.name="aa~"
com.aa.want="祝大家鸡年,大吉吧"
我们新建一个bean类,如下:
@Configuration
@ConfigurationProperties(prefix = "com.aa") 
@PropertySource("classpath:test.properties")
public class ConfigTestBean {
    private String name;
    private String want;
    // 省略getter和setter
}
这里要注意,有一个问题,如果你使用的是1.5以前的版本,那么可以通过locations指定properties文件的位置,这样:
@ConfigurationProperties(prefix = "config2",locations="classpath:test.properties")
但是1.5版本后就没有这个属性了,找了半天发现添加@Configuration和@PropertySource(“classpath:test.properties”)后才可以读取。
0 0
原创粉丝点击