Skip to content

Spring Support

Clayton Long edited this page Aug 14, 2017 · 14 revisions

The rulebook-spring Maven artifact is still maintained. However, with recent updates, it has become unnecessary. That's because RuleBook works with Spring out of the box. And since RuleBook is thread safe, Spring's default Singleton bean configuration works as one might expect with RuleBook.

Configure a RuleBook for Spring Injection

@Configuration
public class SpringAppConfig {
  @Bean
  public RuleBook ruleBook() {
    return new RuleBookRunner("com.example.rulebook.spring");
  }
}

The above example configures a RuleBookRunner to inject a RuleBook created from [POJO Rules](POJO Rules) located in the package com.example.rulebook.spring.

Spring can also inject custom RuleBooks or RuleBooks that extend the default RuleBook, CoRRuleBook.

@Configuration
public class SpringAppConfig {
  @Bean
  public RuleBook ruleBook() {
    return new MyRuleBookClass();
  }
}

Or using RuleBook's Java Domain Specific Language.

@Configuration
public class SpringAppConfig {
  @Bean
  public RuleBook ruleBook() {
    return RuleBookBuilder.create()
      .addRule(rule -> rule
        .withFactType(String.class)
        .when(facts -> true)
        .then(facts -> facts.setValue("msg", "RuleBook is awesome!"))
        .using("msg")
        .then(System.out::println));
  }
}
Clone this wiki locally