org.springframework.jndi
Class JndiObjectTargetSource

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

public class JndiObjectTargetSource
extends JndiObjectLocator
implements TargetSource

TargetSource which performs a fresh JNDI lookup for each call.

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</value></property>
   <property name="lookupOnStartup"><value>false</value></property>
 </bean>

 <bean id="queueConnectionFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
   <property name="proxyInterfaces"><value>javax.jms.QueueConnectionFactory</value></property>
   <property name="targetSource"><ref bean="queueConnectionFactoryTarget"/></property>
 </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).

Since:
1.1
Author:
Juergen Hoeller
See Also:
setLookupOnStartup(boolean), setCache(boolean), AdvisedSupport.setTargetSource(org.springframework.aop.TargetSource), JndiObjectFactoryBean.setProxyInterface(java.lang.Class)

Field Summary
 
Fields inherited from class org.springframework.jndi.JndiLocatorSupport
CONTAINER_PREFIX
 
Fields inherited from class org.springframework.jndi.JndiAccessor
logger
 
Constructor Summary
JndiObjectTargetSource()
           
 
Method Summary
 void afterPropertiesSet()
          Check the jndiName property and initiate a lookup.
 Object getTarget()
          Return a target instance.
 Class getTargetClass()
          Return the type of targets returned by this TargetSource.
 boolean isStatic()
          Will all calls to getTarget() return the same object?
 void releaseTarget(Object target)
          Release the given target object obtained from the getTarget() method.
 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.
 
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

JndiObjectTargetSource

public JndiObjectTargetSource()
Method Detail

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.

See Also:
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.

See Also:
setLookupOnStartup(boolean)

afterPropertiesSet

public void afterPropertiesSet()
                        throws NamingException
Description copied from class: JndiObjectLocator
Check the jndiName property and initiate a lookup.

The JNDI object will thus be fetched eagerly on initialization. For refreshing the JNDI object, subclasses can invoke lookup at any later time.

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

getTargetClass

public Class getTargetClass()
Description copied from interface: TargetSource
Return the type of targets returned by this TargetSource. Can return null, although certain usages of a TargetSource might just work with a predetermined target class.

Specified by:
getTargetClass in interface TargetSource

isStatic

public boolean isStatic()
Description copied from interface: TargetSource
Will all calls to getTarget() return the same object? In that case, there will be no need to invoke releaseTarget, and the AOP framework can cache the return value of getTarget().

Specified by:
isStatic in interface TargetSource
Returns:
whether the target is immutable
See Also:
TargetSource.getTarget()

getTarget

public Object getTarget()
                 throws NamingException
Description copied from interface: TargetSource
Return a target instance. Invoked immediately before the AOP framework calls the "target" of an AOP method invocation.

Specified by:
getTarget in interface TargetSource
Returns:
the target object, whicch contains the joinpoint
Throws:
NamingException

releaseTarget

public void releaseTarget(Object target)
Description copied from interface: TargetSource
Release the given target object obtained from the getTarget() method.

Specified by:
releaseTarget in interface TargetSource
Parameters:
target - object obtained from a call to getTarget()
See Also:
TargetSource.getTarget()


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