SpringBoot项目实现jar包外配置文件管理
应用场景
我们除了使用SpringBoot应用的启动配置文件外,还会有一些自定义的配置文件。如果都放到SpringBoot的启动配置文件中会太乱,所以将配置文件外置可以帮助我们更好的管理配置。
SpringBoot配置文件优先级:https://www.jianshu.com/p/1a18752d2a7b
解决
使用@PropertySource注解指定自定义的配置文件位置
- file:./config,在外部的config目录下
- file:./,在外部的目录下
- file:,jar包所在的目录下
- classpath:/config/,resources/config下
- 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;
}

浙公网安备 33010602011771号