当前页面:
在线文档首页 >
Spring Framework 1.2.9 API 文档英文版
ServiceLocatorFactoryBean (Spring Framework) - Spring Framework 1.2.9 API 文档英文版
org.springframework.beans.factory.config
Class ServiceLocatorFactoryBean
java.lang.Object
org.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)
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
ServiceLocatorFactoryBean
public ServiceLocatorFactoryBean()
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.
setServiceLocatorExceptionClass
public void setServiceLocatorExceptionClass(Class serviceLocatorExceptionClass)
- Set the exception class that the service locator should throw if service
lookup failed. The specified exception class should have a constructor
with one of the following parameter types:
(String, Throwable)
or (Throwable)
or (String)
.
If not specified, subclasses of Spring's BeansException will be thrown,
for example NoSuchBeanDefinitionException. As those are unchecked, the
caller does not need to handle them, so it might be acceptable that
Spring exceptions get thrown as long as they are just handled generically.
- See Also:
determineServiceLocatorExceptionConstructor(Class)
,
createServiceLocatorException(java.lang.reflect.Constructor, org.springframework.beans.BeansException)
,
BeansException
,
NoSuchBeanDefinitionException
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 the population of normal bean properties
but before an initialization callback such as
InitializingBean.afterPropertiesSet()
or a custom init-method.
- Specified by:
setBeanFactory
in interface BeanFactoryAware
- Parameters:
beanFactory
- owning BeanFactory (never null
).
The bean can immediately call methods on the factory.
- Throws:
BeansException
- in case of initialization errors- See Also:
BeanInitializationException
afterPropertiesSet
public void afterPropertiesSet()
- 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
determineServiceLocatorExceptionConstructor
protected Constructor determineServiceLocatorExceptionConstructor(Class exceptionClass)
- Determine the constructor to use for the given service locator exception
class. Only called in case of a custom service locator exception.
The default implementation looks for a constructor with one of the
following parameter types: (String, Throwable)
or (Throwable)
or (String)
.
- Parameters:
exceptionClass
- the exception class
- Returns:
- the constructor to use
- See Also:
setServiceLocatorExceptionClass(java.lang.Class)
createServiceLocatorException
protected Exception createServiceLocatorException(Constructor exceptionConstructor,
BeansException cause)
- Create a service locator exception for the given cause.
Only called in case of a custom service locator exception.
The default implementation can handle all variations of
message and exception arguments.
- Parameters:
exceptionConstructor
- the constructor to usecause
- the cause of the service lookup failure
- Returns:
- the service locator exception to throw
- See Also:
setServiceLocatorExceptionClass(java.lang.Class)
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
(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.
- Specified by:
isSingleton
in interface FactoryBean
- Returns:
- if this bean is a singleton
- See Also:
FactoryBean.getObject()
Copyright (c) 2002-2007 The Spring Framework Project.