OK, using a BeanPostProcessor, register every bean of your interface:
public class EventBusRegisterBeanPostProcessor implements BeanPostProcessor,
ApplicationContextAware {
private ApplicationContext context;
@Autowired
private EventBus eventBus; // The only event bus i assume...
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
if (bean instanceof ILanguageChangeListener) {
registerToEventBus(bean);
}
return bean;
}
private void registerToEventBus(Object bean) {
this.eventBus.register(bean);
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.context = applicationContext;
}
}
Note that if you have many EventBus beans, you should use the ApplicationContext.getBean(String)
to get the EventBus you need.
I quote from the javadoc:
In case of a FactoryBean, this callback will be invoked for both the FactoryBean instance and the objects created by the FactoryBean (as of Spring 2.0). The post-processor can decide whether to apply to either the FactoryBean or created objects or both through corresponding bean instanceof FactoryBean checks.