Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register beans automatically in Guava EventBus with Spring IoC

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?

like image 751
fracz Avatar asked Dec 10 '12 15:12

fracz


People also ask

What is guava EventBus?

The Google Guava EventBus allows publish-subscribe-style communication between components without requiring the components to explicitly register with one another (and thus be aware of each other). The guava-eventbus: component provides integration bridge between Camel and Google Guava EventBus infrastructure.

What is EventBus in Java?

EventBus is a publish/subscribe event bus for Android and Java. EventBus... simplifies the communication between components. decouples event senders and receivers. performs well with Activities, Fragments, and background threads.

How many ways we can create bean in spring?

Different Methods to Create a Spring Bean Creating Bean Inside an XML Configuration File (beans. xml) Using @Component Annotation. Using @Bean Annotation.

What is bean in spring with example?

Spring Bean is nothing special, any object in the Spring framework that we initialize through Spring container is called Spring Bean. Any normal Java POJO class can be a Spring Bean if it's configured to be initialized via container by providing configuration metadata information.


1 Answers

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.

like image 192
ElderMael Avatar answered Nov 04 '22 13:11

ElderMael