Skip to content

Instantly share code, notes, and snippets.

@hoyacom
Forked from rponte/MyAppCtxInitializer.java
Created July 15, 2016 02:54
Show Gist options
  • Save hoyacom/d3d482174406b920a2e84249f3133fda to your computer and use it in GitHub Desktop.
Save hoyacom/d3d482174406b920a2e84249f3133fda to your computer and use it in GitHub Desktop.
Configuring an ApplicationContextInitializer on Spring 3.1
public class MyAppCtxInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
private static Logger LOG = LoggerFactory.getLogger(MyAppCtxInitializer.class);
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment environment = applicationContext.getEnvironment();
try {
environment.getPropertySources().addFirst(new ResourcePropertySource("classpath:env.properties"));
LOG.info("env.properties loaded");
} catch (IOException e) {
// it's ok if the file is not there. we will just log that info.
LOG.info("didn't find env.properties in classpath so not loading it in the AppContextInitialized");
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app ...>
<context-param>
<param-name>contextInitializerClasses</param-name>
<param-value>somepackage.MyAppCtxInitializer</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment