Drools7: How to assign drl files to the KieBase in kmodule.xml? - spring

I'm trying to migrate from drools 5 to drools 7. In version 6 there were changes in the spring integration. Based on the documentation drools:resources
drools:resource was removed, however I couldn't find out how to achieve the same behavior with the new toolset. What I want is to have different kiebases with different rules, that are defined in drl files.
The documentation says that the resources can be defined using packages. Unfortunately in my case a package may contain several drl files and I want to filter some of them.
What I had in drools 5.x:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:drools-spring="http://drools.org/schema/drools-spring"
xsi:schemaLocation="http://drools.org/schema/drools-spring http://drools.org/schema/drools-spring.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<drools-spring:kbase id="rules1And3">
<drools-spring:resources>
<drools-spring:resource source="classpath:rules/Rules1.drl"/>
<drools-spring:resource source="classpath:rules/Rules3.drl"/>
</drools-spring:resources>
</drools-spring:kbase>
<drools-spring:kbase id="rules2And3">
<drools-spring:resources>
<drools-spring:resource source="classpath:rules/Rules2.drl"/>
<drools-spring:resource source="classpath:rules/Rules3.drl"/>
</drools-spring:resources>
</drools-spring:kbase>
<bean id="ruleSessionAutoRefundAndPox" factory-bean="rules1And3"
factory-method="newStatelessKnowledgeSession"/>
<bean id="ruleSessionNonCashRefund" factory-bean="rules2And3"
factory-method="newStatelessKnowledgeSession"/>
</beans>
So here there were 3 files under the Rules. The first kbase only had rule 1 and rule 2 and the second had only rule 2 and rule3.
How it "should" look like in 7.x:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:kie="http://drools.org/schema/kie-spring"
xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://drools.org/schema/kie-spring http://drools.org/schema/kie-spring.xsd">
<kie:kmodule id="rules">
<kie:kbase name="rules1And3">
<!--loaded drl files-->
</kie:kbase>
<kie:kbase name="rules2And3">
<!--loaded drl files-->
</kie:kbase>
</kie:kmodule>
<!-- maybe these are unnecessary and instead ksessions should been defined within kbase elements-->
<bean id="sessionRules1And3" factory-bean="rules1And3"
factory-method="newStatelessKnowledgeSession"/>
<bean id="sessionRules2And3" factory-bean="rules2And3"
factory-method="newStatelessKnowledgeSession"/>
</beans>
Based on what I saw I'm not even sure if the very same behavior is achievable in the new version or maybe the whole approach is wrong, but what I want is to be able to define which drl files are loaded for a kiebase.
Thanks for any help!

I could not find anything that would make it possible to refer to a file explicitly.
What I did is that I placed the under a folder in the main/resources
i.e:
src/main/resources/rules1/Rules1.drl
src/main/resources/rules3/Rules3.drl
and then the kbase definition looks like this:
<kie:kmodule id="rules">
<kie:kbase name="rules1And3" packages="rules1,rules3"/>
...
</kie:kmodule>
NOTE1: the package in the drl file should be correct. i.e. if it is under com/some/package then in the drl file you should have package com.some.package
This should how it is normally, but this was not validated in version 5
NOTE2: if you have a separate kmodule for tests then you should have the drl files also in test/resouces in order to be able to load them.
So this is how I manged to resolve it, if there will be any better answer I will accept it, but this works.

Related

The prefix "context" for element "context:annotation-config" is not bound

I am trying to use to make my Spring application work with annotations. But I am getting an error: that the prefix "context" for element "context:annotation-config" is not found.
Can anyone tell what the root cause for this is.
You are missing the required xml name space entries
Spring reference documentation
make sure you context.xml is similar to the following
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>

schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/beans/spring- beans-4.1.5.xsd

