Top Description Inners Fields Constructors Methods
org.springframework.jndi

public Class JndiObjectFactoryBean

extends JndiObjectLocator
implements FactoryBean<Object>, BeanFactoryAware, BeanClassLoaderAware
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.FactoryBean
Imports
java.lang.reflect.Method, .Modifier, javax.naming.Context, .NamingException, org.aopalliance.intercept.MethodInterceptor, .MethodInvocation, org.springframework.aop.framework.ProxyFactory, org.springframework.beans.SimpleTypeConverter, .TypeConverter, .TypeMismatchException, org.springframework.beans.factory.BeanClassLoaderAware, .BeanFactory, .BeanFactoryAware, .FactoryBean, org.springframework.beans.factory.config.ConfigurableBeanFactory, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils

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 look up 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!

Author
Juergen Hoeller
Since
22.05.2003
See Also
setProxyInterface, setLookupOnStartup, setCache, JndiObjectTargetSource

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
JndiObjectFactoryBean.JndiContextExposingInterceptor

Interceptor that exposes the JNDI context for all method invocations, according to JndiObjectFactoryBean's "exposeAccessContext" flag.

private static class
JndiObjectFactoryBean.JndiObjectProxyFactory

Inner class to just introduce an AOP dependency when actually creating a proxy.

Field Summary

Modifier and TypeField and Description
private ClassLoader
private ConfigurableBeanFactory
private boolean
private Object
private boolean
private Object
private boolean
private Class<?>[]

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
protected Class<?>

Returns:

the merged interface as Class
createCompositeInterface
(Class<?>[]
the interfaces to merge
interfaces
)

Create a composite interface Class for the given interfaces, implementing the given interfaces in one single Class.

public Object
getObject()

Implements org.springframework.beans.factory.FactoryBean.getObject.

Return the singleton JNDI object.
public Class<?>
getObjectType()

Implements org.springframework.beans.factory.FactoryBean.getObjectType.

Return the type of object that this FactoryBean creates, or null if not known in advance.
public boolean
isSingleton()

Overrides default org.springframework.beans.factory.FactoryBean.isSingleton.

Is the object managed by this factory a singleton?
protected Object

Returns:

the located object, or the "defaultObject" as fallback
lookupWithFallback
()

Lookup variant that returns the specified "defaultObject" (if any) in case of lookup failure.

public void
setBeanClassLoader(ClassLoader
the owning class loader
classLoader
)

Implements org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Callback that supplies the bean class loader to a bean instance.
public void
setBeanFactory(BeanFactory
owning BeanFactory (never null). The bean can immediately call methods on the factory.
beanFactory
)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Callback that supplies the owning factory to a bean instance.
public void
setCache(boolean cache)

Set whether to cache the JNDI object once it has been located.

public void
setDefaultObject(Object defaultObject)

Specify a default object to fall back to if the JNDI lookup fails.

public void
setExposeAccessContext(boolean exposeAccessContext)

Set whether to expose the JNDI environment context for all access to the target object, i.e. for all method invocations on the exposed object reference.

public void
setLookupOnStartup(boolean lookupOnStartup)

Set whether to look up the JNDI object on startup.

public void
setProxyInterface(Class<?> proxyInterface)

Specify the proxy interface to use for the JNDI object.

public void
setProxyInterfaces(Class<?>... proxyInterfaces)

Specify multiple proxy interfaces to use for the JNDI object.

Inherited from org.springframework.jndi.JndiObjectLocator:
getExpectedTypegetJndiNamelookupsetExpectedTypesetJndiName

Field Detail

beanClassLoaderback to summary
private ClassLoader beanClassLoader
Annotations
@Nullable
beanFactoryback to summary
private ConfigurableBeanFactory beanFactory
Annotations
@Nullable
cacheback to summary
private boolean cache
defaultObjectback to summary
private Object defaultObject
Annotations
@Nullable
exposeAccessContextback to summary
private boolean exposeAccessContext
jndiObjectback to summary
private Object jndiObject
Annotations
@Nullable
lookupOnStartupback to summary
private boolean lookupOnStartup
proxyInterfacesback to summary
private Class<?>[] proxyInterfaces
Annotations
@Nullable

Constructor Detail

JndiObjectFactoryBeanback to summary
public JndiObjectFactoryBean()

Method Detail

afterPropertiesSetback to summary
public void afterPropertiesSet() throws IllegalArgumentException, NamingException

