Spring 3.1 introduced a very convenient hook for customizing the application context with ApplicationContextInitializer. It can be used to set active profiles and register custom property sources.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | package com.mytutorial.configuration; import static org.springframework.core.env.StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME; import java.io.IOException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.ResourcePropertySource; public class MyAppApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { public static final String DEFAULT_APPLICATION_PROPERTIES = "myapp-service.properties"; private static final Logger LOG = LoggerFactory.getLogger(MyAppApplicationContextInitializer.class); @Override public void initialize(final ConfigurableApplicationContext applicationContext) { final ConfigurableEnvironment environment = applicationContext.getEnvironment(); final MutablePropertySources propertySources = environment.getPropertySources(); propertySources.addAfter(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getApplicationProperties(environment)); } /** * Loads application properties from the configured resource location. */ protected PropertySource<?> getApplicationProperties(final ConfigurableEnvironment environment) { try { final Resource propertiesResource = new ClassPathResource(DEFAULT_APPLICATION_PROPERTIES); final ResourcePropertySource propertySource = new ResourcePropertySource(propertiesResource); LOG.info("Configured application properties from: {}", propertySource); return propertySource; } catch (final IOException ex) { throw new RuntimeException("Unable to load application properties from default location: " + DEFAULT_APPLICATION_PROPERTIES, ex); } } } |
The web.xml can configure the “contextInitializerClasses”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //..... <servlet> <servlet-name>MyApp-endpoint</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextClass</param-name> <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> </init-param> <init-param> <param-name>contextConfigLocation</param-name> <param-value>com.mytutorial.configuration.MyAppEndpointConfiguration</param-value> </init-param> <init-param> <param-name>contextInitializerClasses</param-name> <param-value>com.mytutorial.configuration.MyAppApplicationContextInitializer</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> |
It can also be used in JUnit tests as shown below.
1 2 3 4 5 6 7 8 9 10 11 | package com.mytutorial.configuration; @ContextConfiguration( classes = { MyAppEndpointConfiguration.class }, initializers = MyAppApplicationContextInitializer.class) @RunWith(SpringJUnit4ClassRunner.class) public class CapsEntServiceTest { //.....tests } |