我使用@PropertySource来配置一个配置类:
@Configuration
@PropertySource("classpath:/mongo-${env}.properties")
public class MongoConfiguration {mongo-${env}.properties文件位于类路径中。这个很好用。
我现在使用Spring将配置外部化到Git :所有application.yml文件都已被迁移。但是,我不知道是否可能,也不知道如何将属性文件外部化,比如@PropertySource中声明的属性文件。
我所做的:我试图在Git中将mongo-prod.properties重命名为application-prod.properties。然后我将@PropertySource更改为:
@PropertySource("file:///C://.../config-repo/application-prod.properties")它是存储库的本地副本。这是可行的,但这只是一个硬编码的解决方案。
有更清洁的解决方案吗?
发布于 2019-05-21 13:21:21
假设您正确地设置了配置服务器,则可以使用@ConfigurationProperties注释加载属性。
示例假设您的服务名为customer服务,并希望从配置服务器获取属性文件。
步骤1在您的git中添加客户属性。您还可以添加特定于环境的属性,如
customer-qa.properties ,customer-dev.properties现在要加载这些属性
@Component
@ConfigurationProperties("customer-service")
@Data
public class CustomerServiceConfigurations {
private String somePropertyname;
} 有关更多详细信息,请查看下面的示例配置服务器
https://stackoverflow.com/questions/56238158
复制相似问题