Spring integration

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.

Introducing Spring Framework

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.

Kie Spring Config example

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:

  • kie:import and kie:releaseId: With these two tags, we announce the context we should dynamically load in a specific release of a Kie JAR, and load it into the classpath.
  • kie:module, kie:base, and kie:session: These components are used in a very similar fashion to what we would define inside kmodule.xml. It will let us define Kie Bases and Sessions we can later on reference from other Spring managed components.
  • The kie:batch tag: It lets us define a specific set of commands that needs to be executed to initialize our Kie Sessions; using kie:batch, we can set globals, insert initial facts, or anything specific we need to do to initialize our Kie Session, without having to write any code for it.
  • We can run this example by running the 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.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.182.66