Class JndiObjectFactoryBean

  • All Implemented Interfaces:
    Aware, BeanClassLoaderAware, BeanFactoryAware, FactoryBean<java.lang.Object>, InitializingBean

    public class JndiObjectFactoryBean
    extends JndiObjectLocator
    implements FactoryBean<java.lang.Object>, BeanFactoryAware, BeanClassLoaderAware
    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 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 DriverManagerDataSource definition!

    Juergen Hoeller
    See Also:
    setProxyInterface(java.lang.Class<?>), setLookupOnStartup(boolean), setCache(boolean), JndiObjectTargetSource
    • Constructor Detail

      • JndiObjectFactoryBean

        public JndiObjectFactoryBean()
    • Method Detail

      • setProxyInterface

        public void setProxyInterface(java.lang.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(java.lang.Class<?>...), 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)
      • setExposeAccessContext

        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.

      • afterPropertiesSet

        public void afterPropertiesSet()
                                throws java.lang.IllegalArgumentException,
        Look up the JNDI object and store it.
        Specified by:
        afterPropertiesSet in interface InitializingBean
        afterPropertiesSet in class JndiObjectLocator
      • lookupWithFallback

        protected java.lang.Object lookupWithFallback()
                                               throws javax.naming.NamingException
        Lookup variant that returns the specified "defaultObject" (if any) in case of lookup failure.
        the located object, or the "defaultObject" as fallback
        javax.naming.NamingException - in case of lookup failure without fallback
        See Also:
      • getObjectType

        public java.lang.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 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.

        Specified by:
        getObjectType in interface FactoryBean<java.lang.Object>
        the type of object that this FactoryBean creates, or null if not known at the time of the call
        See Also:
      • isSingleton

        public boolean isSingleton()
        Description copied from interface: FactoryBean
        Is the object managed by this factory a singleton? That is, will FactoryBean.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.

        Specified by:
        isSingleton in interface FactoryBean<java.lang.Object>
        whether the exposed object is a singleton
        See Also:
        FactoryBean.getObject(), SmartFactoryBean.isPrototype()
      • createCompositeInterface

        protected java.lang.Class<?> createCompositeInterface(java.lang.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.

        interfaces - the interfaces to merge
        the merged interface as Class
        See Also:
        Proxy.getProxyClass(java.lang.ClassLoader, java.lang.Class<?>...)