Friday, December 21, 2018

load property file via JNDI to Spring context

There are many ways to load properties via JNDI with little limitations.
the JDNI conteins only the placeholder for the file.

<Environment name="config/dgc509-bio" value="PATH_TO_CONFIG_FILE/dgc509-bio.properties" type="java.lang.String" />

1 ) You can load via application context xml file:
<util:properties id="appConfig" location="file:${config/dgc509-bio}"/>
<context:property-placeholder properties-ref="appConfig"/>

But this properties you will see only in this context + @Value()

2) You can load into PropertySourcesPlaceholderConfigurer

@Beanpublic static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(ConfigurableEnvironment env) throws IOException, NamingException {
   final String path = (String) new JndiLocatorDelegate().lookup("java:comp/env/config/dgc509-bio");
   PropertySourcesPlaceholderConfigurer placeholderConfigurer = new PropertySourcesPlaceholderConfigurer();
   Properties prop = new Properties();
   prop.load(new FileInputStream(new File(path)));
   env.getPropertySources().addFirst(new PropertiesPropertySource("reloaded", prop));
   return placeholderConfigurer;
}