-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Enhance PropertySources to be profile aware #12822
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
We discourage you from using If I understand you properly, you're looking for an API that does what we do for standard |
Yes, I want to be able to re-use the logic for what we call cascaded property loading (loading application.properties, application-profile1.properties, application-profile2.properties, etc.) for more than just application properties. This is primarily for library development, where a library includes within its jar some properties files. We want the library to also be able to have its properties loaded based on the currently active profiles. For a more concrete example, imagine you are in a cloud/microservice arrangement where you own a service (ServiceA), and you want to communicate with some other service (ServiceB). Also imagine that each service has multiple copies of their deployment for different levels of testing, and that each of those deployments is given a profile. For the sake of example, lets say these deployments are each given profiles of If we are using something like Eureka+Ribbon, there are a number of configuration properties ServiceA would need to have to communicate with ServiceB. To reduce copy/paste, ServiceB might publish those configurations in a client jar, with an AutoConfiguration which loads those properties. The ServiceB library will want to provide different properties based on the environment/profile the code is running in. The jar will contain ServiceA would import this library, and when running with the |
Environment dependent configurations in a library? I consider this an anti-pattern and feels really odd to me. Maybe you should take a look into spring-cloud-config... |
😆 considering we modeled config server after one of their services 😉 |
Yep, we absolutely have a remote config server, although we consider using it for more than temporary overrides to be an anti-pattern as putting all config there creates a central point of failure. If your app can't run correctly without the config-server being available, it'll be a very real problem when the config backend inevitably goes down. So we very much still package default configurations in libraries. And we scope those to deployment considerations like Test/Prod/Staging, AWSAccount, AWS Region, etc. As we've adapted this to the Spring Boot world, we've found Profiles are a pretty good implementation for this concept. But the fact that only application properties are profile aware, while no other PropertySource has the same behavior doesn't quite match our expectations. |
We would also find this useful. We have a Spring Boot app that uses Spring Batch. This in turn uses a few nested contexts that load Spring XML files. (Nested contexts are a feature of Spring Batch to get around bean name clashes.) This means we have a main Spring Boot context (using the default property resolution) as well as nested contexts (which need to rely on @propertysource). |
I think we should close this one for now and see how the new
We can always reopen this one if the above is still too cumbersome. |
This comment has been minimized.
This comment has been minimized.
From my perspective this would still be useful feature to consider. For example we have some stack of base libraries across all technologies we use in the project. For example, we have a base library for mariadb, based on spring data. Besides it provides some dependencies configuration and plumbing code to be used in all services using mariadb in our project, we also want it to provide some basic configuration preset, which should then be used by all services, without repeating it in spring:
datasource:
driver-class-name: org.mariadb.jdbc.Driver
username: ${maria.user}
password: ${maria.pass}
url: jdbc:mariadb://${maria.host}:${maria.port}/${maria.db}
jpa:
properties:
hibernate:
schema_update:
unique_constraint_strategy: RECREATE_QUIETLY
dialect: org.hibernate.dialect.MariaDB10Dialect So, basically, we don't want to repeat all this stuff in each service, we just want it to be provided by the lib, and the service config is limited only to this: maria:
user: ...
pass: ...
db: ... This can be done using In my opinion There's somewhere in Spring a properly working code, supporting yamls, profiles, new config file processing etc. Why not to use the same code under BTW here is my working workaround to achieve the same results in current spring implementation. |
It's unfortunately going to be quite hard to change Even then, it's going to be a little tricky because (as you've discovered) it's difficult to get the environment into the loader. We might be able to make a framework change so that Flagging for team discussion to see if there are any other ideas that would help us extend |
@l0co One other abstraction that you might be able to use is |
I can understand that this feature is a part of the framework and is not possible to be introduced within Spring Boot only. In this scenario I can't see what can be done more about the subject. Maybe just some explicite examples of how to achieve it, maybe as the answer to the mentioned stackoverflow thread where people are directed first, looking for the solution to this problem in google. It can be either my way, using An maybe, looking for example on my solution, if this kind of |
Uh oh!
There was an error while loading. Please reload this page.
Boot's feature of loading
application-${profile}.properties
/yml
is excellent, but it would be great to see this extended into otherPropertySource
s, such as those provided byEnvironmentPostProcessor
or@PropertySource
. This essentially would be offering profile awareness to library implementations who provide properties to the application. Similar options are available in https://github.com/Netflix/archaius/tree/2.x to manage profile based config loading from application or library.Seeking opinions on:
ResourcePropertySource
along the lines ofProfileAwareResourcePropertySource
which could be used in places likeEnvironmentPostProcessor
@ProfileAwarePropertySource
to achieve the same.ProfileAwareEnvironmentPostProcessor
to essentially apply the profile loading behavior to all existingPropertySource
s.Example:
@PropertySource("myprops")
with profilefoo
should result in twoPropertySource
s added to theEnvironment
,myprops.properties
andmyprops-foo.properties
.myprops-foo.properties
should be added directly beforemyprops.properties
such that the more specific configurations override the less specific ones. This should be extended to include multiple profiles, with the ordering profiles being applied reflecting their precedence.This would give us options for enabling this feature explicitly per
PropertySource
or globally. Any thoughts?The text was updated successfully, but these errors were encountered: