This post will walk you through integrating the lightweight and straightforward Easy Rules engine with your Spring Boot microservice. We’ll cover the necessary dependencies, basic setup, and an example service to demonstrate its usage.
1. Project Setup and Dependencies
Start by creating a Spring Boot project. Next, add the following Easy Rules dependencies to your pom.xml
(Maven) or build.gradle
(Gradle) file:
Maven:
XML
<dependency>
<groupId>org.jeasy</groupId>
<artifactId>easy-rules-core</artifactId>
<version>5.3.0</version>
</dependency>
Gradle:
Gradle
implementation 'org.jeasy:easy-rules-core:5.3.0'
2. Defining Rules with Easy Rules
Easy Rules promotes a POJO-based approach for defining rules. Create a class that implements the org.jeasy.rules.api.Rule
interface:
Java
import org.jeasy.rules.annotation.Action;
import org.jeasy.rules.annotation.Condition;
import org.jeasy.rules.annotation.Rule;
@Rule(name = "Discount Rule", description = "Apply 10% discount for orders over $100")
public class DiscountRule {
@Condition
public boolean isApplicable(Order order) {
return order.getTotalPrice() > 100;
}
@Action
public void applyDiscount(Order order) {
order.applyDiscount(0.1);
}
}
This code defines a rule named “Discount Rule”. The isApplicable
method defines the condition (order total price greater than 100), and the applyDiscount
method defines the action (apply a 10% discount).
3. Using Easy Rules in a Service
Inject the RulesEngine
bean into your service class and use it to fire rules against your domain objects.
Java
import org.jeasy.rules.api.RulesEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
@Service
public class OrderService {
@Autowired
private RulesEngine rulesEngine;
public Order applyRules(Order order) {
rulesEngine.fire(Arrays.asList(new DiscountRule()), Arrays.asList(order));
return order;
}
}
This service:
- Injects the
RulesEngine
bean. - Creates an instance of the
DiscountRule
. - Fires the rule against the provided
order
object. - Returns the modified
order
object.
4. Configuration Setup
Easy Rules has sensible defaults and often requires minimal configuration. You can customize the RulesEngine
by defining your own bean:
Java
import org.jeasy.rules.api.RulesEngineParameters;
import org.jeasy.rules.core.DefaultRulesEngine;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class EasyRulesConfig {
@Bean
public RulesEngine rulesEngine() {
RulesEngineParameters parameters = new RulesEngineParameters()
.skipOnFirstAppliedRule(true); // Example parameter
return new DefaultRulesEngine(parameters);
}
}
5. YML Changes (Not Required)
Easy Rules doesn’t typically require specific configuration in your application.yml
or application.properties
file.
This guide provides a basic introduction to integrating Easy Rules with your Spring Boot microservice. Its simplicity and annotation-based approach make it easy to define and manage business rules. You can expand on this by exploring more advanced features like rule composition, priority, and custom rule listeners. Remember to tailor the rules and domain models to your specific business needs.
Discover more from GhostProgrammer - Jeff Miller
Subscribe to get the latest posts sent to your email.