Created
December 27, 2016 12:23
-
-
Save akkida746/329eb41c6fe6159e3a8800451b693f0f to your computer and use it in GitHub Desktop.
Read properties file outside war in Spring application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Add this bean in spring-beans.xml --> | |
<bean id="propConfig" class="com.godiva.util.GodivaPropertiesConfugurer" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) { | |
}*/ | |
} | |
} |
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
Above class reads properties files outside war. Basically i am reading properties file from jetty home.