spring-framework / org.springframework.jndi

Package org.springframework.jndi

Types

JndiCallback

interface JndiCallback<T : Any>

Callback interface to be implemented by classes that need to perform an operation (such as a lookup) in a JNDI context. This callback approach is valuable in simplifying error handling, which is performed by the JndiTemplate class. This is a similar to JdbcTemplate's approach.

Note that there is hardly any need to implement this callback interface, as JndiTemplate provides all usual JNDI operations via convenience methods.

JndiObjectFactoryBean

open class JndiObjectFactoryBean : JndiObjectLocator, FactoryBean<Any>, BeanFactoryAware, BeanClassLoaderAware

org.springframework.beans.factory.FactoryBean that looks up a JNDI object. Exposes the object found in JNDI for bean references, e.g. for data access object's "dataSource" property in case of a javax.sql.DataSource.

The typical usage will be to register this as singleton factory (e.g. for a certain JNDI-bound DataSource) in an application context, and give bean references to application services that need it.

The default behavior is to look up the JNDI object on startup and cache it. This can be customized through the "lookupOnStartup" and "cache" properties, using a JndiObjectTargetSource underneath. Note that you need to specify a "proxyInterface" in such a scenario, since the actual JNDI object type is not known in advance.

Of course, bean classes in a Spring environment may lookup e.g. a DataSource from JNDI themselves. This class simply enables central configuration of the JNDI name, and easy switching to non-JNDI alternatives. The latter is particularly convenient for test setups, reuse in standalone clients, etc.

Note that switching to e.g. DriverManagerDataSource is just a matter of configuration: Simply replace the definition of this FactoryBean with a org.springframework.jdbc.datasource.DriverManagerDataSource definition!

JndiObjectTargetSource

open class JndiObjectTargetSource : JndiObjectLocator, TargetSource

AOP org.springframework.aop.TargetSource that provides configurable JNDI lookups for getTarget() calls.

Can be used as alternative to JndiObjectFactoryBean, to allow for relocating a JNDI object lazily or for each operation (see "lookupOnStartup" and "cache" properties). This is particularly useful during development, as it allows for hot restarting of the JNDI server (for example, a remote JMS server).

Example:

 <bean id="queueConnectionFactoryTarget" class="org.springframework.jndi.JndiObjectTargetSource"> <property name="jndiName" value="JmsQueueConnectionFactory"/> <property name="lookupOnStartup" value="false"/> </bean> <bean id="queueConnectionFactory" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces" value="javax.jms.QueueConnectionFactory"/> <property name="targetSource" ref="queueConnectionFactoryTarget"/> </bean>
A createQueueConnection call on the "queueConnectionFactory" proxy will cause a lazy JNDI lookup for "JmsQueueConnectionFactory" and a subsequent delegating call to the retrieved QueueConnectionFactory's createQueueConnection.

Alternatively, use a JndiObjectFactoryBean with a "proxyInterface". "lookupOnStartup" and "cache" can then be specified on the JndiObjectFactoryBean, creating a JndiObjectTargetSource underneath (instead of defining separate ProxyFactoryBean and JndiObjectTargetSource beans).

JndiPropertySource

open class JndiPropertySource : PropertySource<JndiLocatorDelegate>

PropertySource implementation that reads properties from an underlying Spring JndiLocatorDelegate.

By default, the underlying JndiLocatorDelegate will be configured with its "resourceRef" property set to true, meaning that names looked up will automatically be prefixed with "java:comp/env/" in alignment with published JNDI naming conventions. To override this setting or to change the prefix, manually configure a JndiLocatorDelegate and provide it to one of the constructors here that accepts it. The same applies when providing custom JNDI properties. These should be specified using JndiLocatorDelegate#setJndiEnvironment(java.util.Properties) prior to construction of the JndiPropertySource.

Note that org.springframework.web.context.support.StandardServletEnvironment includes a JndiPropertySource by default, and any customization of the underlying JndiLocatorDelegate may be performed within an org.springframework.context.ApplicationContextInitializer or org.springframework.web.WebApplicationInitializer.

JndiTemplateEditor

open class JndiTemplateEditor : PropertyEditorSupport

Properties editor for JndiTemplate objects. Allows properties of type JndiTemplate to be populated with a properties-format string.

Exceptions

JndiLookupFailureException

open class JndiLookupFailureException : NestedRuntimeException

RuntimeException to be thrown in case of JNDI lookup failures, in particular from code that does not declare JNDI's checked javax.naming.NamingException: for example, from Spring's JndiObjectTargetSource.

TypeMismatchNamingException

open class TypeMismatchNamingException : NamingException

Exception thrown if a type mismatch is encountered for an object located in a JNDI environment. Thrown by JndiTemplate.