Another well known framework for dependency injection is the Spring Framework (http://projects.spring.io/spring-framework/). Drools can be easily integrated to other components in an application using Spring, and we have an example of this integration in the chapter-11/chapter-11-spring
project of the code bundle.
The Spring Framework is, in its core, an integration framework that allows binding of different Java components together using Inversion of Control. Through XML configuration files or injection annotations, it lets us tie together bean constructors and setters to initialize the runtime of our applications. Besides that base functionality, Spring has been in the market for several years now, and has a lot of adoption and pluggable libraries that allow us to bind all sorts of functionalities to our apps, including Data Access libraries, Aspect-oriented programming features, URL binding, and Transaction management.
Drools provides a module to integrate with the Spring framework called Kie Spring. It lets us define Kie Modules, Bases, Sessions, and set components on them and bind them together with the rest of our Spring configurations. To start using it, we first need to define this dependency inside our POM file:
<dependency> <groupId>org.kie</groupId> <artifactId>kie-spring</artifactId> <version>6.3.0.Final</version> </dependency>
And after that, we define our Kie components inside our Spring context file. Here's a section of the chapter-11-spring/src/main/resources/spring-context.xml
file that shows the configuration we need to use to define a Kie Session:
<kie:import releaseId-ref="kjarToUse" /> <kie:releaseId id="kjarToUse" groupId="org.drools.devguide" artifactId="chapter-11-kjar" version="0.1-SNAPSHOT" /> <kie:kmodule id="kie-spring-sample"> <kie:kbase name="kbase1"> <kie:ksession name="ksession1"/> </kie:kbase> </kie:kmodule>
In the previous example, we defined a set of important components:
kmodule.xml
. It will let us define Kie Bases and Sessions we can later on reference from other Spring managed components.KieSpringTest
JUnit test in the chapter-11-spring
project of the code bundle. If you want to see a full explanation of the types of tags available for configuring Kie Spring contexts, you can find it at: http://docs.jboss.org/drools/release/latest/drools-docs/html/ch13.html.18.222.182.66