Class JndiObjectFactoryBean
- All Implemented Interfaces:
- Aware,- BeanClassLoaderAware,- BeanFactoryAware,- FactoryBean<Object>,- InitializingBean
FactoryBean that looks up a
 JNDI object. Exposes the object found in JNDI for bean references,
 for example, for data access object's "dataSource" property in case of a
 DataSource.
 The typical usage will be to register this as singleton factory (for example, 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, for example, 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, for example, DriverManagerDataSource is just a matter of
 configuration: Simply replace the definition of this FactoryBean with a
 DriverManagerDataSource definition!
- Since:
- 22.05.2003
- Author:
- Juergen Hoeller
- See Also:
- 
Field SummaryFields inherited from class org.springframework.jndi.JndiLocatorSupportCONTAINER_PREFIXFields inherited from class org.springframework.jndi.JndiAccessorloggerFields inherited from interface org.springframework.beans.factory.FactoryBeanOBJECT_TYPE_ATTRIBUTE
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionvoidLook up the JNDI object and store it.protected Class<?> createCompositeInterface(Class<?>[] interfaces) Create a composite interface Class for the given interfaces, implementing the given interfaces in one single Class.Return the singleton JNDI object.Return the type of object that this FactoryBean creates, ornullif not known in advance.booleanIs the object managed by this factory a singleton? That is, willFactoryBean.getObject()always return the same object (a reference that can be cached)?protected ObjectLookup variant that returns the specified "defaultObject" (if any) in case of lookup failure.voidsetBeanClassLoader(ClassLoader classLoader) Callback that supplies the beanclass loaderto a bean instance.voidsetBeanFactory(BeanFactory beanFactory) Callback that supplies the owning factory to a bean instance.voidsetCache(boolean cache) Set whether to cache the JNDI object once it has been located.voidsetDefaultObject(Object defaultObject) Specify a default object to fall back to if the JNDI lookup fails.voidsetExposeAccessContext(boolean exposeAccessContext) Set whether to expose the JNDI environment context for all access to the target object, i.e.voidsetLookupOnStartup(boolean lookupOnStartup) Set whether to look up the JNDI object on startup.voidsetProxyInterface(Class<?> proxyInterface) Specify the proxy interface to use for the JNDI object.voidsetProxyInterfaces(Class<?>... proxyInterfaces) Specify multiple proxy interfaces to use for the JNDI object.Methods inherited from class org.springframework.jndi.JndiObjectLocatorgetExpectedType, getJndiName, lookup, setExpectedType, setJndiNameMethods inherited from class org.springframework.jndi.JndiLocatorSupportconvertJndiName, isResourceRef, lookup, lookup, setResourceRefMethods inherited from class org.springframework.jndi.JndiAccessorgetJndiEnvironment, getJndiTemplate, setJndiEnvironment, setJndiTemplate
- 
Constructor Details- 
JndiObjectFactoryBeanpublic JndiObjectFactoryBean()
 
- 
- 
Method Details- 
setProxyInterfaceSpecify 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:
 
- 
setProxyInterfacesSpecify 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:
 
- 
setLookupOnStartuppublic 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:
 
- 
setCachepublic 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:
 
- 
setExposeAccessContextpublic 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 (for example, JDBC DataSource, JMS ConnectionFactory) with authorization requirements. 
- 
setDefaultObjectSpecify 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 JndiObjectLocator.setExpectedType(java.lang.Class<?>), the specified value needs to be either of that type or convertible to it.- See Also:
 
- 
setBeanFactoryDescription copied from interface:BeanFactoryAwareCallback 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.- Specified by:
- setBeanFactoryin interface- BeanFactoryAware
- Parameters:
- beanFactory- owning BeanFactory (never- null). The bean can immediately call methods on the factory.
- See Also:
 
- 
setBeanClassLoaderDescription copied from interface:BeanClassLoaderAwareCallback that supplies the beanclass loaderto a bean instance.Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean'sInitializingBean.afterPropertiesSet()method or a custom init-method.- Specified by:
- setBeanClassLoaderin interface- BeanClassLoaderAware
- Parameters:
- classLoader- the owning class loader
 
- 
afterPropertiesSetLook up the JNDI object and store it.- Specified by:
- afterPropertiesSetin interface- InitializingBean
- Overrides:
- afterPropertiesSetin class- JndiObjectLocator
- Throws:
- IllegalArgumentException
- NamingException
 
- 
lookupWithFallbackLookup variant that returns the specified "defaultObject" (if any) in case of lookup failure.- Returns:
- the located object, or the "defaultObject" as fallback
- Throws:
- NamingException- in case of lookup failure without fallback
- See Also:
 
- 
getObjectReturn the singleton JNDI object.- Specified by:
- getObjectin interface- FactoryBean<Object>
- Returns:
- an instance of the bean (can be null)
- See Also:
 
- 
getObjectTypeDescription copied from interface:FactoryBeanReturn the type of object that this FactoryBean creates, ornullif 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 create 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 nullhere. Therefore, it is highly recommended to implement this method properly, using the current state of the FactoryBean.- Specified by:
- getObjectTypein interface- FactoryBean<Object>
- Returns:
- the type of object that this FactoryBean creates,
 or nullif not known at the time of the call
- See Also:
 
- 
isSingletonpublic boolean isSingleton()Description copied from interface:FactoryBeanIs the object managed by this factory a singleton? That is, willFactoryBean.getObject()always return the same object (a reference that can be cached)?NOTE: If a FactoryBean indicates that it holds a singleton object, the object returned from getObject()might get cached by the owning BeanFactory. Hence, do not returntrueunless 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 falsedoes not necessarily indicate that returned objects are independent instances. An implementation of the extendedSmartFactoryBeaninterface may explicitly indicate independent instances through itsSmartFactoryBean.isPrototype()method. PlainFactoryBeanimplementations which do not implement this extended interface are simply assumed to always return independent instances if theisSingleton()implementation returnsfalse.The default implementation returns true, since aFactoryBeantypically manages a singleton instance.- Specified by:
- isSingletonin interface- FactoryBean<Object>
- Returns:
- whether the exposed object is a singleton
- See Also:
 
- 
createCompositeInterfaceCreate 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- the interfaces to merge
- Returns:
- the merged interface as Class
- See Also:
 
 
-