DEV Community

Cover image for Drools integration with Spring Boot
Raja Anbazhagan
Raja Anbazhagan

Posted on • Originally published at springhow.com

Drools integration with Spring Boot

Drools is a versatile business rule engine that can elevate your application by externalizing a lot of decision logic. As the most application servers are now using Spring boot, let's understand how to use Drools with Spring boot.

For a simple example, you can create a KieContainer as shown below.

@Bean
public KieContainer getKieContainer() {
    KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
    kieFileSystem.write( ResourceFactory.newClassPathResource("discount.drl"));
    KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);
    kb.buildAll();
    KieModule kieModule = kb.getKieModule();
    return kieServices.newKieContainer(kieModule.getReleaseId());
}
Enter fullscreen mode Exit fullscreen mode

Once you have a container, You can execute the rules within it by creating a session and firing all rules.

More on this explained at Drools integration with Spring Boot.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post →

Top comments (1)

Collapse
 
roddy profile image
Roddy

I've always just leveraged the kmodule.xml and let Drools load the various kbases from memory from files on the classpath. Simplifies things greatly, and your bean becomes just:

@Bean
public KieContainer kieContainer() {
    KieServices kieServices = KieServices.Factory.get();
    return kieServices.getKieClasspathContainer();
}
Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay