GVKun编程网logo

spring工厂方法和factory bean有什么区别?(spring beanfactory与factorybean)

15

对于想了解spring工厂方法和factorybean有什么区别?的读者,本文将是一篇不可错过的文章,我们将详细介绍springbeanfactory与factorybean,并且为您提供关于Bean

对于想了解spring工厂方法和factory bean有什么区别?的读者,本文将是一篇不可错过的文章,我们将详细介绍spring beanfactory与factorybean,并且为您提供关于BeanFactory 和 ApplicationContext有什么区别?、BeanFactory 和FactoryBean有什么区别、BeanFactory和FactoryBean的区别?、org.springframework.beans.factory.access.BeanFactoryLocator的实例源码的有价值信息。

本文目录一览:

spring工厂方法和factory bean有什么区别?(spring beanfactory与factorybean)

spring工厂方法和factory bean有什么区别?(spring beanfactory与factorybean)

我是新泉。在Bean标签中,我找到了factory-method和factory-bean Attributes。factory-
method和factory-bean有什么区别?

我正在使用工厂方法调用getInstance静态方法来创建单例对象。

工厂豆有什么用?


对于给定的答复,我的理解是

工厂方法用于调用静态方法以在同一bean类中创建对象。

Factory-bean用于基于工厂设计模式创建对象。

例如:-我通过传递我的蔬菜名称(在这种情况下为EggPlant),从VegetableFactory(这将返回被询问的蔬菜对象)类中询问EggPlant对象。

如果我输入错误,请更正?

答案1

小编典典

工厂方法工厂
设计模式之间的区别基本上是相同的,在底部有一点说明。一种是用于获取特定类的实例的方法,另一种是负责创建对象的完整对象,其中包括创建对象所需的所有逻辑。

FactoryBean的界面文档指出:

由BeanFactory中使用的对象(本身就是工厂)实现的接口。如果bean实现此接口,则它将用作对象公开的工厂,而不是直接用作将自身公开的bean实例。

另外,此对象 不用作Bean实例 ,而是通过其getObject方法用作实例提供程序。


更新资料

搜索factory-methodover的使用FactoryBean,似乎它经常与遗留的singleton
bean一起使用,以获取基础实例,但是这种方法不提供对初始化方法的支持,例如,用于init初始化给定集合的方法属性。

在这种情况下,您要么必须在使用该类之前自己调用它,要么定义一个用于处理初始化的包装器,或者使用诸如之类的其他机制MethodInvokingFactoryBean


更新2

严格来说,a
FactoryBean是用来管理特定类型的。实际上,您将拥有一个EggPlantFactory,而不是一个,VegetableFactory因为接口getObject定义的方法FactoryBean不支持参数。

BeanFactory 和 ApplicationContext有什么区别?

BeanFactory 和 ApplicationContext有什么区别?

BeanFactory 和 ApplicationContext有什么区别?

BeanFactory和ApplicationContext是Spring的两大核心接口,都可以当做Spring的容器。其中 ApplicationContext是BeanFactory的子接口。

依赖关系

BeanFactory:是Spring里面最底层的接口,包含了各种Bean的定义,读取bean配置文档, 管理bean的加载、实例化,控制bean的生命周期,维护bean之间的依赖关系

ApplicationContext接口作为BeanFactory的派生,除了提供BeanFactory所具有的功能外,还提 供了更完整的框架功能:

  • 继承MessageSource,因此支持国际化。

  • 统一的资源文件访问方式。

  • 提供在监听器中注册bean的事件。

  • 同时加载多个配置文件。

  • 载入多个(有继承关系)上下文 ,使得每一个上下文都专注于一个特定的层次,比如应用的 web层。

加载方式

BeanFactroy采用的是延迟加载形式来注入Bean的,即只有在使用到某个Bean时(调用 getBean()),才对该Bean进行加载实例化。

ApplicationContext,它是在容器启动时,一次性创建了所有的Bean。

注册方式

BeanFactory和ApplicationContext都支持BeanPostProcessor、 BeanFactoryPostProcessor的使用,但两者之间的区别是:BeanFactory需要手动注册,而 ApplicationContext则是自动注册。

BeanFactory 和FactoryBean有什么区别

BeanFactory 和FactoryBean有什么区别

相同点:都是用来创建bean对象的  

不同点:使用beanfactory创建对象的时候, 必须要遵循严格的生命周期流程,太复杂了,,如果想要简单的自定义某个对象的创建, 同时创建完成的对象想交给spring来管理, 那么就需要实现                         FactroyBean接口了

                      isSingleton:是否是单例对象

                     getobjectType:获取返回对象的类型
                     getobject:自定义创建对象的过程(new,反射,动态代理)

 

   一个是复杂详细版  一个是简约版

