Spring boot之读取自定义配置文件

来源:互联网 发布:淘宝卖家参加聚划算 编辑:程序博客网 时间:2024/06/04 19:00

1,定义配置文件mypro.properties

#自定义配置base.redis.url=localhost:43434
2,在启动类上加上配置注解:

@PropertySource("classpath:mypro.properties")
public class Application {
}
这是定义要读取的properties文件的位置,另外,假如只读取该文件中的部分配置,可以在启动类上使用@ConfigurationProperties(prefix = "base")注解来指定读取的前缀。建议不用设置前缀而是读取所有。

3,在需要使用的类中使用@Value(${base.redis.url})注解注入配置信息

//获取配置信息@Value("${spring.datasource.url}")private String url;



阅读全文
0 0
原创粉丝点击