Open chengaofeng opened 3 years ago
Spring Boot does not support the reloading of properties at runtime. You may be interested in Spring Cloud's support for changing the environment and its refresh scope.
I am sorry not make myself clear.My project is indeed a spring cloud application, The Properties bean is also annotated by @RefreshScope.While The config server is alibaba‘s nacos. When I change the external config file as above(set the value to null), the properties's value didn't change as expect.
In debug mode, when the external config file changed from
test:
users:
- Andy
to
test:
users:
I can find that the propertySource in Environment#propertySources#propertySourceList changed to test.users => null
, and then an EnvironmentChangeEvent
published. but after the ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization
method invoked, the value of the users
in the Properties bean is still users[0]=Andy
Then I dig into the source of
org.springframework.boot.context.properties.source.SpringConfigurationPropertySource#find(PropertyMapping mapping)
private ConfigurationProperty find(PropertyMapping mapping) {
String propertySourceName = mapping.getPropertySourceName();
Object value = getPropertySource().getProperty(propertySourceName);
if (value == null) {
return null;
}
...
}
when the value is null, the method is return null, which means no such item. while in my situation,set test.users=>null
is different from not configuring at all
Spring Cloud version: Hoxton.SR8
Spring Boot version: 2.2.5.RELEASE
@wilkinsona,I'm appreciate that you could take a look at this problem again.
See https://github.com/spring-projects/spring-framework/issues/25142 for background on null
values in a MapPropertySource
and https://github.com/spring-projects/spring-boot/issues/21542#issuecomment-635107511 for a previous Boot issue.
This is a limitation of the way that null
is handled both in Boot and Spring Framework. I'm afraid the fix for this will be quite involved and is unlikely to happen soon.
https://github.com/spring-projects/spring-boot/pull/31037 is another case of #28139.
@philwebb do you know what all is needed to get this fixed? If https://github.com/spring-projects/spring-framework/issues/19986 is addressed, would it be easy for spring-boot to support nulls? Trying to see how to move this forward.
It's not just https://github.com/spring-projects/spring-framework/issues/19986. Spring Framework's Environment
abstraction would also have to be able to distinguish between an absent property and a property with a null
value. https://github.com/spring-projects/spring-framework/issues/25142 was opened to explore this but it was effectively rejected and turned into a documentation issue.
Application has a ben annotated with
@ConfigurationProperties("test")
that has aList<String>
attribute calledusers
. at the beginning, one item listed in the external configuration fileas the application is running, change the configuration file to the following
or
what I expect is
users=null
orusers =[]
, while the result is stillusers[0]=Andy
it‘s ok for the following pattern :
Here's a test that fails with Spring Boot
2.1.17.RELEASE