Skip to content

Instantly share code, notes, and snippets.

@akkida746
Created December 27, 2016 12:23
Show Gist options
  • Save akkida746/329eb41c6fe6159e3a8800451b693f0f to your computer and use it in GitHub Desktop.
Save akkida746/329eb41c6fe6159e3a8800451b693f0f to your computer and use it in GitHub Desktop.
Read properties file outside war in Spring application
<!-- Add this bean in spring-beans.xml -->
<bean id="propConfig" class="com.godiva.util.GodivaPropertiesConfugurer" />
@Configuration
public class PropertiesConfugurer implements WebApplicationInitializer, ApplicationContextAware,
ApplicationContextInitializer<ConfigurableApplicationContext> {
private static final Logger logger = Logger.getLogger(XmlFormatter.class);
private static ApplicationContext context;
private static String relativePath;
@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
File realPath = new File(servletContext.getRealPath("."));
relativePath = realPath.getParentFile().getParentFile().getParentFile().getPath();
}
@Bean
public static PropertyPlaceholderConfigurer ppc() throws IOException {
System.out.println("############################################");
System.out.println(relativePath);
logger.info("************ Application relative path ***********");
logger.info(relativePath);
System.out.println("############################################");
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
Resource[] resources = new FileSystemResource[ ] {new FileSystemResource(relativePath + "/config.properties")};
ppc.setLocations( resources );
ppc.setIgnoreUnresolvablePlaceholders( true );
return ppc;
}
@Override
public void setApplicationContext(ApplicationContext ctx)
throws BeansException {
this.context = ctx;
}
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
/*System.out.println("############# initialize #####################");
System.out.println(relativePath);
System.out.println("############################################");
ConfigurableEnvironment environment = applicationContext.getEnvironment();
try {
environment.getPropertySources().addFirst(new ResourcePropertySource(relativePath + "/config.properties"));
} catch (IOException e) {
}*/
}
}
@akkida746
Copy link
Author

Improved code:

@configuration
public class GdiPropertiesConfigurer implements WebApplicationInitializer {

private static final Logger logger = Logger.getLogger(XmlFormatter.class);

private static String rootDirectory;
	
/*** This method is called at the time of applications startup ***/	
@Override
public void onStartup(ServletContext servletContext)
		throws ServletException {
	
	Path path = Paths.get(servletContext.getRealPath("."));
	path.getRoot();
	
	/*** Setting root directory where Jetty resides ***/
	rootDirectory = (path.getRoot() != null) ? path.getRoot().toString() : StringUtils.EMPTY;					
}

/*** Initializing PropertyPlaceholderConfigurer bean to read config.properties from 
     application relative path ROOT/gdi_som/gdi_som_config/config.properties ***/
@Bean
public static PropertyPlaceholderConfigurer ppc() throws IOException {
	
	logger.info("************ Application relative path ***********");
	logger.info(rootDirectory);

	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	Resource[] resources;
	if(StringUtils.isNotEmpty(rootDirectory))
	{
		resources = new FileSystemResource[ ] {new FileSystemResource(rootDirectory + "/gdi_som/gdi_som_config/config.properties")};
	}
	else
	{
		resources = new FileSystemResource[ ] {new FileSystemResource("/gdi_som/gdi_som_config/config.properties")};
	}
	ppc.setLocations( resources );
	ppc.setIgnoreUnresolvablePlaceholders( true );
	
	return ppc;
}

}

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