BeanFactory和FactoryBean的区别?

BeanFactory和FactoryBean的区别?

BeanFactory:是一个工厂(其实是构建了一个spring上下文的环境,容器),用来管理和获取很多Bean对象,

FactoryBean:是一个Bean生成工具,是用来获取一种类型对象的Bean,它是构造Bean实例的一种方式。


org.springframework.beans.factory.access.BeanFactoryLocator的实例源码

org.springframework.beans.factory.access.BeanFactoryLocator的实例源码

项目:lams    文件:ContextSingletonbeanfactoryLocator.java   
/**
 * Returns an instance which uses the the specified selector,as the name of the
 * deFinition file(s). In the case of a name with a Spring "classpath*:" prefix,* or with no prefix,which is treated the same,the current thread's context class
 * loader's {@code getResources} method will be called with this value to get
 * all resources having that name. These resources will then be combined to form a
 * deFinition. In the case where the name uses a Spring "classpath:" prefix,or
 * a standard URL prefix,then only one resource file will be loaded as the
 * deFinition.
 * @param selector the location of the resource(s) which will be read and
 * combined to form the deFinition for the beanfactoryLocator instance.
 * Any such files must form a valid ApplicationContext deFinition.
 * @return the corresponding beanfactoryLocator instance
 * @throws BeansException in case of factory loading failure
 */
public static beanfactoryLocator getInstance(String selector) throws BeansException {
    String resourceLocation = selector;
    if (resourceLocation == null) {
        resourceLocation = DEFAULT_RESOURCE_LOCATION;
    }

    // For backwards compatibility,we prepend "classpath*:" to the selector name if there
    // is no other prefix (i.e. "classpath*:","classpath:",or some URL prefix).
    if (!ResourcePatternUtils.isUrl(resourceLocation)) {
        resourceLocation = ResourcePatternResolver.CLAsspATH_ALL_URL_PREFIX + resourceLocation;
    }

    synchronized (instances) {
        if (logger.isTraceEnabled()) {
            logger.trace("ContextSingletonbeanfactoryLocator.getInstance(): instances.hashCode=" +
                    instances.hashCode() + ",instances=" + instances);
        }
        beanfactoryLocator bfl = instances.get(resourceLocation);
        if (bfl == null) {
            bfl = new ContextSingletonbeanfactoryLocator(resourceLocation);
            instances.put(resourceLocation,bfl);
        }
        return bfl;
    }
}
项目:spring4-understanding    文件:ContextSingletonbeanfactoryLocator.java   
/**
 * Returns an instance which uses the the specified selector,bfl);
        }
        return bfl;
    }
}
项目:spring4-understanding    文件:ContextSingletonbeanfactoryLocatorTests.java   
/**
 * This test can run multiple times,but due to static keyed lookup of the locators,* 2nd and subsequent calls will actually get back same locator instance. This is not
 * really an issue,since the contained bean factories will still be loaded and released.
 */
@Override
@Test
public void testGetInstance() {
    // Try with and without 'classpath*:' prefix,and with 'classpath:' prefix.
    beanfactoryLocator facLoc = ContextSingletonbeanfactoryLocator.getInstance(
            ClassUtils.addResourcePathToPackagePath(CLASS,CONTEXT));
    getInstanceTest1(facLoc);

    facLoc = ContextSingletonbeanfactoryLocator.getInstance(
            "classpath*:" + ClassUtils.addResourcePathToPackagePath(CLASS,CONTEXT));
    getInstanceTest2(facLoc);

    // This will actually get another locator instance,as the key is the resource name.
    facLoc = ContextSingletonbeanfactoryLocator.getInstance(
            "classpath:" + ClassUtils.addResourcePathToPackagePath(CLASS,CONTEXT));
    getInstanceTest3(facLoc);
}
项目:my-spring-cache-redis    文件:ContextSingletonbeanfactoryLocator.java   
/**
 * Returns an instance which uses the the specified selector,bfl);
        }
        return bfl;
    }
}
项目:spring    文件:ContextSingletonbeanfactoryLocator.java   
/**
 * Returns an instance which uses the specified selector,bfl);
        }
        return bfl;
    }
}
项目:jspresso-ce    文件:AbstractStartup.java   
/**
 * Gets the applicationContext.
 *
 * @return the applicationContext.
 */
protected beanfactory getApplicationContext() {
  try {
    if (applicationContext == null) {
      synchronized (LOCK) {
        if (applicationContext == null) {
          beanfactoryLocator bfl = SingletonbeanfactoryLocator.getInstance(getbeanfactorySelector());
          beanfactoryReference bf = bfl.usebeanfactory(getApplicationContextKey());
          beanfactory tempApplicationContext = bf.getFactory();
          if (tempApplicationContext instanceof ConfigurableApplicationContext) {
            ((ConfigurableApplicationContext) tempApplicationContext).registerShutdownHook();
          }
          applicationContext = tempApplicationContext;
        }
      }
    }
    return applicationContext;
  } catch (RuntimeException ex) {
    LOG.error("{} context Could not be instantiated.",getApplicationContextKey(),ex);
    throw ex;
  }
}
项目:class-guard    文件:ContextSingletonbeanfactoryLocator.java   
/**
 * Returns an instance which uses the the specified selector,bfl);
        }
        return bfl;
    }
}
项目:class-guard    文件:ContextSingletonbeanfactoryLocatorTests.java   
/**
 * This test can run multiple times,CONTEXT));
    getInstanceTest3(facLoc);
}
项目:zstack    文件:ComponentLoaderImpl.java   
public ComponentLoaderImpl () throws ClassNotFoundException,InstantiationException,illegalaccessexception,IOException {
    checkInit();
    beanfactoryLocator factoryLocator = ContextSingletonbeanfactoryLocator
            .getInstance(String.format("classpath:%s",CoreGlobalProperty.BEAN_REF_CONTEXT_CONF));
    beanfactoryReference ref = factoryLocator.usebeanfactory("parentContext");
    ioc = ref.getFactory();
}
项目:jspresso-ce    文件:AbstractbeanfactoryAwareContextListener.java   
private beanfactory getbeanfactory(ServletContextEvent event) {
  String beanfactorySelector = getbeanfactorySelector(event);
  String applicationContextKey = getApplicationContextKey(event);
  beanfactoryLocator bfl = SingletonbeanfactoryLocator
      .getInstance(beanfactorySelector);
  beanfactoryReference bf = bfl.usebeanfactory(applicationContextKey);
  beanfactory beanfactory = bf.getFactory();
  if (beanfactory instanceof ConfigurableApplicationContext) {
    ((ConfigurableApplicationContext) beanfactory).registerShutdownHook();
  }
  return beanfactory;
}
项目:community-edition-old    文件:JBPMSpringActionHandler.java   
/**
 * Construct
 */
protected JBPMSpringActionHandler()
{
    // The following implementation is derived from Spring Modules v0.4
    beanfactoryLocator factoryLocator = new JbpmFactoryLocator();
    beanfactoryReference factory = factoryLocator.usebeanfactory(null);
    initialiseHandler(factory.getFactory());
}
项目:community-edition-old    文件:WorkflowTaskInstance.java   
/**
 * Gets the JBPM Engine instance
 * 
 * @return JBPM Engine
 */
private JBPMEngine getJBPMEngine()
{
    if (jbpmEngine == null)
    {
        beanfactoryLocator factoryLocator = new JbpmFactoryLocator();
        beanfactoryReference factory = factoryLocator.usebeanfactory(null);
        if (jbpmEngineName == null) jbpmEngineName = "jbpm_engine";
        jbpmEngine = (JBPMEngine) factory.getFactory().getBean(jbpmEngineName);
        if (jbpmEngine == null) { throw new WorkflowException(
                    "Failed to retrieve JBPMEngine component"); }
    }
    return jbpmEngine;
}
项目:community-edition-old    文件:AlfrescoJobExecutor.java   
/**
 * Constructor
 */
public AlfrescoJobExecutor()
{
    beanfactoryLocator factoryLocator = new JbpmFactoryLocator();
    beanfactoryReference factory = factoryLocator.usebeanfactory(null);

    transactionService = (TransactionService)factory.getFactory().getBean(ServiceRegistry.TRANSACTION_SERVICE.getLocalName());
    jobLockService = (JobLockService)factory.getFactory().getBean(ServiceRegistry.JOB_LOCK_SERVICE.getLocalName());

    jbpmConfiguration = (JbpmConfiguration)factory.getFactory().getBean("jbpm_configuration");
}
项目:community-edition-old    文件:JBPMSpringAssignmentHandler.java   
/**
 * Construct
 */
protected JBPMSpringAssignmentHandler()
{
    // The following implementation is derived from Spring Modules v0.4
    beanfactoryLocator factoryLocator = new JbpmFactoryLocator();
    beanfactoryReference factory = factoryLocator.usebeanfactory(null);
    initialiseHandler(factory.getFactory());
}
项目:scriptella-etl    文件:EtlExecutorBean.java   
/**
 * This method obtains a global ThreadLocal class independent of the classloader (JVM-scope singleton).
 * The easiest solution is to use System.getProperties().get/put,but this solution violate
 * Properties contract and have other drawbacks.
 * <p>Current solution relies on the idea behind
 * {@link org.springframework.beans.factory.access.SingletonbeanfactoryLocator}. See also bug #4648
 *
 * @return Global ThreadLocal (JVM-scope singleton).
 */
@SuppressWarnings("unchecked")
private static ThreadLocal<beanfactory> getGlobalThreadLocal() {
    beanfactoryLocator locator = SingletonbeanfactoryLocator.getInstance(BEAN_FACTORY_XML_PATH);
    beanfactoryReference ref = locator.usebeanfactory(FACTORY_BEAN_NAME);
    StaticApplicationContext ctx = (StaticApplicationContext) ref.getFactory();
    if (!ctx.containsBean(THREAD_LOCAL_BEAN_NAME)) {
        ctx.registerSingleton(THREAD_LOCAL_BEAN_NAME,ThreadLocal.class);
    }
    return (ThreadLocal) ctx.getBean(THREAD_LOCAL_BEAN_NAME);
}
项目:spring4-understanding    文件:DefaultLocatorFactoryTests.java   
@Test
public void getInstance() {
    beanfactoryLocator bf = DefaultLocatorFactory.getInstance();
    beanfactoryLocator bf2 = DefaultLocatorFactory.getInstance();
    assertTrue(bf.equals(bf2));
}
项目:spring4-understanding    文件:DefaultLocatorFactoryTests.java   
@Test
public void getInstanceString() {
    beanfactoryLocator bf = DefaultLocatorFactory.getInstance("my-bean-refs.xml");
    beanfactoryLocator bf2 = DefaultLocatorFactory.getInstance("my-bean-refs.xml");
    assertTrue(bf.equals(bf2));
}
项目:spring4-understanding    文件:ContextLoader.java   
/**
 * Template method with default implementation (which may be overridden by a
 * subclass),to load or obtain an ApplicationContext instance which will be
 * used as the parent context of the root WebApplicationContext. If the
 * return value from the method is null,no parent context is set.
 * <p>The main reason to load a parent context here is to allow multiple root
 * web application contexts to all be children of a shared EAR context,or
 * alternately to also share the same parent context that is visible to
 * EJBs. For pure web applications,there is usually no need to worry about
 * having a parent context to the root web application context.
 * <p>The default implementation uses
 * {@link org.springframework.context.access.ContextSingletonbeanfactoryLocator},* configured via {@link #LOCATOR_FACTORY_SELECTOR_ParaM} and
 * {@link #LOCATOR_FACTORY_KEY_ParaM},to load a parent context
 * which will be shared by all other users of ContextsingletonbeanfactoryLocator
 * which also use the same configuration parameters.
 * @param servletContext current servlet context
 * @return the parent application context,or {@code null} if none
 * @see org.springframework.context.access.ContextSingletonbeanfactoryLocator
 */
protected ApplicationContext loadParentContext(ServletContext servletContext) {
    ApplicationContext parentContext = null;
    String locatorFactorySelector = servletContext.getinitParameter(LOCATOR_FACTORY_SELECTOR_ParaM);
    String parentContextKey = servletContext.getinitParameter(LOCATOR_FACTORY_KEY_ParaM);

    if (parentContextKey != null) {
        // locatorFactorySelector may be null,indicating the default "classpath*:beanRefContext.xml"
        beanfactoryLocator locator = ContextSingletonbeanfactoryLocator.getInstance(locatorFactorySelector);
        Log logger = LogFactory.getLog(ContextLoader.class);
        if (logger.isDebugEnabled()) {
            logger.debug("Getting parent context deFinition: using parent context key of '" +
                    parentContextKey + "' with beanfactoryLocator");
        }
        this.parentContextRef = locator.usebeanfactory(parentContextKey);
        parentContext = (ApplicationContext) this.parentContextRef.getFactory();
    }

    return parentContext;
}
项目:jspresso-ce    文件:ViewTester.java   
private ApplicationContext getApplicationContext() {
  beanfactoryLocator bfl = SingletonbeanfactoryLocator
      .getInstance(beanfactorySelector);
  beanfactoryReference bf = bfl.usebeanfactory(applicationContextKey);
  return (ApplicationContext) bf.getFactory();
}
项目:jspresso-ce    文件:EntityGenerator.java   
private beanfactoryReference getbeanfactoryReference() {
  beanfactoryLocator bfl = SingletonbeanfactoryLocator.getInstance(beanfactorySelector);
  beanfactoryReference bf = bfl.usebeanfactory(applicationContextKey);
  return bf;
}
项目:sakuli    文件:BeanLoader.java   
private static beanfactory getbeanfactory() {
    beanfactoryLocator bfl = SingletonbeanfactoryLocator.getInstance(CONTEXT_PATH);
    bfl.usebeanfactory("org.sakuli.app.root");
    return getbeanfactoryReference().getFactory();
}
项目:sakuli    文件:BeanLoader.java   
private static beanfactoryReference getbeanfactoryReference() {
    beanfactoryLocator bfl = SingletonbeanfactoryLocator.getInstance(CONTEXT_PATH);
    return bfl.usebeanfactory("org.sakuli.app.root");
}
项目:class-guard    文件:DefaultLocatorFactoryTests.java   
public void testGetInstance() {
    beanfactoryLocator bf = DefaultLocatorFactory.getInstance();
    beanfactoryLocator bf2 = DefaultLocatorFactory.getInstance();
    assertTrue(bf.equals(bf2));
}
项目:class-guard    文件:DefaultLocatorFactoryTests.java   
public void testGetInstanceString() {
    beanfactoryLocator bf = DefaultLocatorFactory.getInstance("my-bean-refs.xml");
    beanfactoryLocator bf2 = DefaultLocatorFactory.getInstance("my-bean-refs.xml");
    assertTrue(bf.equals(bf2));
}
项目:class-guard    文件:ContextLoader.java   
/**
 * Template method with default implementation (which may be overridden by a
 * subclass),indicating the default "classpath*:beanRefContext.xml"
        beanfactoryLocator locator = ContextSingletonbeanfactoryLocator.getInstance(locatorFactorySelector);
        Log logger = LogFactory.getLog(ContextLoader.class);
        if (logger.isDebugEnabled()) {
            logger.debug("Getting parent context deFinition: using parent context key of '" +
                    parentContextKey + "' with beanfactoryLocator");
        }
        this.parentContextRef = locator.usebeanfactory(parentContextKey);
        parentContext = (ApplicationContext) this.parentContextRef.getFactory();
    }

    return parentContext;
}
项目:opennmszh    文件:QoSD.java   
/**
     * <p>onStart</p>
     */
    protected void onStart() {


//Todo REMOVE EXAMPLE IMPORTER CODE
//      ThreadCategory.setPrefix(ImporterService.NAME);
//      m_status = Fiber.STARTING;
//      ThreadCategory.getInstance().debug("SPRING: thread.classLoader="+Thread.currentThread().getContextClassLoader());

//      beanfactoryLocator bfl = DefaultLocatorFactory.getInstance();
//      beanfactoryReference bf = bfl.usebeanfactory("daoContext");
//      ApplicationContext daoContext = (ApplicationContext) bf.getFactory();

//      m_context = new ClasspathXmlApplicationContext(new String[] { "/org/opennms/netmgt/importer/importer-context.xml" },daoContext);
//      ThreadCategory.getInstance().debug("SPRING: context.classLoader="+m_context.getClassLoader());
//      m_status = Fiber.RUNNING;

        ThreadCategory.getInstance().debug("SPRING: thread.classLoader="+Thread.currentThread().getContextClassLoader());

        // finds the already instantiated OpenNMS daoContext
        beanfactoryLocator bfl = DefaultLocatorFactory.getInstance();
        beanfactoryReference bf = bfl.usebeanfactory("daoContext");
        ApplicationContext daoContext = (ApplicationContext) bf.getFactory();


        // this chooses if we expect AlarmMonitor to run in seperate j2ee container ( Jboss ) or in local
        // OpenNMS spring container
        String qosdj2ee=System.getProperty("qosd.usej2ee");
        ThreadCategory.getInstance().info("QoSD System Property qosd.usej2ee=" + qosdj2ee );
        if ("true".equals(qosdj2ee)){
            ThreadCategory.getInstance().debug("QoSD using /org/openoss/opennms/spring/qosd/qosd-j2ee-context.xml");
            m_context = new ClasspathXmlApplicationContext(new String[] { "/org/openoss/opennms/spring/qosd/qosd-j2ee-context.xml" },daoContext);
        }
        else {
            ThreadCategory.getInstance().debug("QoSD using /org/openoss/opennms/spring/qosd/qosd-spring-context.xml");
            m_context = new ClasspathXmlApplicationContext(new String[] { "/org/openoss/opennms/spring/qosd/qosd-spring-context.xml" },daoContext);
        }

        ThreadCategory.getInstance().debug("SPRING: context.classLoader="+m_context.getClassLoader());

        getQoSD().init();
        getQoSD().start();


//      Todo remove original code       
//      ThreadCategory.setPrefix(QoSD.NAME);
//      m_status = Fiber.STARTING;
//      ThreadCategory.getInstance().debug("SPRING: thread.classLoader="+Thread.currentThread().getContextClassLoader());

//      // this chooses if we expect AlarmMonitor to run in seperate j2ee container ( Jboss ) or in local
//      // OpenNMS spring container
//      String qosdj2ee=System.getProperty("qosd.usej2ee");
//      ThreadCategory.getInstance().info("QoSD System Property qosd.usej2ee=" + qosdj2ee );
//      if ("true".equals(qosdj2ee)){
//      ThreadCategory.getInstance().debug("QoSD using /org/openoss/opennms/spring/qosd/qosd-j2ee-context.xml");
//      m_context = new ClasspathXmlApplicationContext(new String[] { "/org/openoss/opennms/spring/qosd/qosd-j2ee-context.xml" });
//      }
//      else {
//      ThreadCategory.getInstance().debug("QoSD using /org/openoss/opennms/spring/qosd/qosd-spring-context.xml");
//      m_context = new ClasspathXmlApplicationContext(new String[] { "/org/openoss/opennms/spring/qosd/qosd-spring-context.xml" });
//      }

//      ThreadCategory.getInstance().debug("SPRING: context.classLoader="+m_context.getClassLoader());

//      getQoSD().init();
//      getQoSD().start();


//      m_status = Fiber.RUNNING;
    }
