This is an exotic use-case, so it requires some patience to understand and may require exotic solutions.
The Context
I'm making a library to be used with Spring that performs automatic actions upon specific bean instances present in the context, created by @Bean
methods and not by @ComponentScan
. If at all possible, the beans should be distinguishable not by type, but by other means, preferably annotations on the factory method.
Here's the ideal case. E.g. let's say there are 2 bean-producing methods:
@Bean
public SomeType makeSome() {...}
@Bean
@Special
public SomeOtherType makeOther() {...}
Here, the second bean is special because of the @Special
annotation on method that created it. But any mechanism of making it distinguishable is an option.
Then, I want to somehow get only the special beans.
The Caveat
I'm aware that if all the beans would implement the same interface, I could inject them by type. But this should work as transparently as possible, requiring as little change to an existing app as possible.
The potential approaches
Here's two broad approaches I have in mind:
1) Jack into the process of registering beans, and transparently wrap the bean instances into some sort of a container (I'm quite certain this part is doable). E.g.
public void registerBean(Object bean, ApplicationContext ctx) {
ctx.register(bean); //do the usual
ctx.register(new Wrapper(bean); //register it wrapped as well
}
Then, inject all all beans of type Wrapper
. The problem here is obviously the duplication... Alternatively I could maybe generate a proxy instance on the fly that would implement a Wrapper
interface, so it could at the same time act as the original bean and as a wrapper. I did say I'm OK with exotic solutions too, didn't I?
2) Spring already distinguishes bean candidates from actual registered beans (e.g. @ComponentScan
can filter the candidates by package, annotations etc). I'm hoping to maybe jack into this process and get a hold of candidate descriptors that still contain some useful metadata (like their factory method) that would allow me to later distinguish those bean instances.