Search in sources :

Example 1 with BeanIsNotAFactoryException

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;
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) FactoryBean(org.springframework.beans.factory.FactoryBean) SmartFactoryBean(org.springframework.beans.factory.SmartFactoryBean) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) BeanIsNotAFactoryException(org.springframework.beans.factory.BeanIsNotAFactoryException) BeansException(org.springframework.beans.BeansException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeanNotOfRequiredTypeException(org.springframework.beans.factory.BeanNotOfRequiredTypeException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) NoUniqueBeanDefinitionException(org.springframework.beans.factory.NoUniqueBeanDefinitionException) BeanIsNotAFactoryException(org.springframework.beans.factory.BeanIsNotAFactoryException)

Example 2 with BeanIsNotAFactoryException

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
    }
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) CarFactoryBean(org.springframework.context.expression.FactoryBeanAccessTests.SimpleBeanResolver.CarFactoryBean) BeanIsNotAFactoryException(org.springframework.beans.factory.BeanIsNotAFactoryException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Boat(org.springframework.context.expression.FactoryBeanAccessTests.SimpleBeanResolver.Boat) Test(org.junit.Test)

Aggregations

BeanIsNotAFactoryException (org.springframework.beans.factory.BeanIsNotAFactoryException)2 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)2 Test (org.junit.Test)1 BeansException (org.springframework.beans.BeansException)1 BeanCreationException (org.springframework.beans.factory.BeanCreationException)1 BeanNotOfRequiredTypeException (org.springframework.beans.factory.BeanNotOfRequiredTypeException)1 FactoryBean (org.springframework.beans.factory.FactoryBean)1 NoUniqueBeanDefinitionException (org.springframework.beans.factory.NoUniqueBeanDefinitionException)1 SmartFactoryBean (org.springframework.beans.factory.SmartFactoryBean)1 Boat (org.springframework.context.expression.FactoryBeanAccessTests.SimpleBeanResolver.Boat)1 CarFactoryBean (org.springframework.context.expression.FactoryBeanAccessTests.SimpleBeanResolver.CarFactoryBean)1 Expression (org.springframework.expression.Expression)1 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)1 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)1