Class SpringEnvironmentPropertySource
- All Implemented Interfaces:
PropertySource
Environment into the Juneau Settings
singleton as a PropertySource.
Allows @Value("{spring.property.key}") resolution
against the same property sources that Spring Boot would resolve through
@org.springframework.beans.factory.annotation.Value: application.yaml,
application.properties, command-line args, environment variables, and any custom
org.springframework.core.env.PropertySource that Spring loads.
Lifecycle:
SpringBeanStore registers one instance per ApplicationContext in
Settings, shared by every store on that context. It is removed
when every such store has been cleared, or when the context closes (for a
ConfigurableApplicationContext). Callers that
want explicit lifecycle control can construct the source directly and add it via Settings.get().addSource(...);
such instances are not shared.
Miss cache:
Juneau's builder keys (e.g. MarshallingContext.unsortedProperties) are not valid Spring Boot property names,
so Spring walks every raw property source for each of them. This source remembers up to 4096 such keys that were
absent. Every lookup checks the Environment's property-source list by identity (an added, removed or replaced
source drops the cache) and re-checks System.getProperty(String) for a cached key. Valid Boot names and
juneau.profiles.active are never cached, and hits are never cached. A source whose contents change
in place (a mutated map, MockEnvironment.setProperty) is not detected for a key already cached as missing;
call invalidate() after such a change. Setting juneau.spring.missCache.disable=true in the Spring
Environment before the first lookup turns the cache off.
Lazy resolution:
The Environment is resolved lazily on the first get(String) call so that
constructors that build the source from an ApplicationContext do not interact with the
context until a lookup actually happens. This matters for tests that mock the context and assert
"no interactions" until a Spring-side bean is requested.
Ordering:
Because Settings walks property sources in reverse insertion order, the Spring
Environment bridge — registered when Spring creates the bean store — shadows any
classpath-default juneau.cfg loaded earlier through the SPI-discovered
ConfigPropertySourceProvider. Programmatic addSource() calls made after the
Spring context is built (e.g. from a user-provided microservice Config) still win.
See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionSpringEnvironmentPropertySource(Supplier<org.springframework.core.env.Environment> envSupplier) Constructor that lazily resolves theEnvironmenton first lookup.SpringEnvironmentPropertySource(org.springframework.core.env.Environment env) Constructor that bridges a pre-builtEnvironment. -
Method Summary
Modifier and TypeMethodDescriptionReturns a property in this property source.voidClears this bridge's miss cache.
-
Constructor Details
-
SpringEnvironmentPropertySource
Constructor that bridges a pre-builtEnvironment.- Parameters:
env- The Spring environment to bridge. Must not benull .
-
SpringEnvironmentPropertySource
public SpringEnvironmentPropertySource(Supplier<org.springframework.core.env.Environment> envSupplier) Constructor that lazily resolves theEnvironmenton first lookup.Use this overload when wiring through
ApplicationContext: passappContext::getEnvironmentso the application context isn't invoked until a lookup actually occurs.- Parameters:
envSupplier- Supplier of the Spring environment. Must not benull . May returnnull , in which case this source returnsPropertyLookupResult.missing()for every key.
-
-
Method Details
-
get
Description copied from interface:PropertySourceReturns a property in this property source.- Specified by:
getin interfacePropertySource- Parameters:
name- The property name.- Returns:
- The property lookup result:
PropertyLookupResult.missing()if this source does not define the key.PropertyLookupResult.present(Optional)withOptional.empty()if the key exists with a null value.PropertyLookupResult.present(Optional)with a value if the key exists with a non-null value.
-
invalidate
Clears this bridge's miss cache.Needed only after a property source's contents change in place (for example a mutated
MapPropertySourcemap, orMockEnvironment.setPropertyin a test) for a key that is not a valid Spring Boot property name and that was already looked up and missed. Adding, removing or replacing a property source, and setting a system property, are detected without it. A lookup already in progress on another thread may still answer from the old cache; lookups that start after this method returns do not.- Since:
- 10.0.0
-