Skip to content

BeanFactory.get*ForAnnotation should take @Bean metadata into account #22541

Closed
@mdeinum

Description

@mdeinum

I'm currently writing a small BeanFactoryPostProcessor and for that I need to process annotations on @Bean methods. Or at least detect the bean definition based on that metadata. I was expecting that getBeanNamesForAnnotation (and friends) would also take annotations on @Bean methods into consideration. However it only works for annotated types.

So when doing something like this.

@Component
@CustomAnnotation
public class MyClass { ... }

It works. But doing the same on an @Bean annotated method doesn't work.

@Bean
@CustomAnnotation
public MyClass myClass() { ... }

It would be nice if the methods would return beans (or bean definitions) annotated throuhg @Bean methods as well. Especially if we want to apply this to beans that aren't under our own control (i.e. external libraries).

For the Spring annotations like @Lazy, @Primary etc. this already works but those are handled in specialized cases when parsing the configuration files. @Lazy on a component works the same as @Lazy on a @Bean method.

In this particulair case I wanted to detect beans with a special annotation so that we could register them in JNDI. It works when annotating the component with our specialized annotation not when using the annotation on @Bean methods. This is especially annoying for classes that are outside of our control.

Currently the only way is to extend the class only for adding the annotation.

Metadata

Metadata

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions