Spring之FactoryBean(工厂Bean)

本文介绍了Spring框架中的FactoryBean接口,它是用于定义自定义服务的核心接口。文章解释了FactoryBean如何支持Singleton和Prototype设计模式,并展示了如何通过BeanFactory或ApplicationContext获取由FactoryBean创建的对象实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringFactoryBean(工厂Bean

org.springframework.beans.factory.FactoryBean是一个接口,是Spring中用于定义自定义服务的核心接口,他本身是在bean工厂中定义的一个bean,同时又是用于创建另一个Spring服务对象的工厂。

FactoryBean.java

package org.springframework.beans.factory;
public interface FactoryBean {

<fontsize=3>  /**
   * 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.
   @return an instance of the bean (should 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();

}

下面是FactoryBeangetObject方法的调用层次。

File.aspx?id=bdd75scdqc996我们可以看出,当我们用BeanFactoryApplicationContext去那getBean()时,如果这个Bean是被一个BeanFactory,那么BeanFactory得到的会是FactoryBean.getObject()的对象。

<spanlang=zh-cn>下面是AbstractBeanFactorygetObjectForSharedInstance(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通过FactoryBean来提供内置的许多有用的核心服务。当然你也可以实现自己的FactoryBean,但是你大不可以不必这样做,Spring已经提供了大量的。

  • org.springframework.jndi JndiObjectFactoryBeanJNDI LookUp

  • org.springframework.orm.hibernate3. LocalSessionFactoryBean:配置本地的Hibernate SessionFactory的工厂bean

  • org.springframework.aop.framework ProxyFactoryBean:通用的用于获得AOP代理的工厂bean

  • org.springframework.transaction.interceptor.TransactionProxyFactoryBean:用于为对象创建事务代理,用于实现简介易用申明性事务管理。

  • …………etc

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值