Description
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 of http://localhost:1234
assuming that the messagesApiServer
bean has a property named port
that returns 1234
.
The value of the annotation is a SpEL expression with the bean as the root of the expression.
@Bean
@DynamicProperty(name = "messages.url", value = "'http://localhost:' + port")
static CommonsExecWebServerFactoryBean messagesApiServer() {
// ...
}
Composed annotations are also supported.
The following code allows @MessageUrl
to be used in place of the @DynamicProperty
annotation above.
@Retention(RetentionPolicy.RUNTIME)
@DynamicProperty(name = "message.url", value = "'http://localhost:' + port")
public @interface MessageUrl {
}
Attributes in the composed annotation are also supported:
@Retention(RetentionPolicy.RUNTIME)
@DynamicProperty(name = "${prefix}.url", value = "'http://localhost:' + port")
public @interface MessageUrl {
String prefix() default "messages";
}
The default property name is now messages.url
. However, if a user provides the annotation @MessageUrl(prefix="foo")
the property name would be foo.url
.