[rules-dev] Spring support for drools-api

Mark Proctor mproctor at codehaus.org
Sun Nov 23 00:28:01 EST 2008


I've been reading about spring this weekend, in preparation of spring 
support for drools-api. My first attempt was using drools-api factories 
directly, which actually works "out of the box". Here is an example 
using the KnowledgeAgentFactory:

 --------beans.xml-------
  <bean id="agent1" 
        class="org.drools.agent.KnowledgeAgentFactory" 
        factory-method="newKnowledgeAgent">
      <constructor-arg type="java.lang.String" value="agent1"/>
 
      <constructor-arg type="java.util.Properties">
          <props>
              <prop key="file">src/main/java/org/drools/springframework/testSpring.drl</prop> 
          </props>
      </constructor-arg>
  </bean>

---------testSpring.drl---------
package  org.drools.springframework
 
rule "rule 1"
when
then
    System.out.println( "hello world" );
end
---------test--------- 
Resource resource = new FileSystemResource( "src/main/java/org/drools/springframework/beans.xml" );				 
BeanFactory factory = new XmlBeanFactory( resource );
KnowledgeAgent agent1 = ( KnowledgeAgent ) factory.getBean( "agent1" );
agent1.getKnowledgeBase().newStatelessKnowledgeSession().executeObject( "test" );


Sow I was quite impressed how easy that was to get working, but people 
like the "Spring" way. So I made a custom SpringKnowledgeAgentFactory 
that implements FactoryBean. So now the above beans.xml can look like 
the following:
  <bean id="agent1" class="org.drools.spring.SpringKnowledgeAgentFactory">
      <property name="name" value="agent1"/>
      <property name="file" 
value="src/main/java/org/drools/spring/testSpring.drl" />
  </bean>

Which is much less verbose. So I think I'll take some time tomorrow to 
do FactoryBean implementations for all main user factories from 
drools-api, shouldn't take long and then we have out of the box spring 
support. Anyone have any feedback, it seems that now that we have 
drools-api, which provides complete implementation/interface separation, 
and Spring only needs to return interfaces, this is all we need to do. 
If it's quick to do, I might have a go at doing one for Guice too.

Mark




More information about the rules-dev mailing list