项目:opennmszh    文件:QoSDrx.java   
/**
     * <p>onStart</p>
     */
    protected void onStart() {
//      Todo REMOVE EXAMPLE IMPORTER CODE
//      ThreadCategory.setPrefix(ImporterService.NAME);
//      m_status = Fiber.STARTING;
//      ThreadCategory.getInstance().debug("SPRING: thread.classLoader="+Thread.currentThread().getContextClassLoader());

//      beanfactoryLocator bfl = DefaultLocatorFactory.getInstance();
//      beanfactoryReference bf = bfl.usebeanfactory("daoContext");
//      ApplicationContext daoContext = (ApplicationContext) bf.getFactory();

//      m_context = new ClasspathXmlApplicationContext(new String[] { "/org/opennms/netmgt/importer/importer-context.xml" },daoContext);
//      ThreadCategory.getInstance().debug("SPRING: context.classLoader="+m_context.getClassLoader());
//      m_status = Fiber.RUNNING;



        ThreadCategory.getInstance().debug("SPRING: thread.classLoader="+Thread.currentThread().getContextClassLoader());

        // finds the already instantiated OpenNMS daoContext
        beanfactoryLocator bfl = DefaultLocatorFactory.getInstance();
        beanfactoryReference bf = bfl.usebeanfactory("daoContext");
        ApplicationContext daoContext = (ApplicationContext) bf.getFactory();

        ThreadCategory.getInstance().debug("QoSDrx using /org/openoss/opennms/spring/qosdrx/qosdrx-spring-context.xml");
        m_context = new ClasspathXmlApplicationContext(new String[] { "/org/openoss/opennms/spring/qosdrx/qosdrx-spring-context.xml" },daoContext);
        ThreadCategory.getInstance().debug("SPRING: context.classLoader="+m_context.getClassLoader());

        getQoSDrx().init();
        getQoSDrx().start();

//Todo remove old code      
//      ThreadCategory.setPrefix(QoSDrx.NAME);
//      m_status = Fiber.STARTING;
//      ThreadCategory.getInstance().debug("SPRING: thread.classLoader="+Thread.currentThread().getContextClassLoader());
//
//      ThreadCategory.getInstance().debug("QoSDrx using /org/openoss/opennms/spring/qosdrx/qosdrx-spring-context.xml");
//      m_context = new ClasspathXmlApplicationContext(new String[] { "/org/openoss/opennms/spring/qosdrx/qosdrx-spring-context.xml" });
//      ThreadCategory.getInstance().debug("SPRING: context.classLoader="+m_context.getClassLoader());
//
//      getQoSDrx().init();
//      getQoSDrx().start();
//
//
//      m_status = Fiber.RUNNING;
    }
项目:opennmszh    文件:Correlator.java   
/**
 * Initialization.
 * 
 * Retrieves the Spring context for the correlator.
 */
public void init() {
    final beanfactoryLocator bfl = DefaultLocatorFactory.getInstance();
    final beanfactoryReference bf = bfl.usebeanfactory("correlatorContext");
    m_correlator = (org.opennms.netmgt.correlation.Correlator) bf.getFactory().getBean("correlator");
}
项目:OpenNMS    文件:QoSD.java   
/**
     * <p>onStart</p>
     */
    protected void onStart() {


//Todo REMOVE EXAMPLE IMPORTER CODE
//      ThreadCategory.setPrefix(ImporterService.NAME);
//      m_status = Fiber.STARTING;
//      ThreadCategory.getInstance().debug("SPRING: thread.classLoader="+Thread.currentThread().getContextClassLoader());

//      beanfactoryLocator bfl = DefaultLocatorFactory.getInstance();
//      beanfactoryReference bf = bfl.usebeanfactory("daoContext");
//      ApplicationContext daoContext = (ApplicationContext) bf.getFactory();

//      m_context = new ClasspathXmlApplicationContext(new String[] { "/org/opennms/netmgt/importer/importer-context.xml" },daoContext);
        }

        ThreadCategory.getInstance().debug("SPRING: context.classLoader="+m_context.getClassLoader());

        getQoSD().init();
        getQoSD().start();


