Class ConfigFileApplicationListener
- java.lang.Object
- org.springframework.boot.context.config.ConfigFileApplicationListener
- All Implemented Interfaces:
EventListener
,EnvironmentPostProcessor
,org.springframework.context.ApplicationListener<org.springframework.context.ApplicationEvent>
,org.springframework.context.event.SmartApplicationListener
,org.springframework.core.Ordered
public class ConfigFileApplicationListener extends Object implements EnvironmentPostProcessor, org.springframework.context.event.SmartApplicationListener, org.springframework.core.Ordered
EnvironmentPostProcessor
that configures the context environment by loading properties from well known file locations. By default properties will be loaded from 'application.properties' and/or 'application.yml' files in the following locations:- classpath:
- file:./
- classpath:config/
- file:./config/:
Alternative search locations and names can be specified using
setSearchLocations(String)
andsetSearchNames(String)
.Additional files will also be loaded based on active profiles. For example if a 'web' profile is active 'application-web.properties' and 'application-web.yml' will be considered.
The 'spring.config.name' property can be used to specify an alternative name to load and the 'spring.config.location' property can be used to specify alternative search locations or specific files.
Field Summary
Fields Modifier and Type Field Description static String
ACTIVE_PROFILES_PROPERTY
The "active profiles" property name.static String
CONFIG_ADDITIONAL_LOCATION_PROPERTY
The "config additional location" property name.static String
CONFIG_LOCATION_PROPERTY
The "config location" property name.static String
CONFIG_NAME_PROPERTY
The "config name" property name.static int
DEFAULT_ORDER
The default order for the processor.static String
INCLUDE_PROFILES_PROPERTY
The "includes profiles" property name.
Constructor Summary
Constructors Constructor Description ConfigFileApplicationListener()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addPostProcessors(org.springframework.context.ConfigurableApplicationContext context)
Add appropriate post-processors to post-configure the property-sources.protected void
addPropertySources(org.springframework.core.env.ConfigurableEnvironment environment, org.springframework.core.io.ResourceLoader resourceLoader)
Add config file property sources to the specified environment.int
getOrder()
void
onApplicationEvent(org.springframework.context.ApplicationEvent event)
void
postProcessEnvironment(org.springframework.core.env.ConfigurableEnvironment environment, SpringApplication application)
Post-process the givenenvironment
.void
setOrder(int order)
void
setSearchLocations(String locations)
Set the search locations that will be considered as a comma-separated list.void
setSearchNames(String names)
Sets the names of the files that should be loaded (excluding file extension) as a comma-separated list.boolean
supportsEventType(Class<? extends org.springframework.context.ApplicationEvent> eventType)
Field Detail
ACTIVE_PROFILES_PROPERTY
public static final String ACTIVE_PROFILES_PROPERTY
The "active profiles" property name.- See Also:
- Constant Field Values
INCLUDE_PROFILES_PROPERTY
public static final String INCLUDE_PROFILES_PROPERTY
The "includes profiles" property name.- See Also:
- Constant Field Values
CONFIG_NAME_PROPERTY
public static final String CONFIG_NAME_PROPERTY
The "config name" property name.- See Also:
- Constant Field Values
CONFIG_LOCATION_PROPERTY
public static final String CONFIG_LOCATION_PROPERTY
The "config location" property name.- See Also:
- Constant Field Values
CONFIG_ADDITIONAL_LOCATION_PROPERTY
public static final String CONFIG_ADDITIONAL_LOCATION_PROPERTY
The "config additional location" property name.- See Also:
- Constant Field Values
DEFAULT_ORDER
public static final int DEFAULT_ORDER
The default order for the processor.- See Also:
- Constant Field Values
Constructor Detail
ConfigFileApplicationListener
public ConfigFileApplicationListener()
Method Detail
supportsEventType
public boolean supportsEventType(Class<? extends org.springframework.context.ApplicationEvent> eventType)
- Specified by:
supportsEventType
in interfaceorg.springframework.context.event.SmartApplicationListener
onApplicationEvent
public void onApplicationEvent(org.springframework.context.ApplicationEvent event)
- Specified by:
onApplicationEvent
in interfaceorg.springframework.context.ApplicationListener<org.springframework.context.ApplicationEvent>
postProcessEnvironment
public void postProcessEnvironment(org.springframework.core.env.ConfigurableEnvironment environment, SpringApplication application)
Description copied from interface:EnvironmentPostProcessor
Post-process the givenenvironment
.- Specified by:
postProcessEnvironment
in interfaceEnvironmentPostProcessor
- Parameters:
environment
- the environment to post-processapplication
- the application to which the environment belongs
addPropertySources
protected void addPropertySources(org.springframework.core.env.ConfigurableEnvironment environment, org.springframework.core.io.ResourceLoader resourceLoader)
Add config file property sources to the specified environment.- Parameters:
environment
- the environment to add source toresourceLoader
- the resource loader- See Also:
addPostProcessors(ConfigurableApplicationContext)
addPostProcessors
protected void addPostProcessors(org.springframework.context.ConfigurableApplicationContext context)
Add appropriate post-processors to post-configure the property-sources.- Parameters:
context
- the context to configure
setOrder
public void setOrder(int order)
getOrder
public int getOrder()
- Specified by:
getOrder
in interfaceorg.springframework.core.Ordered
- Specified by:
getOrder
in interfaceorg.springframework.context.event.SmartApplicationListener
setSearchLocations
public void setSearchLocations(String locations)
Set the search locations that will be considered as a comma-separated list. Each search location should be a directory path (ending in "/") and it will be prefixed by the file names constructed fromsearch names
and profiles (if any) plus file extensions supported by the properties loaders. Locations are considered in the order specified, with later items taking precedence (like a map merge).- Parameters:
locations
- the search locations
setSearchNames
public void setSearchNames(String names)
Sets the names of the files that should be loaded (excluding file extension) as a comma-separated list.- Parameters:
names
- the names to load