import com.apress.prospring3.ch22.domain.Contact;
import com.apress.prospring3.ch22.service.ContactService;
public class RuleEngineTest {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:app-context.xml");
ctx.refresh();
ContactService contactService = ctx.getBean("contactService", ContactService.class);
// Construct Contact object
Contact contact = new Contact();
contact.setId(1l);
contact.setFirstName("Clarence");
contact.setLastName("Ho");
contact.setBirthDate(DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime("1980-08-09"));
// Apply rule to contact object
contactService.applyRule(contact);
System.out.println("Contact: " + contact);
// Wait for rule to be updated
try {
System.in.read();
} catch (Exception ex) {
ex.printStackTrace();
}
// Apply the rule again
contactService.applyRule(contact);
System.out.println("Contact: " + contact);
}
}
In Listing 22-16, upon initialization of Spring's GenericXmlApplicationContext, an instance of
Contact object is constructed. Then, the instance of ContactService interface is obtained to apply the
rule onto the Contact object and then output the result to the console. The program will be paused for
user input, before the second application of the rule. During the pause, we can then modify the
RuleFactoryImpl.groovy class so that Spring will refresh the bean and we can see the changed rule
in action.
Running the testing program will produce the following output:
Executing
rule
Condition
Param index: 0
Condition
success: true
Condition
Param index: 1
Condition
success: false
Condition
Param index: 0
Condition
success: true
Condition
Param index: 1
Condition
success: false
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home