@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; } }
@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; } } }
@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; }
if (this.targetBeanWrapper == null && this.beanFactory.isSingleton(this.targetBeanName)) {
/** * 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; }
if (this.singleton || this.beanFactory.isSingleton(name)) {
if (!allListeners.contains(listener) && supportsEvent(listener, eventType, sourceType)) { if (retriever != null) { if (beanFactory.isSingleton(listenerBeanName)) { retriever.applicationListeners.add(listener);
if (this.targetBeanWrapper == null && this.beanFactory.isSingleton(this.targetBeanName)) {
if (!allListeners.contains(listener) && supportsEvent(listener, eventType, sourceType)) { if (retriever != null) { if (beanFactory.isSingleton(listenerBeanName)) { retriever.applicationListeners.add(listener);
@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); }
@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); }
if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { return parentBeanFactory.isSingleton(originalBeanName(name));
if (beanName != null && beanFactory.containsBean(beanName) && !beanFactory.isSingleton(beanName)) { return bean;
public boolean isSingleton() { if (this.beanFactory == null) { throw new FactoryBeanNotInitializedException(); } return this.beanFactory.isSingleton(this.targetBeanName); }
if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { return parentBeanFactory.isSingleton(originalBeanName(name));
if (this.targetBeanWrapper == null && this.beanFactory.isSingleton(this.targetBeanName)) {
private Object getBeanIfPossible() { if (target == null) { if (cacheService || beanFactory.isSingleton(beanName)) { getBean(); } } return target; }
@Override public boolean isSingleton() { if (this.beanFactory == null) { throw new FactoryBeanNotInitializedException(); } return this.beanFactory.isSingleton(this.targetBeanName); }
if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { return parentBeanFactory.isSingleton(originalBeanName(name));
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"); } }