Spring Support

It is possible integrate brainslug with the Spring application context, so that the Spring Beans are available in the execution context. To use it, you need to get the brainslug-spring module.

Setup

Just import the spring configuration class into you application context.

Calls to the ExecutionContext or the Registry will then return the beans from the Spring application context.

All beans of type FlowBuilder will be made available in the BrainslugContext as well:

@Configuration
@Import(brainslug.spring.SpringBrainslugConfiguration.class)
public class ConfigurationExample {

  @Component
  public static class SpringExampleTask implements Task {
    Environment environment;

    @Autowired
    public SpringExampleTask(Environment environment) {
      this.environment = environment;
    }

    @Override
    public void execute(ExecutionContext context) {
      printHello(context.property("name", String.class));

      context.service(SpringExampleTask.class).printHello("again");
    }

    public void printHello(String name) {
      System.out.println(
        format("Hello %s!", name)
      );
    }
  }

  @Bean
  FlowBuilder flowBuilder() {
    return new FlowBuilder() {
      @Override
      public void define() {
        flowId(id("spring-flow"));

        start(task(id("spring-task")).delegate(SpringExampleTask.class));
      }
    };
  }

  public static void main(String[] args) {
    AnnotationConfigApplicationContext applicationContext =
      new AnnotationConfigApplicationContext(ConfigurationExample.class);

    BrainslugContext brainslugContext = applicationContext.getBean(BrainslugContext.class);

    brainslugContext.startFlow(FlowBuilder.id("spring-flow"),
      newProperties().with("name", "World"));
  }

}

Will output the two lines:

Hello World!
Hello again!

Custom BrainslugContext

To configure the BrainslugContext in your ApplicationContext you can use the SpringBrainslugContextBuilder:

@Configuration
@Import(brainslug.spring.SpringBrainslugConfiguration.class)
public class ContextBuilderExample {
  @Bean
  SpringBrainslugContextBuilder contextBuilder() {
    return new SpringBrainslugContextBuilder()
      .withPropertyStore(new HashMapPropertyStore());
      // ...
  }
}