Overrides org.springframework.jndi.JndiObjectLocator.afterPropertiesSet.

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Look up the JNDI object and store it.

Annotations
@Override
createCompositeInterfaceback to summary
protected Class<?> createCompositeInterface(Class<?>[] interfaces)

Create a composite interface Class for the given interfaces, implementing the given interfaces in one single Class.

The default implementation builds a JDK proxy class for the given interfaces.

Parameters
interfaces:Class<?>[]

the interfaces to merge

Returns:Class<?>

the merged interface as Class

See Also
java.lang.reflect.Proxy#getProxyClass
getObjectback to summary
public Object getObject()

Implements org.springframework.beans.factory.FactoryBean.getObject.

Return the singleton JNDI object.

Returns:Object

Doc from org.springframework.beans.factory.FactoryBean.getObject.

an instance of the bean (can be null)

Annotations
@Override
@Nullable
getObjectTypeback to summary
public Class<?> getObjectType()

Implements org.springframework.beans.factory.FactoryBean.getObjectType.

Doc from org.springframework.beans.factory.FactoryBean.getObjectType.

Return the type of object that this FactoryBean creates, or null if not known in advance.

This allows one to check for specific types of beans without instantiating objects, for example on autowiring.

In the case of implementations that are creating a singleton object, this method should try to avoid singleton creation as far as possible; it should rather estimate the type in advance. For prototypes, returning a meaningful type here is advisable too.

This method can be called before this FactoryBean has been fully initialized. It must not rely on state created during initialization; of course, it can still use such state if available.

Note

Autowiring will simply ignore FactoryBeans that return null here. Therefore, it is highly recommended to implement this method properly, using the current state of the FactoryBean.

Returns:Class<?>

the type of object that this FactoryBean creates, or null if not known at the time of the call

Annotations
@Override
isSingletonback to summary
public boolean isSingleton()

Overrides default org.springframework.beans.factory.FactoryBean.isSingleton.

Doc from org.springframework.beans.factory.FactoryBean.isSingleton.

Is the object managed by this factory a singleton? That is, will getObject() always return the same object (a reference that can be cached)?

Note

If a FactoryBean indicates to hold a singleton object, the object returned from getObject() might get cached by the owning BeanFactory. Hence, do not return true unless the FactoryBean always exposes the same reference.

The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory; usually, it has to be defined as singleton there.

Note

This method returning false does not necessarily indicate that returned objects are independent instances. An implementation of the extended SmartFactoryBean interface may explicitly indicate independent instances through its SmartFactoryBean#isPrototype() method. Plain FactoryBean implementations which do not implement this extended interface are simply assumed to always return independent instances if the isSingleton() implementation returns false.

The default implementation returns true, since a FactoryBean typically manages a singleton instance.

Returns:boolean

whether the exposed object is a singleton

Annotations
@Override
lookupWithFallbackback to summary
protected Object lookupWithFallback() throws NamingException

Lookup variant that returns the specified "defaultObject" (if any) in case of lookup failure.

Returns:Object

the located object, or the "defaultObject" as fallback

Exceptions
NamingException:
in case of lookup failure without fallback
See Also
setDefaultObject
setBeanClassLoaderback to summary
public void setBeanClassLoader(ClassLoader classLoader)

Implements org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Doc from org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Callback that supplies the bean class loader to a bean instance.

Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean's InitializingBean#afterPropertiesSet() method or a custom init-method.

Parameters
classLoader:ClassLoader

the owning class loader

Annotations
@Override
setBeanFactoryback to summary
public void setBeanFactory(BeanFactory beanFactory)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Doc from org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Callback that supplies the owning factory to a bean instance.

Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean#afterPropertiesSet() or a custom init-method.

Parameters
beanFactory:BeanFactory

owning BeanFactory (never null). The bean can immediately call methods on the factory.

Annotations
@Override
setCacheback to summary
public void setCache(boolean cache)

Set whether to cache the JNDI object once it has been located. Default is "true".

Can be turned off to allow for hot redeployment of JNDI objects. In this case, the JNDI object will be fetched for each invocation.

For hot redeployment, a proxy interface needs to be specified.

See Also
setProxyInterface, setLookupOnStartup
setDefaultObjectback to summary
public void setDefaultObject(Object defaultObject)

Specify a default object to fall back to if the JNDI lookup fails. Default is none.

