Skip to content

Instantly share code, notes, and snippets.

@gary-liguoliang
Last active September 3, 2015 10:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gary-liguoliang/9403188 to your computer and use it in GitHub Desktop.
Save gary-liguoliang/9403188 to your computer and use it in GitHub Desktop.
Using JNDI in Spring - JNDI DataSource / Environment - using a <jee:jndi-lookup string inside an instance of PropertyPlaceholderConfigurer
<!-- by https://gist.github.com/prule/5523826 -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
<!-- see https://gist.github.com/prule/5523793 for an example tomcat configuration exposing these JNDI resources -->
<context:property-placeholder order="1" ignore-unresolvable="true" system-properties-mode="OVERRIDE"/>
<bean id="envConfig" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<!-- load properties bundled with the application -->
<property name="locations">
<list>
<value>classpath:app.properties</value>
</list>
</property>
<!-- add JNDI based properties exposed by the container -->
<property name="properties">
<bean class="java.util.Properties">
<constructor-arg>
<map>
<entry key="ldap.host">
<jee:jndi-lookup jndi-name="java:comp/env/ldap/host"/>
</entry>
</map>
</constructor-arg>
</bean>
</property>
</bean>
<!-- use the String exposed by JNDI -->
<bean id="contextSource"
class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
<constructor-arg value="ldap://${ldap.host}/ou=users,dc=javathinking,dc=com"/>
<property name="userDn" value="uid=admin,ou=system"/>
<property name="password" value="secret"/>
</bean>
<!-- use the DataSource exposed by JNDI -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/mydb"/>
</bean>
<!-- use the mail Session exposed by JNDI -->
<bean id="mailSession" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/mail/session"/>
</bean>
</beans>
@gary-liguoliang
Copy link
Author

Error: The prefix "jee" for element "jee:jndi-lookup" is not bound.
Make sure the name space "jee' is there:

xmlns:jee="http://www.springframework.org/schema/jee"

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