SpringのFactoryBean(工場Bean)


SpringのFactoryBean(工場Bean)
org.springframe ework.beans.factory.FactoryBeanはインターフェースであり、Springでカスタムサービスを定義するためのコアインターフェースであり、彼自身はbean工場で定義されたbeanであり、同時にもう一つのSpringサービス対象工場を作るためのものである.
FactoryBean.java
package org.springframe ework.beans.factory;public インターフェース FactoryBean {  /**   * リセット an instance (possibly shared or independent) 保存先 the object   * managed by this ファクトリー. As with a. BenFactory this allows   * support for ぼん the Singleton and Prottotype design pattern.   * 
If. this method returns null the ファクトリー will consider the   * FactoryBean as not フルー initialized and throw a. cores ponding   * FactoryBeanNot Initialized Exception.   * @return an instance 保存先 the bean (ショルダー not be  null; a null value
   * will be considered as an indication of incomplete initialization)
   * @throws Exception in case of creation errors
   * @see FactoryBeanNotInitializedException
<fontsize=3>   */
  Object getObject() throws Exception;
  Class getObjectType();
  boolean isSingleton();

}

FactoryBean getObject

BeanFactory ApplicationContext getBean()Bean BeanFactoryBeanFactory FactoryBean.getObject()

<spanlang=zh-cn> AbstractBeanFactory getObjectForSharedInstance(String,Object)

    // Now we have the bean instance, which may be a normal bean or a FactoryBean.
    // If it&aposs a FactoryBean, we use it to create a bean instance, unless the
    // caller actually wants a reference to the factory.
    if (beanInstance instanceof FactoryBean) {
      if (!isFactoryDereference(name)) {
        // Return bean instance from factory.
        FactoryBean factory = (FactoryBean) beanInstance; //Cast toFactoryBean.
        if (logger.isDebugEnabled()) {
          logger.debug("Bean with name &apos" + beanName + "&apos is a factory bean");
        }
        try {
          beanInstance = factory.getObject(); // Get the realObject
        }
        catch (Exception ex) {
          throw new BeanCreationException(beanName, "FactoryBean threw exception on object creation", ex);
        }
        if (beanInstance == null) {
          throw new FactoryBeanNotInitializedException(
              beanName, "FactoryBean returned null object: " +
              "probably not fully initialized (maybe due to circular bean reference)");
        }
      }
      else {
         // The user wants the factory itself.
        if (logger.isDebugEnabled()) {
          logger.debug("Calling code asked for FactoryBean instance for name &apos" + beanName + "&apos");
        }
      }
    }

Spring FactoryBeanFactoryBean, ,Spring

  • org.springframework.jndi JndiObjectFactoryBeanJNDI LookUp

  • org.springframework.orm.hibernate3. LocalSessionFactoryBeanHibernate SessionFactory bean

  • org.springframework.aop.framework ProxyFactoryBeanAOP bean

  • org.springframework.transaction.interceptor.TransactionProxyFactoryBean: , 。

  • …………etc

http://www.writely.com/View.aspx?docid=bdd75r9hmc98j