* Interface to be implemented by objects used within a {@link 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.
beanfactory 使用的对象实现的接口,他们本身就是单个对象的工厂,如果一个bean实现了这个接口,这个bean就是一个对象的公开工厂,而不是直接作为自己bean的实例。
大意是:factorybean 会提供一些复杂bean的实现,提供给beanFactory使用,实现了这个接口的bean ,spring的beanfactory的getBean方法获取的不是这个bean 的实例,而是他内部getObject返回对象的实例。
A bean that implements this interface cannot be used as a normal bean.</b>
* A FactoryBean is defined in a bean style, but the object exposed for bean
* references ({@link #getObject()}) is always the object that it creates.
实现了这个接口的bean不是普通的bean,一个factorybean以一个bean的方式定义,但是bean引用公开的对象总是他所创建的对象。
FactoryBeans can support singletons and prototypes, and can either create
objects lazily on demand or eagerly on startup. The {@link SmartFactoryBean}
interface allows for exposing more fine-grained behavioral metadata.
factoryBean支持创建单例多例,也可以根据需要创建懒加载的对象或者启动的时候需要创建的对象。SmartFactoryBean 这个接口允许
暴露更多细粒度的行为元数据。
* <p>This interface is heavily used within the framework itself, for example for
* the AOP {@link org.springframework.aop.framework.ProxyFactoryBean} or the
* {@link org.springframework.jndi.JndiObjectFactoryBean}. It can be used for
* custom components as well; however, this is only common for infrastructure code.
这个接口被框架本身重度使用,例如aop(ProxyFactoryBean)和JndiObjectFactoryBean,他也能用来自定义组件。但是这是适用于基础架构代码。
* <p><b>{@code FactoryBean} is a programmatic contract. Implementations are not
* supposed to rely on annotation-driven injection or other reflective facilities.</b>
* {@link #getObjectType()} {@link #getObject()} invocations may arrive early in
* the bootstrap process, even ahead of any post-processor setup. If you need access
* other beans, implement {@link BeanFactoryAware} and obtain them programmatically.
factoryBean是一个编程契约,实现不应该依赖注解方式注入或者其他反射工具。里面的方法getObject,getObjectType可能在启动过程提前调用。甚至所有post-processer前面。如果你需要用其他bean,实现BeanFactoryAware接口,通过编程方式获取。
<p>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).
最后,factorybean的创建参与包含beanfactory的bean的创建同步,内部的同步过程是没必要的,代替的是factory的自己延迟初始化(或者类似)。
/**
* Return an instance (possibly shared or independent) of the object
* managed by this factory.
* <p>As with a {@link BeanFactory}, this allows support for both the
* Singleton and Prototype design pattern.
* <p>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 {@link FactoryBeanNotInitializedException}.
* <p>As of Spring 2.0, FactoryBeans are allowed to return {@code null}
* objects. The factory will consider this as normal value to be used; it
* will not throw a FactoryBeanNotInitializedException in this case anymore.
* FactoryBean implementations are encouraged to throw
* FactoryBeanNotInitializedException themselves now, as appropriate.
* @return an instance of the bean (can be {@code null})
* @throws Exception in case of creation errors
* @see FactoryBeanNotInitializedException
*/
@Nullable
T getObject() throws Exception;
返回一个被工厂管理的对象,可能是多例或者单例。和beanfactory一样,支持单例多例设计模式。如果这个factorybean在调用的时候还没完全初始化成功。(例如,包含一个循环依赖)。抛出一个相应的FactoryBeanNotInitializedException 异常。
spring2.0 以后,factory允许返回空对象。工厂将把他作为正常值一样使用。这种情况将不再抛出一个FactoryBeanNotInitializedException 异常,FactoryBean的实现类将抛出一个FactoryBeanNotInitializedException 异常,是情况而定。
/**
* Return the type of object that this FactoryBean creates,
* or {@code null} if not known in advance.
* <p>This allows one to check for specific types of beans without
* instantiating objects, for example on autowiring.
* <p>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.
* <p>This method can be called <i>before</i> 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.
* <p><b>NOTE:</b> Autowiring will simply ignore FactoryBeans that return
* {@code null} here. Therefore it is highly recommended to implement
* this method properly, using the current state of the FactoryBean.
* @return the type of object that this FactoryBean creates,
* or {@code null} if not known at the time of the call
* @see ListableBeanFactory#getBeansOfType
*/
@Nullable
Class<?> getObjectType();
返回FactoryBean创建的对象的类型或者null,如果提前不知道的话,这样就可以在bean没初始化的时候监测bean的具体类型,例如autowiring.
在接口实现类正在创建一个单例对象的时候,这个方法应该尽力避免单例对象的创建,而是提前预测这个对象的类型。
对于多例的对象,也建议在这返回一个有意义的对象类型。这个方法可以在这个factorybean完全初始化之前调用。在初始化的过程中不是强依赖于创建状态的,当然,如果这些状态存在也可以使用。
注意:antowireing 将简单的忽略返回null的factoryBean,因此强烈要求正确实现这个方法。用这个factorybean的当前状态,
返回:这个factorybean创建的对象类型,如果调用的时候还不知道的话返回null.
/**
* Is the object managed by this factory a singleton? That is,
* will {@link #getObject()} always return the same object
* (a reference that can be cached)?
* <p><b>NOTE:</b> If a FactoryBean indicates to hold a singleton object,
* the object returned from {@code getObject()} might get cached
* by the owning BeanFactory. Hence, do not return {@code true}
* unless the FactoryBean always exposes the same reference.
* <p>The singleton status of the FactoryBean itself will generally
* be provided by the owning BeanFactory; usually, it has to be
* defined as singleton there.
* <p><b>NOTE:</b> This method returning {@code false} does not
* necessarily indicate that returned objects are independent instances.
* An implementation of the extended {@link SmartFactoryBean} interface
* may explicitly indicate independent instances through its
* {@link SmartFactoryBean#isPrototype()} method. Plain {@link FactoryBean}
* implementations which do not implement this extended interface are
* simply assumed to always return independent instances if the
* {@code isSingleton()} implementation returns {@code false}.
* <p>The default implementation returns {@code true}, since a
* {@code FactoryBean} typically manages a singleton instance.
* @return whether the exposed object is a singleton
* @see #getObject()
* @see SmartFactoryBean#isPrototype()
*/
default boolean isSingleton() {
return true;
}
这个 工厂管理的对象是单例的么?如果是,那么getobject 方法会一直返回同一个对象。
注意:如果一个factoryBean指示保存的是单例对象,那么getobject 获得的对象可能会被拥有的beanfactory缓存。因此,不要返回true,除非factory bean 老是公开同样的对象引用。
这个factoryBean本身的单例状态通常有所拥有的的beanFactory提供,通常它必须是单例状态。
注意:这个方法返回false 并不一定说明返回的对象是多例的,一个扩展的接口实现SmartFactoryBean有个方法isPrototype可以明确指示这个返回的对象是多例的。
没有实现这个扩展接口的普通接口实现类似(factortyBean),如果isSingleton返回false,只是假设每次返回独立实例。
因为factoryBean常见管理一个单例实例,所以接口默认返回false。