Class 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) and setSearchNames(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.

    • Method Detail

      • supportsEventType

        public boolean supportsEventType​(Class<? extends org.springframework.context.ApplicationEvent> eventType)
        Specified by:
        supportsEventType in interface org.springframework.context.event.SmartApplicationListener
      • onApplicationEvent

        public void onApplicationEvent​(org.springframework.context.ApplicationEvent event)
        Specified by:
        onApplicationEvent in interface org.springframework.context.ApplicationListener<org.springframework.context.ApplicationEvent>
      • 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 to
        resourceLoader - 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 interface org.springframework.core.Ordered
        Specified by:
        getOrder in interface org.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 from search 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