Skip to content
Jon Brisbin edited this page Aug 26, 2013 · 6 revisions

Spring Support

Reactor provides out-of-the-box support for Spring ApplicationContexts by providing a BeanPostProcessor implementation that finds annotated beans and wires them into Reactors using SpEL and also provides some helper FactoryBean implementations for creating Reactor Environment and Reactor instances.

@EnableReactor

Since creating the initial Environment is a standard part of using Reactor in any application, reactor-spring provides a JavaConfig annotation that you put on your @Configuration bean to implicitly create an Environment based on the default properties file bootstrapping mechanism. You don’t have to create an Environment bean explicitly if you use @EnableReactor.

Using the @EnableReactor annotation also configures the BeanPostProcessor to post-process your beans that have methods annotated with the @Selector annotation. Here’s an example of a POJO bean definition that uses annotations to consume events published to a Reactor bean defined in the same ApplicationContext:

/**
 * HandlerBean.java
 */
@Component
public class HandlerBean {

  @Selector(value="test.topic", reactor="@rootReactor")
  public void handleTestTopic(Event<String> evt) {
    // handle the event
  }

}

/**
 * ReactorConfig.java
 */
@Configuration
@EnableReactor
@ComponentScan
public class ReactorConfig {

  @Bean
  public Reactor rootReactor(Environment env) {
    // implicit Environment is injected into bean def method
    return Reactors.reactor().env(env).get();
  }

}

Any other components who also have the same Reactor injected into them can publish events to it, while the POJO handler beans can handle the events.

@Service
public class TestService {

  @Autowired
  private Reactor rootReactor;

  public void fireEvent(String s) {
    rootReactor.notify("test.topic", Event.wrap(s));
  }

}

Request/Reply with Annotated Handlers

If you’re using annotated handler beans as Consumers using the @Selector annotation, your method can also serve as a request/reply handler by returning a value. To tell the BeanPostProcessor where to send the return value, use the @ReplyTo("topic") annotation on your handler method.

Clone this wiki locally