BeanFactory which
are themselves factories for individual objects. If a bean implements this
interface, it is used as a factory for an object to expose, not directly as a
bean instance that will be exposed itself.
NB: A bean that implements this interface cannot be used as a normal bean.
A FactoryBean is defined in a bean style, but the object exposed for bean
references (getObject()) is always the object that it creates.
FactoryBeans can support singletons and prototypes, and can either create
objects lazily on demand or eagerly on startup. The SmartFactoryBean
interface allows for exposing more fine-grained behavioral metadata.
This interface is heavily used within the framework itself, for example for
the AOP org. or the
org.. It can be used for
custom components as well; however, this is only common for infrastructure code.
FactoryBean is a programmatic contract. Implementations are not
supposed to rely on annotation-driven injection or other reflective facilities.
getObjectType() getObject() invocations may arrive early in the
bootstrap process, even ahead of any post-processor setup. If you need access to
other beans, implement BeanFactoryAware and obtain them programmatically.
The container is only responsible for managing the lifecycle of the FactoryBean
instance, not the lifecycle of the objects created by the FactoryBean. Therefore,
a destroy method on an exposed bean object (such as java.)
will not be called automatically. Instead, a FactoryBean should implement
DisposableBean and delegate any such close call to the underlying object.
Finally, FactoryBean objects participate in the containing BeanFactory's synchronization of bean creation. There is usually no need for internal synchronization other than for purposes of lazy initialization within the FactoryBean itself (or the like).
org.springframework.beans.factory.BeanFactory , org.springframework.aop.framework.ProxyFactoryBean , org.springframework.jndi.JndiObjectFactoryBean
| Modifier and Type | Field and Description |
|---|---|
| public static final String | OBJECT_TYPE_ATTRIBUTE
The name of an attribute that can be
|
| Modifier and Type | Method and Description |
|---|---|
| public T | Returns: an instance of the bean (can benull)Return an instance (possibly shared or independent) of the object managed by this factory. |
| public Class | Returns: the type of object that this FactoryBean creates, ornull if not known at the time of the callReturn the type of object that this FactoryBean creates,
or |
| public default boolean | Returns: whether the exposed object is a singletonIs the object managed by this factory a singleton? |
| OBJECT_TYPE_ATTRIBUTE | back to summary |
|---|---|
| public static final String OBJECT_TYPE_ATTRIBUTE The name of an attribute that can be
| |
| getObject | back to summary |
|---|---|
| public T getObject() throws Exception Return an instance (possibly shared or independent) of the object managed by this factory. As with a If this FactoryBean is not fully initialized yet at the time of
the call (for example because it is involved in a circular reference),
throw a corresponding As of Spring 2.0, FactoryBeans are allowed to return
| |
| getObjectType | back to summary |
|---|---|
| public Class Return the type of object that this FactoryBean creates,
or 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
| |
| isSingleton | back to summary |
|---|---|
| public default boolean isSingleton() Is the object managed by this factory a singleton? That is,
will Note If a FactoryBean indicates to hold a singleton object,
the object returned from 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 The default implementation returns
| |