-
Notifications
You must be signed in to change notification settings - Fork 38.4k
Introduce ability to add to the Environment
using properties of a bean
#32209
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
Environment
using properties of a bean
Hi @rwinch, Thanks for the proposal! 👍 We discussed this topic within the team, and we are hesitant to introduce such a feature that is declarative and relies on SpEL expressions. However, we came up with a counterproposal: register a I put together a proof of concept in the following feature branch. main...sbrannen:spring-framework:issues/gh-32209-register-DynamicPropertyRegistry-as-bean Take a look at The basic idea can be seen in this @Bean
ApiServer apiServer(DynamicPropertyRegistry registry) {
ApiServer apiServer = new ApiServer();
registry.add("api.url", apiServer::getUrl);
return apiServer;
} That follows the same programming model as a The The Thus, the
However, if we don't make use of I suppose your proposal for Spring Boot Testjars exhibits the same behavior, but if that's not the case, please let us know. In any case, please let us know what you think of our counterproposal. Cheers, Sam p.s. @philwebb, you may also be interested in this, especially since I took some inspiration from Boot's |
Thank you @sbrannen!
Can you help me to understand the reason that a declarative feature should not be using SpEL? I ask because we can use SpEL with
I think that the main piece that is missing with the counter proposal is an easy way for the framework to easily provide well known configurations. For example, to specify the issuer URI in configuration this proposal needs to be: @Bean
static CommonsExecWebServerFactoryBean authorizationServer(DynamicPropertyRegistry registry) {
CommonsExecWebServerFactoryBean result = ...
// NOTE: getObject throws Exception which doesn't work with Supplier
registry.add("spring.security.oauth2.client.provider.spring.issuer-uri", () -> "http://127.0.0.1:" + result.getObject().getPort());
return result;
} With the current solution, users can do this @Bean
@OAuth2ClientProviderIssuerUri
static CommonsExecWebServerFactoryBean authorizationServer() {
return CommonsExecWebServerFactoryBean.builder()
// ...
} They can also easily override the providerName (replace "spring" with a new value) using: @Bean
@OAuth2ClientProviderIssuerUri(providerName = "myProviderName")
static CommonsExecWebServerFactoryBean authorizationServer() {
return CommonsExecWebServerFactoryBean.builder()
// ...
}
This is not the case with my solution. I added the tests to a branch without the The reason that I'm also not a fan of the need for |
Environment
using properties of a beanEnvironment
using properties of a bean
Hi @rwinch, After further discussions within the team, we decided to register
We are not saying that a declarative feature should not use SpEL. Rather, we are saying that we do not want to introduce this feature into the core framework. Providing a general-purpose SpEL-based solution that suits all use cases for the community might not be trivial. One has to take into consideration what types of annotation attributes are supported, how they are converted/included in the SpEL expression, how nested annotations are handled, whether arrays are supported as annotation attributes, etc. As an alternative to an annotation-based approach (using SpEL and property placeholders), one may choose a programmatic approach, providing a DSL/utility that works with #32271 should therefore serve as a building block within the core framework that portfolio projects and third parties can build on, potentially using SpEL as you proposed in this issue. Regarding the need for In light of the above, I am closing this issue as:
And we encourage you and @philwebb to review the result of #32271 once it's on Cheers, Sam |
I just wanted to let you know that #32271 has been resolved and was included in Spring Framework 6.2 M2. In summary...
|
It would be nice to be able to add properties to the
Environment
based upon properties of a bean. Spring Boot Testjars does this using the code in org.springframework.experimental.boot.test.context.For example, the following code will add a property named
messages.url
to the environment with the value ofhttp://localhost:1234
assuming that themessagesApiServer
bean has a property namedport
that returns1234
.The value of the annotation is a SpEL expression with the bean as the root of the expression.
Composed annotations are also supported.
The following code allows
@MessageUrl
to be used in place of the@DynamicProperty
annotation above.Attributes in the composed annotation are also supported:
The default property name is now
messages.url
. However, if a user provides the annotation@MessageUrl(prefix="foo")
the property name would befoo.url
.The text was updated successfully, but these errors were encountered: