You could use a subclass of PropertyPlaceHolderConfigurer that resolves placeholders as environment entry:
Code:
package org.sprfr;
import java.util.Properties;
import javax.naming.NamingException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.jndi.JndiTemplate;
public class EnvEntryPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer implements InitializingBean {
public static String CONTAINER_PREFIX = "java:comp/env/";
private boolean resourceRef = true;
public void setResourceRef(boolean resourceRef) {
this.resourceRef = resourceRef;
}
public boolean isResourceRef() {
return resourceRef;
}
private boolean envEntryOverride = false;
private JndiTemplate jndiTemplate;
public void setEnvEntryOverride(boolean contextOverride) {
this.envEntryOverride = contextOverride;
}
public void afterPropertiesSet() throws Exception {
this.jndiTemplate = new JndiTemplate();
}
protected String resolvePlaceholder(String placeholder, Properties props) {
String value = null;
if (this.envEntryOverride) {
value = resolveEnvEntryPlaceholder(placeholder);
}
if (value == null) {
value = super.resolvePlaceholder(placeholder, props);
}
if (value == null) {
value = resolveEnvEntryPlaceholder(placeholder);
}
return value;
}
protected String resolveEnvEntryPlaceholder(String placeholder) {
String value = null;
Object envEntry = null;
try {
String jndiNameToUse = convertJndiName(placeholder);
envEntry = jndiTemplate.lookup(jndiNameToUse);
} catch (NamingException e) {
logger.error("Naming Exception while trying to lookup env entry " + placeholder, e);
}
if (envEntry != null) {
value = envEntry.toString();
}
return value;
}
protected String convertJndiName(String jndiName) {
if (isResourceRef() && !jndiName.startsWith(CONTAINER_PREFIX) && jndiName.indexOf(':') == -1) {
jndiName = (new StringBuffer(CONTAINER_PREFIX).append(jndiName)).toString();
}
return jndiName;
}
}
In your applicationContext.xml :
Code:
<bean id="propertyConfigurer" class="org.sprfr.EnvEntryPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/app.properties</value>
<value>classpath:/jdbc.properties</value>
</list>
</property>
</bean>
You add a context file descriptor in $CATALINA_HOME/conf/[enginename]/[hostname]/[context].xml where you define the environment entry :
Code:
<Context reloadable="false" debug="0">
<Environment
name="myentry"
type="java.lang.String"
value="myvalue"/>
</Context>
And use it in your context:
Code:
<bean id="myBean" class="MyBean">
<property name="myProperty"><value>${myentry}</value></property>
</bean>
You can also define a default value in your app.properties and set envEntryOverride to true on propertyConfigurer. So you could deploy your war without context descriptor. And if you need to override some properties, you create one.
Hope this helps.