I have code with lazy initialized beans:
@Component @Lazy class Resource {...} @Component @Lazy @CustomProcessor class ResourceProcessorFoo{ @Autowired public ResourceProcessor(Resource resource) {...} } @Component @Lazy @CustomProcessor class ResourceProcessorBar{ @Autowired public ResourceProcessor(Resource resource) {...} }
After initializing the application context, there are no instances of this beans. When a resource bean is created by the application context (for example, applicationContext.getBean (Resource.class)), instances of @CustomProcessor are not marked with beans.
When creating a bean resource, you must create beans with @CustomProcessor. How to do it?
Updated: One of the ugly solutions found - use an empty autoinstaller:
@Autowired public void setProcessors(List<ResourceProcessor> processor){}
Another ugly solution with bean BeanPostProcessor (so magic!)
@Component class CustomProcessor implements BeanPostProcessor{ public postProcessBeforeInitialization(Object bean, String beanName) { if(bean instanceof Resource){ applicationContext.getBeansWithAnnotation(CustomProcessor.class); } } }
Maybe there is a more elegant way?
source share