Class SpringEnvironmentPropertySource

java.lang.Object
org.apache.juneau.rest.server.springboot.SpringEnvironmentPropertySource
All Implemented Interfaces:
PropertySource

public class SpringEnvironmentPropertySource extends Object implements PropertySource
Bridges a Spring 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 Details

    • SpringEnvironmentPropertySource

      public SpringEnvironmentPropertySource(org.springframework.core.env.Environment env)
      Constructor that bridges a pre-built Environment.
      Parameters:
      env - The Spring environment to bridge. Must not be null.
    • SpringEnvironmentPropertySource

      public SpringEnvironmentPropertySource(Supplier<org.springframework.core.env.Environment> envSupplier)
      Constructor that lazily resolves the Environment on first lookup.

      Use this overload when wiring through ApplicationContext: pass appContext::getEnvironment so the application context isn't invoked until a lookup actually occurs.

      Parameters:
      envSupplier - Supplier of the Spring environment. Must not be null. May return null, in which case this source returns PropertyLookupResult.missing() for every key.
  • Method Details

    • get

      Description copied from interface: PropertySource
      Returns a property in this property source.
      Specified by:
      get in interface PropertySource
      Parameters:
      name - The property name.
      Returns:
      The property lookup result:
    • invalidate

      public void invalidate()
      Clears this bridge's miss cache.

      Needed only after a property source's contents change in place (for example a mutated MapPropertySource map, or MockEnvironment.setProperty in 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