This can be an arbitrary bean reference or literal value. It is typically used for literal values in scenarios where the JNDI environment might define specific config settings but those are not required to be present.

Note

This is only supported for lookup on startup. If specified together with setExpectedType, the specified value needs to be either of that type or convertible to it.

See Also
setLookupOnStartup, ConfigurableBeanFactory#getTypeConverter(), SimpleTypeConverter
setExposeAccessContextback to summary
public void setExposeAccessContext(boolean exposeAccessContext)

Set whether to expose the JNDI environment context for all access to the target object, i.e. for all method invocations on the exposed object reference.

Default is "false", i.e. to only expose the JNDI context for object lookup. Switch this flag to "true" in order to expose the JNDI environment (including the authorization context) for each method invocation, as needed by WebLogic for JNDI-obtained factories (e.g. JDBC DataSource, JMS ConnectionFactory) with authorization requirements.

setLookupOnStartupback to summary
public void setLookupOnStartup(boolean lookupOnStartup)

Set whether to look up the JNDI object on startup. Default is "true".

Can be turned off to allow for late availability of the JNDI object. In this case, the JNDI object will be fetched on first access.

For a lazy lookup, a proxy interface needs to be specified.

See Also
setProxyInterface, setCache
setProxyInterfaceback to summary
public void setProxyInterface(Class<?> proxyInterface)

Specify the proxy interface to use for the JNDI object.

Typically used in conjunction with "lookupOnStartup"=false and/or "cache"=false. Needs to be specified because the actual JNDI object type is not known in advance in case of a lazy lookup.

See Also
setProxyInterfaces, setLookupOnStartup, setCache
setProxyInterfacesback to summary
public void setProxyInterfaces(Class<?>... proxyInterfaces)

Specify multiple proxy interfaces to use for the JNDI object.

Typically used in conjunction with "lookupOnStartup"=false and/or "cache"=false. Note that proxy interfaces will be autodetected from a specified "expectedType", if necessary.

See Also
setExpectedType, setLookupOnStartup, setCache
org.springframework.jndi back to summary

private Class JndiObjectFactoryBean.JndiContextExposingInterceptor

extends Object
implements MethodInterceptor
Class Inheritance
  • java.lang.Object
  • org.springframework.jndi.JndiObjectFactoryBean.JndiContextExposingInterceptor
All Implemented Interfaces
org.aopalliance.intercept.MethodInterceptor, org.aopalliance.intercept.Interceptor, org.aopalliance.aop.Advice

Interceptor that exposes the JNDI context for all method invocations, according to JndiObjectFactoryBean's "exposeAccessContext" flag.

Field Summary

Modifier and TypeField and Description
private final JndiTemplate

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Object
invoke(MethodInvocation
the method invocation joinpoint
invocation
)

Implements org.aopalliance.intercept.MethodInterceptor.invoke.

Implement this method to perform extra treatments before and after the invocation.
protected boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

jndiTemplateback to summary
private final JndiTemplate jndiTemplate

Constructor Detail

JndiContextExposingInterceptorback to summary
public JndiContextExposingInterceptor(JndiTemplate jndiTemplate)

Method Detail

invokeback to summary
public Object invoke(MethodInvocation invocation) throws Throwable

Implements org.aopalliance.intercept.MethodInterceptor.invoke.

Doc from org.aopalliance.intercept.MethodInterceptor.invoke.

Implement this method to perform extra treatments before and after the invocation. Polite implementations would certainly like to invoke Joinpoint#proceed().

Parameters
invocation:MethodInvocation

the method invocation joinpoint

Returns:Object

the result of the call to Joinpoint#proceed(); might be intercepted by the interceptor

Annotations
@Override
@Nullable
Exceptions
Throwable:
if the interceptors or the target object throws an exception
isEligibleback to summary
protected boolean isEligible(Method method)
org.springframework.jndi back to summary

private Class JndiObjectFactoryBean.JndiObjectProxyFactory

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.jndi.JndiObjectFactoryBean.JndiObjectProxyFactory

Inner class to just introduce an AOP dependency when actually creating a proxy.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static Object
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

JndiObjectProxyFactoryback to summary
private JndiObjectProxyFactory()

Method Detail

createJndiObjectProxyback to summary
private static Object createJndiObjectProxy(JndiObjectFactoryBean jof) throws NamingException