org.springframework.beans.factory.config
Class ServiceLocatorFactoryBean

java.lang.Object
  extended byorg.springframework.beans.factory.config.ServiceLocatorFactoryBean
All Implemented Interfaces:
BeanFactoryAware, FactoryBean, InitializingBean

public class ServiceLocatorFactoryBean
extends Object
implements FactoryBean, BeanFactoryAware, InitializingBean

FactoryBean that takes an interface which must have one or more methods with the signatures MyType xxx() or MyType xxx(MyIdType id) (typically, MyService getService() or MyService getService(String id)) and creates a dynamic proxy which implements that interface, delegating to the Spring BeanFactory underneath.

Such service locator allow to decouple the caller from Spring BeanFactory API, using an appropriate custom locator interface. They will typically be used for prototype beans, i.e. for factory methods that are supposed to return a new instance for each call. The client receives a reference to the service locator via setter or constructor injection, being able to invoke the locator's factory methods on demand. For singleton beans, direct setter or constructor injection of the target bean is preferable.

On invocation of the no-arg factory method, or the single-arg factory method with an id of null or empty String, if exactly one bean in the factory matches the return type of the factory method, that is returned, otherwise a NoSuchBeanDefinitionException is thrown.

On invocation of the single-arg factory method with a non-null (and non-empty) argument, the proxy returns the result of a BeanFactory.getBean(name) call, using a stringified version of the passed-in id as bean name.

A factory method argument will usually be a String, but can also be an int or a custom enumeration type, for example, stringified via toString. The resulting String can be used as bean name as-is, provided that corresponding beans are defined in the bean factory. Alternatively, mappings between service ids and bean names can be defined.

Since:
1.1.4
Author:
Colin Sampaleanu, Juergen Hoeller
See Also:
setServiceLocatorInterface(java.lang.Class), setServiceMappings(java.util.Properties)

Constructor Summary
ServiceLocatorFactoryBean()
           
 
Method Summary
 void afterPropertiesSet()
          Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
 Object getObject()
          Return an instance (possibly shared or independent) of the object managed by this factory.
 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 setBeanFactory(BeanFactory beanFactory)
          Callback that supplies the owning factory to a bean instance.
 void setServiceLocatorInterface(Class interfaceName)
          Set the service locator interface to use, which must have one or more methods with the signatures MyType xxx() or MyType xxx(MyIdType id) (typically, MyService getService() or MyService getService(String id)).
 void setServiceMappings(Properties serviceMappings)
          Set mappings between service ids (passed into the service locator) and bean names (in the bean factory).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ServiceLocatorFactoryBean

public ServiceLocatorFactoryBean()
Method Detail

setServiceLocatorInterface

public void setServiceLocatorInterface(Class interfaceName)
Set the service locator interface to use, which must have one or more methods with the signatures MyType xxx() or MyType xxx(MyIdType id) (typically, MyService getService() or MyService getService(String id)). See class-level javadoc for information on the semantics of such methods.


setServiceMappings

public void setServiceMappings(Properties serviceMappings)
Set mappings between service ids (passed into the service locator) and bean names (in the bean factory). Service ids that are not defined here will be treated as bean names as-is.

The empty string as service id key defines the mapping for null and empty string, and for factory methods without parameter. If not defined, a single matching bean will be retrieved from the bean factory.

Parameters:
serviceMappings - mappings between service ids and bean names, with service ids as keys as bean names as values

setBeanFactory

public void setBeanFactory(BeanFactory beanFactory)
                    throws BeansException
Description copied from interface: BeanFactoryAware
Callback that supplies the owning factory to a bean instance.

Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method.

Specified by:
setBeanFactory in interface BeanFactoryAware
Parameters:
beanFactory - owning BeanFactory (may not be null). The bean can immediately call methods on the factory.
Throws:
BeansException - in case of initialization errors
See Also:
BeanInitializationException

afterPropertiesSet

public void afterPropertiesSet()
                        throws BeansException
Description copied from interface: InitializingBean
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
BeansException

getObject

public Object getObject()
Description copied from interface: FactoryBean
Return an instance (possibly shared or independent) of the object managed by this factory. As with a BeanFactory, this allows support for both the Singleton and Prototype design pattern.

If this method returns null, the factory will consider the FactoryBean as not fully initialized and throw a corresponding FactoryBeanNotInitializedException.

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.