1. /**
    2. * 该接口的实现会和BeanFactory搭配使用(在populateBean)
    3. * which are themselves factories. If a bean implements this interface,
    4. * it is used as a factory for an object to expose, not directly as a bean
    5. * instance that will be exposed itself.
    6. *
    7. * <p><b>NB: A bean that implements this interface cannot be used as a
    8. * normal bean.</b> A FactoryBean is defined in a bean style, but the
    9. * object exposed for bean references ({@link #getObject()} is always
    10. * the object that it creates.
    11. *
    12. * <p>FactoryBeans can support singletons and prototypes, and can
    13. * either create objects lazily on demand or eagerly on startup.
    14. * The {@link SmartFactoryBean} interface allows for exposing
    15. * more fine-grained behavioral metadata.
    16. *
    17. * <p>This interface is heavily used within the framework itself, for
    18. * example for the AOP {@link org.springframework.aop.framework.ProxyFactoryBean}
    19. * or the {@link org.springframework.jndi.JndiObjectFactoryBean}.
    20. * It can be used for application components as well; however,
    21. * this is not common outside of infrastructure code.
    22. *
    23. * <p><b>NOTE:</b> FactoryBean objects participate in the containing
    24. * BeanFactory's synchronization of bean creation. There is usually no
    25. * need for internal synchronization other than for purposes of lazy
    26. * initialization within the FactoryBean itself (or the like).
    27. *
    28. * @author Rod Johnson
    29. * @author Juergen Hoeller
    30. * @since 08.03.2003
    31. * @see org.springframework.beans.factory.BeanFactory
    32. * @see org.springframework.aop.framework.ProxyFactoryBean
    33. * @see org.springframework.jndi.JndiObjectFactoryBean
    34. */
    35. public interface FactoryBean<T> {
    36. /**
    37. * Return an instance (possibly shared or independent) of the object
    38. * managed by this factory.
    39. * <p>As with a {@link BeanFactory}, this allows support for both the
    40. * Singleton and Prototype design pattern.
    41. * <p>If this FactoryBean is not fully initialized yet at the time of
    42. * the call (for example because it is involved in a circular reference),
    43. * throw a corresponding {@link FactoryBeanNotInitializedException}.
    44. * <p>As of Spring 2.0, FactoryBeans are allowed to return {@code null}
    45. * objects. The factory will consider this as normal value to be used; it
    46. * will not throw a FactoryBeanNotInitializedException in this case anymore.
    47. * FactoryBean implementations are encouraged to throw
    48. * FactoryBeanNotInitializedException themselves now, as appropriate.
    49. * @return an instance of the bean (can be {@code null})
    50. * @throws Exception in case of creation errors
    51. * @see FactoryBeanNotInitializedException
    52. */
    53. T getObject() throws Exception;
    54. /**
    55. * Return the type of object that this FactoryBean creates,
    56. * or {@code null} if not known in advance.
    57. * <p>This allows one to check for specific types of beans without
    58. * instantiating objects, for example on autowiring.
    59. * <p>In the case of implementations that are creating a singleton object,
    60. * this method should try to avoid singleton creation as far as possible;
    61. * it should rather estimate the type in advance.
    62. * For prototypes, returning a meaningful type here is advisable too.
    63. * <p>This method can be called <i>before</i> this FactoryBean has
    64. * been fully initialized. It must not rely on state created during
    65. * initialization; of course, it can still use such state if available.
    66. * <p><b>NOTE:</b> Autowiring will simply ignore FactoryBeans that return
    67. * {@code null} here. Therefore it is highly recommended to implement
    68. * this method properly, using the current state of the FactoryBean.
    69. * @return the type of object that this FactoryBean creates,
    70. * or {@code null} if not known at the time of the call
    71. * @see ListableBeanFactory#getBeansOfType
    72. */
    73. Class<?> getObjectType();
    74. /**
    75. * Is the object managed by this factory a singleton? That is,
    76. * will {@link #getObject()} always return the same object
    77. * (a reference that can be cached)?
    78. * <p><b>NOTE:</b> If a FactoryBean indicates to hold a singleton object,
    79. * the object returned from {@code getObject()} might get cached
    80. * by the owning BeanFactory. Hence, do not return {@code true}
    81. * unless the FactoryBean always exposes the same reference.
    82. * <p>The singleton status of the FactoryBean itself will generally
    83. * be provided by the owning BeanFactory; usually, it has to be
    84. * defined as singleton there.
    85. * <p><b>NOTE:</b> This method returning {@code false} does not
    86. * necessarily indicate that returned objects are independent instances.
    87. * An implementation of the extended {@link SmartFactoryBean} interface
    88. * may explicitly indicate independent instances through its
    89. * {@link SmartFactoryBean#isPrototype()} method. Plain {@link FactoryBean}
    90. * implementations which do not implement this extended interface are
    91. * simply assumed to always return independent instances if the
    92. * {@code isSingleton()} implementation returns {@code false}.
    93. * @return whether the exposed object is a singleton
    94. * @see #getObject()
    95. * @see SmartFactoryBean#isPrototype()
    96. */
    97. boolean isSingleton();
    98. }

    使用过程

    1. AbstractApplicationContext#refresh
    2. AbstractBeanFactory#getBean
    3. AbstractBeanFactory#doGetBean
    4. AbstractBeanFactory#getObjectForBeanInstance
    5. AbstractBeanFactory#getObjectFromFactoryBean
    6. AbstractBeanFactory#doGetObjectFromFactoryBean
    7. object = factory.getObject();