//      Todo remove original code       
//      ThreadCategory.setPrefix(QoSD.NAME);
//      m_status = Fiber.STARTING;
//      ThreadCategory.getInstance().debug("SPRING: thread.classLoader="+Thread.currentThread().getContextClassLoader());

//      // this chooses if we expect AlarmMonitor to run in seperate j2ee container ( Jboss ) or in local
//      // OpenNMS spring container
//      String qosdj2ee=System.getProperty("qosd.usej2ee");
//      ThreadCategory.getInstance().info("QoSD System Property qosd.usej2ee=" + qosdj2ee );
//      if ("true".equals(qosdj2ee)){
//      ThreadCategory.getInstance().debug("QoSD using /org/openoss/opennms/spring/qosd/qosd-j2ee-context.xml");
//      m_context = new ClasspathXmlApplicationContext(new String[] { "/org/openoss/opennms/spring/qosd/qosd-j2ee-context.xml" });
//      }
//      else {
//      ThreadCategory.getInstance().debug("QoSD using /org/openoss/opennms/spring/qosd/qosd-spring-context.xml");
//      m_context = new ClasspathXmlApplicationContext(new String[] { "/org/openoss/opennms/spring/qosd/qosd-spring-context.xml" });
//      }

//      ThreadCategory.getInstance().debug("SPRING: context.classLoader="+m_context.getClassLoader());

//      getQoSD().init();
//      getQoSD().start();


//      m_status = Fiber.RUNNING;
    }
项目:OpenNMS    文件:QoSDrx.java   
/**
     * <p>onStart</p>
     */
    protected void onStart() {
//      Todo REMOVE EXAMPLE IMPORTER CODE
//      ThreadCategory.setPrefix(ImporterService.NAME);
//      m_status = Fiber.STARTING;
//      ThreadCategory.getInstance().debug("SPRING: thread.classLoader="+Thread.currentThread().getContextClassLoader());

//      beanfactoryLocator bfl = DefaultLocatorFactory.getInstance();
//      beanfactoryReference bf = bfl.usebeanfactory("daoContext");
//      ApplicationContext daoContext = (ApplicationContext) bf.getFactory();

//      m_context = new ClasspathXmlApplicationContext(new String[] { "/org/opennms/netmgt/importer/importer-context.xml" },daoContext);
        ThreadCategory.getInstance().debug("SPRING: context.classLoader="+m_context.getClassLoader());

        getQoSDrx().init();
        getQoSDrx().start();

//Todo remove old code      
//      ThreadCategory.setPrefix(QoSDrx.NAME);
//      m_status = Fiber.STARTING;
//      ThreadCategory.getInstance().debug("SPRING: thread.classLoader="+Thread.currentThread().getContextClassLoader());
//
//      ThreadCategory.getInstance().debug("QoSDrx using /org/openoss/opennms/spring/qosdrx/qosdrx-spring-context.xml");
//      m_context = new ClasspathXmlApplicationContext(new String[] { "/org/openoss/opennms/spring/qosdrx/qosdrx-spring-context.xml" });
//      ThreadCategory.getInstance().debug("SPRING: context.classLoader="+m_context.getClassLoader());
//
//      getQoSDrx().init();
//      getQoSDrx().start();
//
//
//      m_status = Fiber.RUNNING;
    }
项目:OpenNMS    文件:Correlator.java   
/**
 * <p>init</p>
 */
public void init() {
    final beanfactoryLocator bfl = DefaultLocatorFactory.getInstance();
       final beanfactoryReference bf = bfl.usebeanfactory("correlatorContext");
       m_correlator = (org.opennms.netmgt.correlation.Correlator) bf.getFactory().getBean("correlator");
   }
项目:tavern    文件:SpringTavernContextLoader.java   
/**
 * Template method with default implementation (which may be overridden by a
 * subclass),or <code>null</code> if none
 * @throws BeansException if the context Couldn't be initialized
 * @see org.springframework.context.access.ContextSingletonbeanfactoryLocator
 */
protected ApplicationContext loadParentContext(ServletContext servletContext)
        throws BeansException {

    ApplicationContext parentContext = null;
    String locatorFactorySelector = servletContext.getinitParameter(LOCATOR_FACTORY_SELECTOR_ParaM);
    String parentContextKey = servletContext.getinitParameter(LOCATOR_FACTORY_KEY_ParaM);

    if (parentContextKey != null) {
        // locatorFactorySelector may be null,indicating the default "classpath*:beanRefContext.xml"
        beanfactoryLocator locator = ContextSingletonbeanfactoryLocator.getInstance(locatorFactorySelector);
        if (logger.isDebugEnabled()) {
            logger.debug("Getting parent context deFinition: using parent context key of '" +
                    parentContextKey + "' with beanfactoryLocator");
        }
        this.parentContextRef = locator.usebeanfactory(parentContextKey);
        parentContext = (ApplicationContext) this.parentContextRef.getFactory();
    }

    return parentContext;
}
项目:lams    文件:ContextSingletonbeanfactoryLocator.java   
/**
 * Returns an instance which uses the default "classpath*:beanRefContext.xml",as
 * the name of the deFinition file(s). All resources returned by the current
 * thread's context class loader's {@code getResources} method with this
 * name will be combined to create a deFinition,which is just a beanfactory.
 * @return the corresponding beanfactoryLocator instance
 * @throws BeansException in case of factory loading failure
 */
public static beanfactoryLocator getInstance() throws BeansException {
    return getInstance(null);
}
项目:lams    文件:DefaultLocatorFactory.java   
/**
 * Return an instance object implementing beanfactoryLocator. This will normally
 * be a singleton instance of the specific ContextSingletonbeanfactoryLocator class,* using the default resource selector.
 */
public static beanfactoryLocator getInstance() throws FatalBeanException {
    return ContextSingletonbeanfactoryLocator.getInstance();
}
项目:lams    文件:DefaultLocatorFactory.java   
/**
 * Return an instance object implementing beanfactoryLocator. This will normally
 * be a singleton instance of the specific ContextSingletonbeanfactoryLocator class,* using the specified resource selector.
 * @param selector a selector variable which provides a hint to the factory as to
 * which instance to return.
 */
public static beanfactoryLocator getInstance(String selector) throws FatalBeanException {
    return ContextSingletonbeanfactoryLocator.getInstance(selector);
}
项目:lams    文件:SpringBeanAutowiringInterceptor.java   
/**
 * Determine the beanfactoryLocator to obtain the beanfactoryReference from.
 * <p>The default implementation exposes Spring's default
 * {@link ContextSingletonbeanfactoryLocator}.
 * @param target the target bean to autowire
 * @return the beanfactoryLocator to use (never {@code null})
 * @see org.springframework.context.access.ContextSingletonbeanfactoryLocator#getInstance()
 */
protected beanfactoryLocator getbeanfactoryLocator(Object target) {
    return ContextSingletonbeanfactoryLocator.getInstance();
}
项目:spring4-understanding    文件:ContextSingletonbeanfactoryLocator.java   
/**
 * Returns an instance which uses the default "classpath*:beanRefContext.xml",which is just a beanfactory.
 * @return the corresponding beanfactoryLocator instance
 * @throws BeansException in case of factory loading failure
 */
public static beanfactoryLocator getInstance() throws BeansException {
    return getInstance(null);
}
项目:spring4-understanding    文件:DefaultLocatorFactory.java   
/**
 * Return an instance object implementing beanfactoryLocator. This will normally
 * be a singleton instance of the specific ContextSingletonbeanfactoryLocator class,* using the default resource selector.
 */
public static beanfactoryLocator getInstance() throws FatalBeanException {
    return ContextSingletonbeanfactoryLocator.getInstance();
}
项目:spring4-understanding    文件:DefaultLocatorFactory.java   
/**
 * Return an instance object implementing beanfactoryLocator. This will normally
 * be a singleton instance of the specific ContextSingletonbeanfactoryLocator class,* using the specified resource selector.
 * @param selector a selector variable which provides a hint to the factory as to
 * which instance to return.
 */
public static beanfactoryLocator getInstance(String selector) throws FatalBeanException {
    return ContextSingletonbeanfactoryLocator.getInstance(selector);
}
项目:spring4-understanding    文件:SpringBeanAutowiringInterceptor.java   
/**
 * Determine the beanfactoryLocator to obtain the beanfactoryReference from.
 * <p>The default implementation exposes Spring's default
 * {@link ContextSingletonbeanfactoryLocator}.
 * @param target the target bean to autowire
 * @return the beanfactoryLocator to use (never {@code null})
 * @see org.springframework.context.access.ContextSingletonbeanfactoryLocator#getInstance()
 */
protected beanfactoryLocator getbeanfactoryLocator(Object target) {
    return ContextSingletonbeanfactoryLocator.getInstance();
}

关于spring工厂方法和factory bean有什么区别?spring beanfactory与factorybean的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于BeanFactory 和 ApplicationContext有什么区别?、BeanFactory 和FactoryBean有什么区别、BeanFactory和FactoryBean的区别?、org.springframework.beans.factory.access.BeanFactoryLocator的实例源码等相关内容,可以在本站寻找。

本文标签: