Within the rule, two conditions are defined. The first one is to check whether the age of a contact is
larger than or equal to the provided parameter value, while the second check is for the smaller than or
equal to condition.
Then, one action is defined to assign the value provided in the parameter to the ageCategory
property of the Contact object.
The parameters define the values for both condition checking and action. For example, in the first
parameter, it means that when the age is between 0 and 10, then the value Kid will be assigned to the
ageCategory property of the Contact object, and so on. So, for each parameter, the first two values will be
used by the two conditions to check for age range, while the last value will be used for assigning the
ageCategory property.
The next step is to define the Spring ApplicationContext. Listing 22-15 shows the configuration file
(/src/main/resources/app-context.xml).
Listing 22-15. The Spring Configuration
<?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:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang-3.1.xsd">
<context:component-scan base-package="com.apress.prospring3.ch22" />
<lang:groovy id="ruleFactory" refresh-check-delay="5000"
script-source="file:resources/RuleFactoryImpl.groovy"/>
</beans>
The configuration is simple. For defining Spring beans in a scripting language, we need to use lang-
namespace. Then, the <lang:groovy> tag is used to declare a Spring bean with a Groovy script. The
script-source attribute defines the location of the Groovy script that Spring will load from. For the
refreshable bean, the attribute refresh-check-delay should be provided. In this case, we supplied the
value of 5000ms, which instructs Spring to check for file changes if the elapsed time from the last
invocation is greater than five seconds. Note that Spring will not check the file every five seconds.
Instead, it will check the file only when the corresponding bean is invoked.
Testing the Age Category Rule
Now we are ready to test the rule. The testing program is shown in Listing 22-16, which is a Java class.
Listing 22-16. Testing the Rule Engine
package com.apress.prospring3.ch22;
import org.joda.time.format.DateTimeFormat;
import org.springframework.context.support.GenericXmlApplicationContext;
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home