org.springframework.beans.factory.BeanFactory.isSingleton()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.5k)|赞(0)|评价(0)|浏览(67)

本文整理了Java中org.springframework.beans.factory.BeanFactory.isSingleton()方法的一些代码示例,展示了BeanFactory.isSingleton()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BeanFactory.isSingleton()方法的具体详情如下:
包路径:org.springframework.beans.factory.BeanFactory
类名称:BeanFactory
方法名:isSingleton

BeanFactory.isSingleton介绍

[英]Is this bean a shared singleton? That is, will #getBean always return the same instance?

Note: This method returning false does not clearly indicate independent instances. It indicates non-singleton instances, which may correspond to a scoped bean as well. Use the #isPrototype operation to explicitly check for independent instances.

Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.
[中]这个bean是共享的单例吗?也就是说,getBean是否总是返回相同的实例?
注意:此方法返回false并不能清楚地指示独立实例。它表示非单例实例,也可能对应于作用域bean。使用#isPrototype操作显式检查独立实例。
将别名转换回相应的规范bean名称。将询问父工厂是否在此工厂实例中找不到该bean。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public Object getAspectCreationMutex() {
  if (this.beanFactory.isSingleton(this.name)) {
    // Rely on singleton semantics provided by the factory -> no local lock.
    return null;
  }
  else if (this.beanFactory instanceof ConfigurableBeanFactory) {
    // No singleton guarantees from the factory -> let's lock locally but
    // reuse the factory's singleton lock, just in case a lazy dependency
    // of our advice bean happens to trigger the singleton lock implicitly...
    return ((ConfigurableBeanFactory) this.beanFactory).getSingletonMutex();
  }
  else {
    return this;
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public Advice getAdvice() {
  Advice advice = this.advice;
  if (advice != null) {
    return advice;
  }
  Assert.state(this.adviceBeanName != null, "'adviceBeanName' must be specified");
  Assert.state(this.beanFactory != null, "BeanFactory must be set to resolve 'adviceBeanName'");
  if (this.beanFactory.isSingleton(this.adviceBeanName)) {
    // Rely on singleton semantics provided by the factory.
    advice = this.beanFactory.getBean(this.adviceBeanName, Advice.class);
    this.advice = advice;
    return advice;
  }
  else {
    // No singleton guarantees from the factory -> let's lock locally but
    // reuse the factory's singleton lock, just in case a lazy dependency
    // of our advice bean happens to trigger the singleton lock implicitly...
    synchronized (this.adviceMonitor) {
      advice = this.advice;
      if (advice == null) {
        advice = this.beanFactory.getBean(this.adviceBeanName, Advice.class);
        this.advice = advice;
      }
      return advice;
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public int getOrder() {
  if (this.beanFactory != null && this.aspectBeanName != null &&
      this.beanFactory.isSingleton(this.aspectBeanName) &&
      this.beanFactory.isTypeMatch(this.aspectBeanName, Ordered.class)) {
    return ((Ordered) this.beanFactory.getBean(this.aspectBeanName)).getOrder();
  }
  return Ordered.LOWEST_PRECEDENCE;
}

代码示例来源:origin: spring-projects/spring-framework

if (this.targetBeanWrapper == null && this.beanFactory.isSingleton(this.targetBeanName)) {

代码示例来源:origin: spring-projects/spring-framework

/**
 * Determine the order for this factory's target aspect, either
 * an instance-specific order expressed through implementing the
 * {@link org.springframework.core.Ordered} interface (only
 * checked for singleton beans), or an order expressed through the
 * {@link org.springframework.core.annotation.Order} annotation
 * at the class level.
 * @see org.springframework.core.Ordered
 * @see org.springframework.core.annotation.Order
 */
@Override
public int getOrder() {
  Class<?> type = this.beanFactory.getType(this.name);
  if (type != null) {
    if (Ordered.class.isAssignableFrom(type) && this.beanFactory.isSingleton(this.name)) {
      return ((Ordered) this.beanFactory.getBean(this.name)).getOrder();
    }
    return OrderUtils.getOrder(type, Ordered.LOWEST_PRECEDENCE);
  }
  return Ordered.LOWEST_PRECEDENCE;
}

代码示例来源:origin: spring-projects/spring-framework

if (this.singleton || this.beanFactory.isSingleton(name)) {

代码示例来源:origin: spring-projects/spring-framework

if (!allListeners.contains(listener) && supportsEvent(listener, eventType, sourceType)) {
  if (retriever != null) {
    if (beanFactory.isSingleton(listenerBeanName)) {
      retriever.applicationListeners.add(listener);

代码示例来源:origin: org.springframework/spring-beans

if (this.targetBeanWrapper == null && this.beanFactory.isSingleton(this.targetBeanName)) {

代码示例来源:origin: org.springframework/spring-context

if (!allListeners.contains(listener) && supportsEvent(listener, eventType, sourceType)) {
  if (retriever != null) {
    if (beanFactory.isSingleton(listenerBeanName)) {
      retriever.applicationListeners.add(listener);

代码示例来源:origin: spring-projects/spring-framework

@Test
public void factoryPrototype() throws Exception {
  assertTrue(getBeanFactory().isSingleton("&prototypeFactory"));
  assertFalse(getBeanFactory().isSingleton("prototypeFactory"));
  TestBean tb = (TestBean) getBeanFactory().getBean("prototypeFactory");
  assertTrue(!tb.getName().equals(DummyFactory.SINGLETON_NAME));
  TestBean tb2 = (TestBean) getBeanFactory().getBean("prototypeFactory");
  assertTrue("Prototype references !=", tb != tb2);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void factorySingleton() throws Exception {
  assertTrue(getBeanFactory().isSingleton("&singletonFactory"));
  assertTrue(getBeanFactory().isSingleton("singletonFactory"));
  TestBean tb = (TestBean) getBeanFactory().getBean("singletonFactory");
  assertTrue("Singleton from factory has correct name, not " + tb.getName(), tb.getName().equals(DummyFactory.SINGLETON_NAME));
  DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory");
  TestBean tb2 = (TestBean) getBeanFactory().getBean("singletonFactory");
  assertTrue("Singleton references ==", tb == tb2);
  assertTrue("FactoryBean is BeanFactoryAware", factory.getBeanFactory() != null);
}

代码示例来源:origin: spring-projects/spring-framework

if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
  return parentBeanFactory.isSingleton(originalBeanName(name));

代码示例来源:origin: AxonFramework/AxonFramework

if (beanName != null && beanFactory.containsBean(beanName) && !beanFactory.isSingleton(beanName)) {
  return bean;

代码示例来源:origin: camunda/camunda-bpm-platform

public boolean isSingleton() {
  if (this.beanFactory == null) {
    throw new FactoryBeanNotInitializedException();
  }
  return this.beanFactory.isSingleton(this.targetBeanName);
}

代码示例来源:origin: org.springframework/spring-beans

if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
  return parentBeanFactory.isSingleton(originalBeanName(name));

代码示例来源:origin: camunda/camunda-bpm-platform

if (this.targetBeanWrapper == null && this.beanFactory.isSingleton(this.targetBeanName)) {

代码示例来源:origin: org.eclipse.gemini.blueprint/gemini-blueprint-core

private Object getBeanIfPossible() {
  if (target == null) {
    if (cacheService || beanFactory.isSingleton(beanName)) {
      getBean();
    }
  }
  return target;
}

代码示例来源:origin: org.eclipse.gemini.blueprint/gemini-blueprint-core

@Override
public boolean isSingleton() {
  if (this.beanFactory == null) {
    throw new FactoryBeanNotInitializedException();
  }
  return this.beanFactory.isSingleton(this.targetBeanName);
}

代码示例来源:origin: camunda/camunda-bpm-platform

if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
  return parentBeanFactory.isSingleton(originalBeanName(name));

代码示例来源:origin: springframework/spring-aop

public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
  super.setBeanFactory(beanFactory);
  // Check whether the target bean is defined as prototype.
  if (beanFactory.isSingleton(getTargetBeanName())) {
    throw new BeanDefinitionStoreException(
      "Cannot use PrototypeBasedTargetSource against singleton bean with name '" + getTargetBeanName() + "': " +
      "instances would not be independent");
  }
}

相关文章