Skip to content

BeanFactory.getBeansWithAnnotation() should not consider abstract beans [SPR-10663] #15291

@spring-projects-issues

Description

@spring-projects-issues

Andreas Thaler opened SPR-10663 and commented

Assumed you have a BeanFactory designed for extension (for example using lookup-method)

public abstract class MyFactory implements FactoryBean{ ... }

configured as abstract bean

<bean id="myFactory" abstract="true" class="MyFactory">

then a call to ```
ApplicationContext.getBeansWithAnnotation(..)


As the class is an abstract class the call will fail with:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myFactory' defined in class path resource [myConfig.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [MyFactory]: Is it an abstract class?; nested exception is java.lang.InstantiationException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getSingletonFactoryBeanForTypeCheck(AbstractAutowireCapableBeanFactory.java:767)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:698)
at org.springframework.beans.factory.support.AbstractBeanFactory.getType(AbstractBeanFactory.java:578)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAnnotationOnBean(DefaultListableBeanFactory.java:464)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansWithAnnotation(DefaultListableBeanFactory.java:449)
at org.springframework.context.support.AbstractApplicationContext.getBeansWithAnnotation(AbstractApplicationContext.java:1182)
at
...


Same scenario will work fine if class is not implementing FactoryBean, so it seems that for the special case of a FactoryBean the abstract flag is not repected.

Affects: 3.1.3

Sub-tasks:

Referenced from: commits ed996ab, 8ed8ee2, d4b54d8

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions