简体   繁体   English

Spring Boot 使用 PropertySource 为每个环境加载配置

[英]Spring boot Load configuration per environment using PropertySource

I'm trying to add authorization to several microservices.我正在尝试为多个微服务添加授权。 Given all the services share similar authorization process, I want to extract the logic to a shared library.鉴于所有服务共享相似的授权过程,我想将逻辑提取到共享库中。

I managed to create library, but I realise all the configurations need to be set in the application.yml file in the microservice which calls the library.我设法创建了库,但我意识到所有配置都需要在调用库的微服务中的application.yml文件中进行设置。 I don't want to expose some of the configurations at service layer though.不过,我不想在服务层公开一些配置。

After some searches, I found I could set @PropertySource("library.properties") in my library's configuration class to force reading properties from the specified .properties file within the library.经过一些搜索,我发现我可以在库的配置类中设置@PropertySource("library.properties")以强制从库中指定的.properties文件读取属性。

The problem now is I want to set different values for different environments, eg authorization URL for test and production would be different.现在的问题是我想为不同的环境设置不同的值,例如测试和生产的授权 URL 会不同。 How can I configure the file so that the configuration class would read same property value based on active profile (eg environment = test/staging/production)?如何配置文件,以便配置类可以根据活动配置文件读取相同的属性值(例如环境 = 测试/暂存/生产)?

您可以在资源文件夹中拥有多个属性文件,例如“application-environment.yml”。Spring 框架会根据活动配置文件选择正确的文件。例如,如果您定义了“暂存”环境并具有暂存配置文件,然后您的属性文件应命名为 application-staging.yml。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM