-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Add prefix support for environment variables #3450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I like that. |
+1 for this feature! |
Here is what I thought: At the I though about something like this: protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) {
...
if(this.environmentPrefix != null && !this.environmentPrefix.equals("")) {
filterSystemEnvironmentPropertySourceByPrefix(environment);
}
...
}
private void filterSystemEnvironmentPropertySourceByPrefix(ConfigurableEnvironment environment) {
final Map<String, Object> systemEnvironment = environment.getSystemEnvironment();
final Map<String, Object> prefixedSystemEnvironment = new HashMap<String, Object>(systemEnvironment.size());
for(String key : systemEnvironment.keySet()) {
if(key.startsWith(environmentPrefix)) {
prefixedSystemEnvironment.put(key.substring(environmentPrefix.length()), systemEnvironment.get(key));
}
}
environment.getPropertySources().replace(
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
new SystemEnvironmentPropertySource(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, prefixedSystemEnvironment));
} What do you think? There are any collateral damage doing this? I have tested myself and it seems o work quite well. If you want I can make a Gist or a PR with my code. |
Creates an environmentPrefix property that is used to filter the SystemEnvironmentPropertySource's map of environment variables and create a new one only with the prefixed properties. This new map is used on a new SystemEnvironmentPropertySource that replaces the former one. Fixes spring-projects#3450
Creates an environmentPrefix property that is used to filter the SystemEnvironmentPropertySource's map of environment variables and create a new one only with the prefixed properties. This new map is used on a new SystemEnvironmentPropertySource that replaces the former one. Fixes spring-projects#3450
Include a filter in ConfigurationPropertiesBindingPostProcessor that checks if the property "spring.property.prefix" exists (not blank) and filter the system properties ensuring that the properties with the prefix are available on property binding. Fixes spring-projects#3450
Include a filter in ConfigurationPropertiesBindingPostProcessor that checks if the property "spring.property.prefix" exists (not blank) and filter the system properties ensuring that the properties with the prefix are available on property binding. Fixes spring-projects#3450
Include a filter in ConfigurationPropertiesBindingPostProcessor that checks if the property "spring.property.prefix" exists (not blank) and filter the system properties ensuring that the properties with the prefix are available on property binding. Fixes spring-projects#3450
Include a filter in ConfigurationPropertiesBindingPostProcessor that checks if the property "spring.property.prefix" exists (not blank) and filter the system properties ensuring that the properties with the prefix are available on property binding. Fixes spring-projects#3450
Include a filter in ConfigurationPropertiesBindingPostProcessor that checks if the property "spring.property.prefix" exists (not blank) and filter the system properties ensuring that the properties with the prefix are available on property binding. Fixes spring-projects#3450
Include a filter in ConfigurationPropertiesBindingPostProcessor that checks if the property "spring.property.prefix" exists (not blank) and filter the system properties ensuring that the properties with the prefix are available on property binding. Fixes spring-projects#3450
Include a filter in ConfigurationPropertiesBindingPostProcessor that checks if the property "spring.property.prefix" exists (not blank) and filter the system properties ensuring that the properties with the prefix are available on property binding. Fixes spring-projects#3450
Include a filter in ConfigurationPropertiesBindingPostProcessor that checks if the property "spring.property.prefix" exists (not blank) and filter the system properties ensuring that the properties with the prefix are available on property binding. Fixes spring-projects#3450
I personally prefer something like that application.properties
Config.java @Bean
public MyBean myBean(@ConfigurationProperties(prefix="my.prefix") Environment env) {
env.getProperty("name");
.... Can it be implemented at this issue?? |
@mageddo This issue is specifically for adding a prefix to system environment variable. You example can't be implemented because we already support |
@philwebb, @snicoll, I've never submitted anything to the Spring project, so I was looking for some guidance before I submit my code for review so hopefully I get it right the first time. I found this page: https://github.com/spring-projects/spring-boot/wiki/Working-with-Git-branches and it seems like I need to pick the right branch to start with. I'm working off of the 2.1 branch because that's what my project is using. Is the 2.1 branch OK for this enhancement or would you like me to start it off of a different branch? |
@mcompton13 thanks for working on this. Please create a branch from |
We've renamed a lot of our own configuration properties since this was raised. The general shift to containers has also reduced the need for this feature. |
Please consider adding support for this. There are still plenty of Spring Boot based projects operating in environments which developers have little to no control over. I've been in this situation several times over the years and the ability to prefix the environment variables would make life easier. An example: customer insisting on deploying multiple Spring Boot applications to the same WildFly instance, with each application using its own database - which makes using |
Well that didn't last long :) I'll reopen it again. |
Yup. We have multiple deployments, some with docker and some on premise. For the latter, we created our own prefixed env variables of the most used spring ones and then used them as default values in the properties file. But that is very limiting and we cannot change others without rebuilding the application. |
Wow! It's been a few years since I first touched this. I'll give it a go later today! 👍 |
Some environments add some standard prefix when setting environment variables. As an example, the GitHub Actions ecosystem uses environment variables with an In my Spring Boot applications I have implemented a somewhat hacky work-around for supporting this |
Currently environment variables get bound to
@Configuration
properties directly (for exampleSERVER_PORT
toServer.port
). This can cause problems when trying to setignoreInvalidFields
or if you want to run two different boot apps as the same user.@gregturn suggested that we consider supporting a prefix when binding. For example:
Which would then bind
MYAPP_SERVER_PORT
rather thanSERVER_PORT
.The text was updated successfully, but these errors were encountered: