org.springframework.jndi
Class JndiObjectFactoryBean

java.lang.Object
  extended byorg.springframework.jndi.JndiAccessor
      extended byorg.springframework.jndi.JndiLocatorSupport
          extended byorg.springframework.jndi.JndiObjectLocator
              extended byorg.springframework.jndi.JndiObjectFactoryBean
All Implemented Interfaces:
FactoryBean, InitializingBean

public class JndiObjectFactoryBean
extends JndiObjectLocator
implements FactoryBean

FactoryBean that looks up a JNDI object. Exposes the object found in JNDI when used as bean reference, e.g. for JdbcTemplate'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 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, because the actual JNDI object type is not known in advance.

Of course, service implementations can lookup e.g. a DataSource from JNDI themselves, but this class enables central configuration of the JNDI name, and easy switching to non-JNDI replacements. The latter can be used for test setups, standalone clients, etc.

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

Since:
22.05.2003
Author:
Juergen Hoeller
See Also:
setProxyInterface(java.lang.Class), setLookupOnStartup(boolean), setCache(boolean), JndiObjectTargetSource, DataSource, JdbcAccessor.setDataSource(javax.sql.DataSource), DriverManagerDataSource

Field Summary
 
Fields inherited from class org.springframework.jndi.JndiLocatorSupport
CONTAINER_PREFIX
 
Fields inherited from class org.springframework.jndi.JndiAccessor
logger
 
Constructor Summary
JndiObjectFactoryBean()
           
 
Method Summary
 void afterPropertiesSet()
          Look up the JNDI object and store it.
 Object getObject()
          Return the singleton JNDI object.
 Class getObjectType()
          Return the type of object that this FactoryBean creates, or null if not known in advance.
 boolean isSingleton()
          Is the bean managed by this factory a singleton or a prototype?
 void setCache(boolean cache)
          Set whether to cache the JNDI object once it has been located.
 void setLookupOnStartup(boolean lookupOnStartup)
          Set whether to look up the JNDI object on startup.
 void setProxyInterface(Class proxyInterface)
          Specify the proxy interface to use for the JNDI object.
 
Methods inherited from class org.springframework.jndi.JndiObjectLocator
getJndiName, lookup, setJndiName
 
Methods inherited from class org.springframework.jndi.JndiLocatorSupport
convertJndiName, isResourceRef, lookup, setResourceRef
 
Methods inherited from class org.springframework.jndi.JndiAccessor
getJndiEnvironment, getJndiTemplate, setJndiEnvironment, setJndiTemplate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JndiObjectFactoryBean

public JndiObjectFactoryBean()
Method Detail

setProxyInterface

public void setProxyInterface(Class proxyInterface)
Specify the proxy interface to use for the JNDI object. Needs to be specified because the actual JNDI object type is not known in advance in case of a lazy lookup.

Typically used in conjunction with "lookupOnStartup"=false and/or "cache"=false.

See Also:
setLookupOnStartup(boolean), setCache(boolean)

setLookupOnStartup

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(java.lang.Class), setCache(boolean)

setCache

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(java.lang.Class), setLookupOnStartup(boolean)

afterPropertiesSet

public void afterPropertiesSet()
                        throws NamingException
Look up the JNDI object and store it.

Specified by:
afterPropertiesSet in interface InitializingBean
Overrides:
afterPropertiesSet in class JndiObjectLocator
Throws:
NamingException
See Also:
JndiObjectLocator.lookup()

getObject

public Object getObject()
Return the singleton JNDI object.

Specified by:
getObject in interface FactoryBean
Returns:
an instance of the bean (should not be null; a null value will be considered as an indication of incomplete initialization)
See Also:
FactoryBeanNotInitializedException

getObjectType

public Class getObjectType()
Description copied from interface: FactoryBean
Return the type of object that this FactoryBean creates, or null if not known in advance. This allows to check for specific types of beans without instantiating objects, for example on autowiring.

For a singleton, this 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.

Specified by:
getObjectType in interface FactoryBean
Returns:
the type of object that this FactoryBean creates, or null if not known at the time of the call
See Also:
ListableBeanFactory.getBeansOfType(java.lang.Class)

isSingleton

public boolean isSingleton()
Description copied from interface: FactoryBean
Is the bean managed by this factory a singleton or a prototype? That is, will getObject() always return the same object?

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

Specified by:
isSingleton in interface FactoryBean
Returns:
if this bean is a singleton


Copyright (C) 2003-2004 The Spring Framework Project.