use of org.springframework.beans.factory.NoSuchBeanDefinitionException in project spring-boot by spring-projects.
the class NoUniqueBeanDefinitionFailureAnalyzer method buildMessage.
private void buildMessage(StringBuilder message, String beanName) {
try {
BeanDefinition definition = this.beanFactory.getMergedBeanDefinition(beanName);
message.append(getDefinitionDescription(beanName, definition));
} catch (NoSuchBeanDefinitionException ex) {
message.append(String.format("\t- %s: a programmatically registered singleton", beanName));
}
}
use of org.springframework.beans.factory.NoSuchBeanDefinitionException in project spring-framework by spring-projects.
the class DefaultListableBeanFactory method removeBeanDefinition.
@Override
public void removeBeanDefinition(String beanName) throws NoSuchBeanDefinitionException {
Assert.hasText(beanName, "'beanName' must not be empty");
BeanDefinition bd = this.beanDefinitionMap.remove(beanName);
if (bd == null) {
if (this.logger.isTraceEnabled()) {
this.logger.trace("No bean named '" + beanName + "' found in " + this);
}
throw new NoSuchBeanDefinitionException(beanName);
}
if (hasBeanCreationStarted()) {
// Cannot modify startup-time collection elements anymore (for stable iteration)
synchronized (this.beanDefinitionMap) {
List<String> updatedDefinitions = new ArrayList<>(this.beanDefinitionNames);
updatedDefinitions.remove(beanName);
this.beanDefinitionNames = updatedDefinitions;
}
} else {
// Still in startup registration phase
this.beanDefinitionNames.remove(beanName);
}
this.frozenBeanDefinitionNames = null;
resetBeanDefinition(beanName);
}
use of org.springframework.beans.factory.NoSuchBeanDefinitionException in project spring-framework by spring-projects.
the class DefaultListableBeanFactory method resolveNamedBean.
//---------------------------------------------------------------------
// Dependency resolution functionality
//---------------------------------------------------------------------
@Override
public <T> NamedBeanHolder<T> resolveNamedBean(Class<T> requiredType) throws BeansException {
NamedBeanHolder<T> namedBean = resolveNamedBean(requiredType, (Object[]) null);
if (namedBean != null) {
return namedBean;
}
BeanFactory parent = getParentBeanFactory();
if (parent instanceof AutowireCapableBeanFactory) {
return ((AutowireCapableBeanFactory) parent).resolveNamedBean(requiredType);
}
throw new NoSuchBeanDefinitionException(requiredType);
}
use of org.springframework.beans.factory.NoSuchBeanDefinitionException in project spring-framework by spring-projects.
the class StaticListableBeanFactory method getBean.
//---------------------------------------------------------------------
// Implementation of BeanFactory interface
//---------------------------------------------------------------------
@Override
public Object getBean(String name) throws BeansException {
String beanName = BeanFactoryUtils.transformedBeanName(name);
Object bean = this.beans.get(beanName);
if (bean == null) {
throw new NoSuchBeanDefinitionException(beanName, "Defined beans are [" + StringUtils.collectionToCommaDelimitedString(this.beans.keySet()) + "]");
}
// bean factory if the bean isn't a factory
if (BeanFactoryUtils.isFactoryDereference(name) && !(bean instanceof FactoryBean)) {
throw new BeanIsNotAFactoryException(beanName, bean.getClass());
}
if (bean instanceof FactoryBean && !BeanFactoryUtils.isFactoryDereference(name)) {
try {
return ((FactoryBean<?>) bean).getObject();
} catch (Exception ex) {
throw new BeanCreationException(beanName, "FactoryBean threw exception on object creation", ex);
}
} else {
return bean;
}
}
use of org.springframework.beans.factory.NoSuchBeanDefinitionException in project spring-framework by spring-projects.
the class AutowiredAnnotationBeanPostProcessorTests method testSmartObjectFactoryInjectionWithTargetNotAvailable.
@Test
public void testSmartObjectFactoryInjectionWithTargetNotAvailable() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SmartObjectFactoryInjectionBean.class));
SmartObjectFactoryInjectionBean bean = (SmartObjectFactoryInjectionBean) bf.getBean("annotatedBean");
try {
bean.getTestBean();
fail("Should have thrown NoSuchBeanDefinitionException");
} catch (NoSuchBeanDefinitionException ex) {
// expected
}
assertNull(bean.getOptionalTestBean());
assertEquals(new TestBean("default"), bean.getOptionalTestBeanWithDefault());
assertEquals(new TestBean("default"), bean.getUniqueTestBeanWithDefault());
assertNull(bean.getUniqueTestBean());
bf.destroySingletons();
}
Aggregations