I get an error in spring-dispatcher.xml in eclipse as given below.
schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/beans/spring-
beans-4.1.5.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root
element of the document is not <xsd:schema>.
I have latest spring libraries...
spring-beans-4.1.5.RELEASE.jar
spring-beans-4.1.5.RELEASE-javadoc.jar
spring-beans-4.1.5.RELEASE-sources.jar
spring-context-4.1.5.RELEASE.jar
spring-context-4.1.5.RELEASE-javadoc.jar
spring-context-4.1.5.RELEASE-sources.jar
spring-context-support-4.1.5.RELEASE.jar
spring-context-support-4.1.5.RELEASE-javadoc.jar
spring-context-support-4.1.5.RELEASE-sources.jar
spring-webmvc-4.1.5.RELEASE.jar
spring-webmvc-4.1.5.RELEASE-javadoc.jar
spring-webmvc-4.1.5.RELEASE-sources.jar
spring-webmvc-portlet-4.1.5.RELEASE.jar
spring-webmvc-portlet-4.1.5.RELEASE-javadoc.jar
spring-webmvc-portlet-4.1.5.RELEASE-sources.jar
spring-dispatcher.xml as given below...
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
It would be great if I get some help... The posts with same subject did not help me to resolve this.Thanks in advance...
The error is because it could not find the xsd. Try doing the below which is using a specific version 4.1.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
Or if you don't mention a version, it will try to use the latest.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
From Cosmina I. - Pivotal Certified Professional Spring Developer Exam A Study Guide - 2017
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
A recommended (best) practice is to use this, since the version will
be correctly identified from the Spring dependency version in the
project. Also, the other advantage is that you can upgrade the Spring
version you are using, and the new definition specifications will be
automatically supported in your configuration files without your
having to modify them
There is no such xsd in any Spring jar :
http://www.springframework.org/schema/beans/spring-beans-4.1.5.xsd
Spring xsd for Spring 4.1.x can be referenced by :
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
or better :
http://www.springframework.org/schema/beans/spring-beans.xsd
The correspondance between the URL and the real location inside each spring jar can be found in META-INF/spring.schemas, so the "version less" URL will still work when u upgrade Spring.
In my case it went away by adding
<?xml version="1.0" encoding="utf-8"?>
to spring-dispatcher.xml and then running update maven project.
Belive me or not, this may be a maven problem if you're using it and things ended up like this from nothing. I know this may not be your case, but if so, please update your maven project. If you're using Eclipse IDE:
Right click on the project name.
Maven.
Update Project...
Again, this may be your case. It worked for me doing this. This can work also to anybody else who is on the same situation.
Try this, it worked for me
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
I have enroled for spring course on udemy. I followed every step that my instructor show me to do.
So if you are using spring mvc and hibernate you may encounter this error
Failed to read schema document 'http://www.springframework.org/schema/tx/spring-tx.xsd' etc for:
<mvc:annotation-driven/> and <tx:annotation-driven transaction-manager="myTransactionManager" /> elements
in my spring configuration file i had these two urls
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
in xsi:schemaLocation, which i replaced with
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
Actually visit these two sites
http://www.springframework.org/schema/mvc/ and http://www.springframework.org/schema/tx/
and just added the latest version of spring-mvc and spring-tx i.e, spring-mvc-4.2.xsd and spring-tx-4.2.xsd as shown above.
In my opinion specifying version no explicitly is a good practice.
It worked for me, hope this works for you too.
Thank you.
I tried modifying the xml as mentioned in the other solutions but none of them worked for me. In the end I tried opening the schema location file on a web browser but it wouldn't connect either (even though internet was working fine). Was the spring framework server down?
Turns out the Internet Security software that we use had to be turned on to access this site. So try opening the schema file in a web browser to verify that the URL is correct and that you can connect to it especially if the configuration was working fine previously.
I had a similar problem, it was giving the similar error for http://mybatis.org/schema/mybatis-spring.xsd file. Finally it was a missing jar in the application. I added the below dependency in my gradle file and that solved the problem
compile group: 'org.mybatis', name: 'mybatis-spring', version: '2.0.0'
So it could be a similar dependency or a spring jar missing in your application
Using Version-less dependencies is appreciated. Find my code below.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean "class name goes here"> </bean>
</beans>

spring data - jpa application-context schema error

