Skip to content

Instantly share code, notes, and snippets.

@prule
Created May 6, 2013 07:37
Show Gist options
  • Save prule/5523826 to your computer and use it in GitHub Desktop.
Save prule/5523826 to your computer and use it in GitHub Desktop.
Spring application context showing how to reference JNDI resources exposed by a container
<?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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment