Java Reference
In-Depth Information
create even more robust validation. To read more about JSR 303 and validation using it within a Spring
application, visit the Spring documentation on validation at
reference/ html/validation.html .
Before you move on, however, the previous example only applied the validation itself to the input.
It did not apply any processing to the item once it did pass validation. This next section will look at how
to apply business logic once an item has passed validation.
Subclassing the ValidatingItemProcessor
Although in the previous section you were able to perform item validation, you didn't actually process
the item once it did pass validation. In this section, you will look at how to subclass the
ValidatingItemProcessor to apply logic to each item as it passes validation.
By subclassing the ValidatingItemProcessor class, you can override the process method to apply
your logic to each item. If you use the same example as you did for the validation, you can add the
ability to output the customer's name and the number record he was in your implementation. Listing 8-
10 shows the code for the CustomerValidatingItemProcessor .
Listing 8-10. CustomerValidatingItemProcessor
package com.apress.springbatch.chapter8;
import org.springframework.batch.item.validator.ValidatingItemProcessor;
public class CustomerValidatingItemProcessor extends ValidatingItemProcessor<Customer> {
private int recordCount = 0;
public Customer process(Customer customer) {
System.out.println(customer.getFirstName() + " " +
customer.getLastName() + " was record number " +
recordCount + " in your file.");
return customer;
With the validation logic already addressed with your BeanValidator class and the annotations on
the Customer class, the CustomerValidatingItemProcessor only needs to concern itself with the actual
logic required for this step. In this case, you keep a running count of the number of items you receive
and print them out to standard out with each item. To use your implementation of the
ValidatingItemProcessor , the only configuration change you need to do is update the class identified in
the customerValidatingProcessor bean. Listing 8-11 shows the updated configuration.
Search WWH ::

Custom Search