use of org.springframework.beans.factory.BeanIsNotAFactoryException 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.BeanIsNotAFactoryException in project spring-framework by spring-projects.
the class FactoryBeanAccessTests method factoryBeanAccess.
@Test
public void factoryBeanAccess() {
// SPR9511
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new SimpleBeanResolver());
Expression expr = new SpelExpressionParser().parseRaw("@car.colour");
assertEquals("red", expr.getValue(context));
expr = new SpelExpressionParser().parseRaw("&car.class.name");
assertEquals(CarFactoryBean.class.getName(), expr.getValue(context));
expr = new SpelExpressionParser().parseRaw("@boat.colour");
assertEquals("blue", expr.getValue(context));
expr = new SpelExpressionParser().parseRaw("&boat.class.name");
try {
assertEquals(Boat.class.getName(), expr.getValue(context));
fail("Expected BeanIsNotAFactoryException");
} catch (BeanIsNotAFactoryException binafe) {
// success
}
// No such bean
try {
expr = new SpelExpressionParser().parseRaw("@truck");
assertEquals("red", expr.getValue(context));
fail("Expected NoSuchBeanDefinitionException");
} catch (NoSuchBeanDefinitionException nsbde) {
// success
}
// No such factory bean
try {
expr = new SpelExpressionParser().parseRaw("&truck");
assertEquals(CarFactoryBean.class.getName(), expr.getValue(context));
fail("Expected NoSuchBeanDefinitionException");
} catch (NoSuchBeanDefinitionException nsbde) {
// success
}
}
Aggregations