The Java Object Validation Framework |
||
i-screen.org |
Download (SourceForge Hosted) Documentation
Articles Feedback |
Spring Quick GuideThis guide will help you get iScreen up and working with the Spring Inversion of Control Framework. OverviewConfiguring iScreen via Spring (and any other IoC container, for that matter) requires two basic things: configuring the factory (factory of individual validation services) that represents a configuration; and the individual validation services. The factory needs to know where the configuration file is. A validation service represents a particular set of validations for a particular Java object and is retrieved from the factory. Configure The FactoryIn order to inject ValidationServices into an object, you need to configure the ValidationFactory. The factory needs to know what kind of configuration type is being used (currently, that means XML), the location of the root configuration file, and any services that the configuration requires (those Validators that have service configurations). The following Spring configuration defines the ValidationFactory to use XML configuration files, and defines the root configuration file to be my/projects/validations.xml. Note that in this example, there are no services.
<bean id="iScreenFactory"
class="org.iscreen.ValidationFactoryConfig">
<constructor-arg><value>my/projects/validations.xml</value></constructor-arg>
</bean>
The org.iscreen.ValidationFactoryConfig class has a number of constructors and setters (so, you can use construction-based injection, or setter-based). The previous example is the simplest, and assumes that the configuration file is the default XML type (no others are supported at this time), and there are no special services that the factory needs to make available to the configured validators. To make services available (such as DataSources, other Spring beans, etc.) you can use the following constructor (though, you could use setters, instead). The last argument is a map of those services, with the keys representing the the service-id attribute on the constraints defined for the validators needing the services.
<bean id="iScreenFactory"
class="org.iscreen.ValidationFactory">
<constructor-arg index="0"><value>my/projects/validations.xml</value></constructor-arg>
<constructor-arg index="1">
<map>
<entry>
<key><value>key</value></key>
<value>some value</value>
</entry>
</map>
</constructor-arg>
</bean>
Once the validation factory is defined, you can define each ValidationService that needs to be injected into another bean. This is done by referencing the already configured factory and referencing the particular validation set that you want to use (there's a one-to-one relationship between a Validation Set defined in your validation configuration and a org.iscreen.ValidationService that's injected via the following configuration). Note that the org.iscreen.ValidationServiceWrapper has different constructors and setters, so see it's JavaDoc for different approaches to constructing it.
<bean id="SomeValidation"
class="org.iscreen.ValidationServiceWrapper">
<constructor-arg index="0"><ref name="iScreenFactory" /></constructor-arg>
<constructor-arg index="1"><value>validation set name</value></constructor-arg>
</bean>
Finally, you can inject the org.iscreen.ValidationService into another Spring-configured bean so that it can access the interface (and API) directly.
<bean id="someBean" class="someClass">
<property name="validation"><ref name="SomeValidation" /></property>
</bean>
|
|
|