or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

admin-jmx.mdansi-support.mdaot-native-image.mdapplication-info.mdavailability.mdbootstrap.mdbootstrapping.mdbuilder.mdcloud-platform.mdconfiguration-annotations.mdconfiguration-data.mdconfiguration-properties.mdconversion.mddiagnostics.mdenvironment-property-sources.mdindex.mdjson-support.mdlifecycle-events.mdlogging.mdorigin-tracking.mdresource-loading.mdretry-support.mdssl-tls.mdstartup-metrics.mdsupport.mdsystem-utilities.mdtask-execution.mdthreading.mdutilities.mdvalidation.mdweb-support.md
tile.json

support.mddocs/

Spring Boot Support Package

Package: org.springframework.boot.support

The support package provides classes for environment post-processing and application lifecycle concerns, including factory interfaces for creating environment post-processors and various built-in post-processor implementations.

Core Interfaces

EnvironmentPostProcessorsFactory

Factory interface for creating EnvironmentPostProcessor instances with dependency injection support.

@FunctionalInterface
public interface EnvironmentPostProcessorsFactory {
    List<EnvironmentPostProcessor> getEnvironmentPostProcessors(
        DeferredLogFactory logFactory,
        ConfigurableBootstrapContext bootstrapContext);

    static EnvironmentPostProcessorsFactory fromSpringFactories(ClassLoader classLoader);
    static EnvironmentPostProcessorsFactory of(Class<?>... classes);
    static EnvironmentPostProcessorsFactory of(String... classNames);
    static EnvironmentPostProcessorsFactory of(ClassLoader classLoader, String... classNames);
}

Factory Methods:

  • fromSpringFactories() - Creates factory backed by spring.factories loader
  • of(Class<?>...) - Creates factory from class types using reflection
  • of(String...) - Creates factory from class names using reflection

Example:

// Create from spring.factories
EnvironmentPostProcessorsFactory factory =
    EnvironmentPostProcessorsFactory.fromSpringFactories(classLoader);

// Create from specific classes
EnvironmentPostProcessorsFactory factory =
    EnvironmentPostProcessorsFactory.of(
        MyPostProcessor.class,
        AnotherPostProcessor.class
    );

// Get post-processors
List<EnvironmentPostProcessor> processors =
    factory.getEnvironmentPostProcessors(logFactory, bootstrapContext);

Listener Components

EnvironmentPostProcessorApplicationListener

Application listener that triggers environment post-processors during application startup.

public class EnvironmentPostProcessorApplicationListener
        implements SmartApplicationListener, Ordered {

    public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 10;

    public EnvironmentPostProcessorApplicationListener();

    public static EnvironmentPostProcessorApplicationListener with(
        EnvironmentPostProcessorsFactory postProcessorsFactory);

    @Override
    public boolean supportsEventType(Class<? extends ApplicationEvent> eventType);

    @Override
    public void onApplicationEvent(ApplicationEvent event);

    @Override
    public int getOrder();

    public void setOrder(int order);
}

Supported Events:

  • ApplicationEnvironmentPreparedEvent - Applies environment post-processors
  • ApplicationPreparedEvent - Switches deferred logs
  • ApplicationFailedEvent - Switches deferred logs on failure

Example:

// Create with custom factory
EnvironmentPostProcessorsFactory factory =
    EnvironmentPostProcessorsFactory.of(CustomPostProcessor.class);
EnvironmentPostProcessorApplicationListener listener =
    EnvironmentPostProcessorApplicationListener.with(factory);

// Add to application
SpringApplication app = new SpringApplication(MyApp.class);
app.addListeners(listener);

AnsiOutputApplicationListener

Configures ANSI output based on environment properties.

public class AnsiOutputApplicationListener
        implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {

    @Override
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event);

    @Override
    public int getOrder();
}

Configuration Properties:

  • spring.output.ansi.enabled - Enable/disable ANSI output (ALWAYS, DETECT, NEVER)
  • spring.output.ansi.console-available - Set console availability

Built-in Environment Post-Processors

RandomValuePropertySourceEnvironmentPostProcessor

Adds the RandomValuePropertySource to the environment for generating random values.

public class RandomValuePropertySourceEnvironmentPostProcessor
        implements EnvironmentPostProcessor, Ordered {

    public static final int ORDER = Ordered.HIGHEST_PRECEDENCE + 1;

    public RandomValuePropertySourceEnvironmentPostProcessor(DeferredLogFactory logFactory);

    @Override
    public int getOrder();

    @Override
    public void postProcessEnvironment(
        ConfigurableEnvironment environment,
        SpringApplication application);
}

Usage: Enables ${random.*} property placeholders:

# application.properties
my.secret=${random.value}
my.number=${random.int}
my.uuid=${random.uuid}

SystemEnvironmentPropertySourceEnvironmentPostProcessor

Replaces the system environment property source with an origin-aware version that tracks where system properties come from.

public class SystemEnvironmentPropertySourceEnvironmentPostProcessor
        implements EnvironmentPostProcessor, Ordered {

    public static final int DEFAULT_ORDER =
        SpringApplicationJsonEnvironmentPostProcessor.DEFAULT_ORDER - 1;

    @Override
    public void postProcessEnvironment(
        ConfigurableEnvironment environment,
        SpringApplication application);

    @Override
    public int getOrder();

    public void setOrder(int order);

    public static void postProcessEnvironment(
        ConfigurableEnvironment environment,
        ConfigurableEnvironment parentEnvironment);
}

Inner Class:

protected static class OriginAwareSystemEnvironmentPropertySource
        extends SystemEnvironmentPropertySource
        implements PropertySourceInfo, OriginLookup<String> {

    @Override
    public boolean containsProperty(String name);

    @Override
    public Object getProperty(String name);

    @Override
    public Origin getOrigin(String key);

    @Override
    public String getPrefix();

    @Override
    public boolean isImmutable();
}

SpringApplicationJsonEnvironmentPostProcessor

Parses JSON from spring.application.json or SPRING_APPLICATION_JSON environment variable and adds properties to the environment.

public class SpringApplicationJsonEnvironmentPostProcessor
        implements EnvironmentPostProcessor, Ordered {

    public static final String SPRING_APPLICATION_JSON_PROPERTY = "spring.application.json";
    public static final String SPRING_APPLICATION_JSON_ENVIRONMENT_VARIABLE =
        "SPRING_APPLICATION_JSON";
    public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 5;

    @Override
    public int getOrder();

    public void setOrder(int order);

    @Override
    public void postProcessEnvironment(
        ConfigurableEnvironment environment,
        SpringApplication application);
}

Usage:

# As environment variable
export SPRING_APPLICATION_JSON='{"server":{"port":8080},"logging":{"level":{"root":"INFO"}}}'

# As system property
java -Dspring.application.json='{"server":{"port":8080}}' -jar app.jar

The JSON is flattened into dot-notation properties:

  • {"server":{"port":8080}} becomes server.port=8080
  • {"items":[1,2,3]} becomes items[0]=1, items[1]=2, items[2]=3

Factory Implementations

SpringFactoriesEnvironmentPostProcessorsFactory

Implementation that loads post-processors from spring.factories files.

class SpringFactoriesEnvironmentPostProcessorsFactory
        implements EnvironmentPostProcessorsFactory {

    SpringFactoriesEnvironmentPostProcessorsFactory(SpringFactoriesLoader loader);

    @Override
    public List<EnvironmentPostProcessor> getEnvironmentPostProcessors(
        DeferredLogFactory logFactory,
        ConfigurableBootstrapContext bootstrapContext);
}

ReflectionEnvironmentPostProcessorsFactory

Implementation that creates post-processors using reflection with constructor injection.

class ReflectionEnvironmentPostProcessorsFactory
        implements EnvironmentPostProcessorsFactory {

    ReflectionEnvironmentPostProcessorsFactory(Class<?>... classes);
    ReflectionEnvironmentPostProcessorsFactory(ClassLoader classLoader, String... classNames);
    ReflectionEnvironmentPostProcessorsFactory(ClassLoader classLoader, List<String> classNames);

    @Override
    public List<EnvironmentPostProcessor> getEnvironmentPostProcessors(
        DeferredLogFactory logFactory,
        ConfigurableBootstrapContext bootstrapContext);
}

Supported Constructor Parameters:

  • DeferredLogFactory - For deferred logging
  • Log - Logger instance
  • ConfigurableBootstrapContext - Bootstrap context
  • BootstrapContext - Bootstrap context (read-only)
  • BootstrapRegistry - Bootstrap registry

Usage Examples

Creating Custom Environment Post-Processor

public class CustomEnvironmentPostProcessor implements EnvironmentPostProcessor {

    private final Log logger;

    public CustomEnvironmentPostProcessor(DeferredLogFactory logFactory) {
        this.logger = logFactory.getLog(getClass());
    }

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment,
                                      SpringApplication application) {
        // Add custom property source
        Map<String, Object> properties = new HashMap<>();
        properties.put("custom.property", "value");
        environment.getPropertySources().addLast(
            new MapPropertySource("customSource", properties));

        logger.info("Custom environment post-processor applied");
    }
}

Using with SpringApplication

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(MyApplication.class);

        // Add custom post-processor factory
        EnvironmentPostProcessorsFactory factory =
            EnvironmentPostProcessorsFactory.of(CustomEnvironmentPostProcessor.class);
        app.addListeners(EnvironmentPostProcessorApplicationListener.with(factory));

        app.run(args);
    }
}

Registering in spring.factories

# META-INF/spring.factories
org.springframework.boot.EnvironmentPostProcessor=\
com.example.CustomEnvironmentPostProcessor,\
com.example.AnotherCustomPostProcessor

Related Property Source Classes

The following property source classes are documented in environment-property-sources.md:

  • ConfigTreePropertySource - Property source for reading configuration from directory trees (Kubernetes ConfigMaps/Secrets)
  • DefaultPropertiesPropertySource - Property source wrapper for default properties
  • PropertySourceLoader - Interface for loading property sources from resources
  • YamlPropertySourceLoader - Loads YAML format property sources
  • PropertiesPropertySourceLoader - Loads standard .properties files

For detailed documentation of these classes, see environment-property-sources.md.

Import Statements

import org.springframework.boot.support.AnsiOutputApplicationListener;
import org.springframework.boot.support.EnvironmentPostProcessorApplicationListener;
import org.springframework.boot.support.EnvironmentPostProcessorsFactory;
import org.springframework.boot.support.RandomValuePropertySourceEnvironmentPostProcessor;
import org.springframework.boot.support.SpringApplicationJsonEnvironmentPostProcessor;
import org.springframework.boot.support.SystemEnvironmentPropertySourceEnvironmentPostProcessor;
import org.springframework.boot.EnvironmentPostProcessor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.bootstrap.ConfigurableBootstrapContext;
import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.core.env.ConfigurableEnvironment;