Skip to content

Instantly share code, notes, and snippets.

@rponte
Created October 31, 2012 21:17
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rponte/3989915 to your computer and use it in GitHub Desktop.
Save rponte/3989915 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>
@cwash
Copy link

cwash commented Jul 24, 2013

This was helpful for me to get PropertySource loaded that will dynamically load in others.

@akkida746
Copy link

I hope this would be helpful too, i have made these changes in my project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment