Let’s say I have an interface for language change event in my application (it’s based on Vaadin):
public interface ILanguageChangeListener{
@Subscribe onLanguageChange(LanguageChangeEvent event);
}
And I have many beans that implements this interface annotated with @Component, thus they are available in Spring IoC. I have also an EventBus bean:
<bean id="languageSwitcher" class="com.google.common.eventbus" scope="session" />
Now, after getting an instance of any bean from IoC I have to get also an instance of the languageSwitcher and register the newely created bean in it with:
languageSwitcher.register(myNewBean);
in order to receive this events. Is it possible to somehow tell the IoC that I want to call the register method of the languageSwitcher bean on every new bean that implements the ILanguageChangeListener?
OK, using a BeanPostProcessor, register every bean of your interface:
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: