Skip to content

Spring Support

Clayton Long edited this page May 7, 2017 · 14 revisions

RuleBook provides Spring support through the rulebook-spring Maven artifact. By adding rulebook-spring to your project dependencies, RuleBook gains Spring support through RuleBookFactoryBean and SpringRuleBook. RuleBookFactoryBean allows developers to quickly configure RuleBooks for Spring injection, including RuleBooks that scan for POJO Rules and predefined RuleBooks. SpringRuleBook is a RuleBook decorator that allows POJO Rules to be injected by Spring and then added to RuleBook simply by using the addRule() method. A @RuleBean annotation is also available as a convenience for the injection of POJO Rules.

Add the RuleBook Spring dependency

For Maven

Add the dependency

<dependency>
  <groupId>com.deliveredtechnologies</groupId>
  <artifactId>rulebook-spring</artifactId>
  <version>0.6</version>
</dependency>

For Gradle

Add the dependency

compile 'com.deliveredtechnologies:rulebook-spring:0.6'

RuleBookFactoryBean

RuleBookFactoryBean is the easiest way to use RuleBook with Spring. Simply add it to the Spring configuration.

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

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

Aside from building a RuleBook from POJO Rules created from scanning a package, RuleBookFactoryBean can also create RuleBooks from existing RuleBook classes as shown in the following example.

@Configuration
public class SpringAppConfig {
  @Bean
  public RuleBookFactoryBean ruleBook() {
    return new RuleBookFactoryBean(MyRuleBook.class);
  }
}

RuleBookFactoryBean can also create a RuleBook from a package using a specific RuleBook class.

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

SpringRuleBook

SpringRuleBook allows you to build a RuleBook by adding either Rules or POJO Rules. With SpringRuleBook, you can use Spring to inject POJO Rules and then add those POJO Rules to a RuleBook... a SpringRuleBook. Most of the time, RuleBookFactoryBean or building a RuleBook using RuleBookBuilder in the Spring configuration will be the preferred way to use Spring with RuleBook. But in some cases, the option to have Spring inject Rules that can then be easily added to a RuleBook might be wanted. For those cases, there is SpringRuleBook.

Clone this wiki locally