I am having an issue with the xml schema for spring jpa repository.
I have tried all the suggestions available online but do not seem to resolve it.
please help.
getting this error in the application-context.xml for the repository.
Multiple annotations found at this line:
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'repository:repositories'.
- cvc-complex-type.2.4.a: Invalid content was found starting with element 'repositories'. One of '{"http://www.springframework.org/schema/
beans":import, "http://www.springframework.org/schema/beans":alias, "http://www.springframework.org/schema/beans":bean, WC[##other:"http://
www.springframework.org/schema/beans"]}' is expected.
my application context looks like this
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<jdbc:embedded-database id="dataSource" type="H2" />
<tx:annotation-driven transaction-manager="transactionManager" />
<repositories base-package="com.company.repositories" />
<context:component-scan base-package="com.company.*" />
</beans>
We generally recommend to refer to the schema files that don't have version numbers in them as this will make sure you pick up the version form the dependency in your classpath.
The reason for this is that XSD doesn't know any kind of version concept. This means that spring-beans.xsd and spring-beans-3.0.xsd are completely unrelated to XSD which in turn means that if you happen to import both of them (I'll get to that in a second), this will create errors as XSD considers the types declared in them to be declared twice.
The reason you might accidentally import a non-versioned XSD is that the Spring Data XSDs actually refer to them to make sure we pick up the latest version (to be forward compatible).
Long story short: remove the explicit versions from the names and you should be good.
after a lot of trial and error the following application context doesn't seem to have any errors.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<jdbc:embedded-database id="dataSource" type="H2" />
<tx:annotation-driven transaction-manager="transactionManager" />
<jpa:repositories base-package="com.company.repositories" />
<context:component-scan base-package="com.company.*" />
</beans>
It beyond me why it didn't resolve the errors when I tried it earlier.
I was getting the following error.
Referenced file contains errors (http://www.springframework.org/schema/beans/spring-beans-3.0.xsd).
I did the following, preferences > general > network connection > cache --> deleted the caches xsd.
Then I clicked on the problems view and deleted the problem and cleaned the project. Don't have the error now. Anybody who understands this better is welcome to answer.

No setter found for property 'kBaseName' in class 'org.kie.spring.factorybeans.KBaseFactoryBean'

This is an integration issue of Drools KIE and Spring MVC Web V 3.2.3 where google just isn't finding any references to so I'll try my luck here ...
I'm integrating Drools KIE and Spring 3.2.3.RELEASE (MVC Web)- and I'm getting the following error:
No setter found for property 'kBaseName' in class 'org.kie.spring.factorybeans.KBaseFactoryBean'
[config set: maven-spring-drools/web-context application-config.xml
/maven-spring-drools/src/main/resources/spring
The Project is a pure Maven project w/out any outside natures imposed upon it (aka. Drools/Spring).
It's complaining that it can't find the setters for the kBaseName', which is set here:
I'm using a kmodule.xml found in the META-INF dir under the src/main/resources dir.
Can anyone help me discover the disconnect?
Moreover - do I have to do it this way? The project executes the SPring MVC Web App just fine and the Drools KIE test case runs perfectly in the same Maven project. Can't I just integrate them programmatically instead?
Many thanks in advance ... :-)
The offending file: application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:kie="http://drools.org/schema/kie-spring"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://drools.org/schema/kie-spring http://drools.org/schema/kie-spring.xsd">
<context:component-scan base-package="com.versaggi.springweb"/>
<kie:kmodule id="ksession-rules">
<kie:kbase name="rules" packages="rules">
</kie:kbase>
</kie:kmodule>
<bean id="kiePostProcessor" class="org.kie.spring.KModuleBeanFactoryPostProcessor" />
</beans>

Spring config not found

I have the following in my web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
I have 2 files:
applicationContext-web.xml in the WEB-INF next to the web.xml
applicationContext-service.xml in the myapp-service.jar
When deploying the app, I get a
No matching bean of type [AServiceBean] found for dependency: expected
at least 1 bean which qualifies as autowire candidate for this
dependency.
Seems like the applicationContext-service.xml is not found. If I copy it next to the web.xml, it works fine. I can't figure out why this happens.
The server is a Tomcat 6.
Any help is appreciated.
Thanks.
EDIT
For clarification: if I use
<param-value>
classpath:applicationContext-web.xml,
classpath:applicationContext-service.xml
</param-value>
the app deploys without any issue, so it's just a matter of finding (or not finding) the applicationContext-service.xml
Try to use classpath*:applicationContext-*.xml (there is asterisk before the colon).
However it may not work, e.g. JBoss has problems, to make it work you need use some special class loader from jboss.
Also, there are some problems using patterns in root.
Anyway I would recommend avoid patterns, better to make an applicationContext.xml with two explicit import statements.
You need to place your config files into the classpath.
WEB-INF/classess is the directory you need to place your configuration files
classpath:applicationContext-*.xml will then work
or sth similar to this to keep them in one place
WEB-INF/classes/spring
classpath:spring/applicationContext-*.xml
applicationContext-service.xml : You dont need to copy this one if it is already in the jar file
Sample main-config.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<import resource="classpath:spring/config1.xml" />
<import resource="classpath:spring/config2.xml" />
.
.
<import resource="classpath:spring/configN.xml" />
</beans>

Resources