Class ConfigurationPropertiesRebinder
- All Implemented Interfaces:
EventListener,org.springframework.beans.factory.Aware,org.springframework.context.ApplicationContextAware,org.springframework.context.ApplicationListener<EnvironmentChangeEvent>
EnvironmentChangeEvent and rebinds beans that were bound to the
Environment using @ConfigurationProperties. When these beans are re-bound and
re-initialized, the changes are available immediately to any component that is using
the @ConfigurationProperties bean.
Rebinding a given bean is serialized against other concurrent rebinds of that same
bean, so two overlapping refreshes (for example a manual rebind(String) call
racing with an EnvironmentChangeEvent-triggered rebind()) cannot
interleave their destroy/reset/re-initialize steps.
To revert properties that were removed from the Environment to their defaults
without exposing those defaults to other threads, a new instance of the bean's class is
bound first and its state is then copied onto the live bean. A concurrent reader
therefore never sees a bound property reset to its class-level default (for example a
null nested object or a 0), and getter-only collections and maps are
updated in place rather than being cleared and refilled. The bean is still mutated in
place, field by field, so a reader can observe a mix of old and new values while a
rebind is in progress. Beans that need a consistent view across a refresh should use
RefreshScope instead, which serializes reads against refreshes via a per-bean
ReadWriteLock.
Known limitation: a collection property that has a value in the Environment can
be briefly empty during a rebind. The bean is initialized again after the new state has
been copied onto it, and Spring Boot's binder updates an existing, modifiable
collection by clearing it and then adding the bound elements. That happens inside
Spring Boot's binding and cannot be avoided here. Maps are not affected.
- Author:
- Dave Syer, Yanming Zhou
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConfigurationPropertiesRebinder(ConfigurationPropertiesBeans beans, RefreshAutoConfiguration.RefreshProperties refreshProperties) -
Method Summary
Modifier and TypeMethodDescriptionA map of bean name to errors when instantiating the bean.voidvoidrebind()booleanWARNING: This method rebinds beans from any context in the hierarchy using the main application context.booleanvoidsetApplicationContext(org.springframework.context.ApplicationContext applicationContext) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.context.ApplicationListener
supportsAsyncExecution
-
Constructor Details
-
ConfigurationPropertiesRebinder
-
ConfigurationPropertiesRebinder
public ConfigurationPropertiesRebinder(ConfigurationPropertiesBeans beans, RefreshAutoConfiguration.RefreshProperties refreshProperties)
-
-
Method Details
-
setApplicationContext
public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws org.springframework.beans.BeansException - Specified by:
setApplicationContextin interfaceorg.springframework.context.ApplicationContextAware- Throws:
org.springframework.beans.BeansException
-
getErrors
A map of bean name to errors when instantiating the bean.- Returns:
- The errors accumulated since the latest destroy.
-
rebind
@ManagedOperation public void rebind() -
rebind
-
rebind
WARNING: This method rebinds beans from any context in the hierarchy using the main application context.- Parameters:
type- bean type to rebind.- Returns:
- true, if successful.
-
getNeverRefreshable
-
getBeanNames
-
onApplicationEvent
- Specified by:
onApplicationEventin interfaceorg.springframework.context.ApplicationListener<EnvironmentChangeEvent>
-