SpringBoot项目实现jar包外配置文件管理

应用场景

我们除了使用SpringBoot应用的启动配置文件外,还会有一些自定义的配置文件。如果都放到SpringBoot的启动配置文件中会太乱,所以将配置文件外置可以帮助我们更好的管理配置。
SpringBoot配置文件优先级:https://www.jianshu.com/p/1a18752d2a7b

解决

使用@PropertySource注解指定自定义的配置文件位置

  1. file:./config,在外部的config目录下
  2. file:./,在外部的目录下
  3. file:,jar包所在的目录下
  4. classpath:/config/,resources/config下
  5. classpath:/,resources下

实践

如果想要在jar包同级的目录中放置配置文件,即下图所示。可以按照以下代码进行处理。

@Data
@Component
@PropertySource(value = "file:config.yml", factory = YamlPropertySourceFactory.class)//指定文件位置
@ConfigurationProperties(prefix = "config")
public class MyConfig {

    private String host;

    private String port;

    private String uri;
}
posted @   云雀L  阅读(606)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示