Sorry if I added it so late guys :(
Please help me fixing the English since I'm not a native English
speaker, if you can... thanks in advance!!!
Best regards,
Simo

PS I also notified the Google Guice community[1] about integration, I
hope I did right, let's wait for their feedbacks...
Yours,
Simo

[1] http://markmail.org/message/6opn5q34dsbtushc

http://people.apache.org/~simonetripodi/
http://www.99soft.org/



On Mon, Jun 28, 2010 at 12:09 PM,  <[email protected]> wrote:
> Space: Apache Bean Validation 
> (https://cwiki.apache.org/confluence/display/BeanValidation)
> Page: Obtaining a validator 
> (https://cwiki.apache.org/confluence/display/BeanValidation/Obtaining+a+validator)
>
>
> Edited by Simone Tripodi:
> ---------------------------------------------------------------------
> To obtain a validator, you must first create a ValidatorFactory. If there is 
> only one jsr303 implementation in your classpath, you can use:
>
> {code:java}
> ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
> {code}
>
> to obtain the factory. If there are various implementations in the classpath, 
> or you want to be sure you are using the Apache one, you can use:
>
> {code:java}
> ValidatorFactory avf = 
> Validation.byProvider(ApacheValidationProvider.class).configure().buildValidatorFactory();
> {code}
>
> You should not instantiate more than one factory, as factory creation is a 
> costly process and the factory also acts as a constraint cache for the 
> validators.
>
> Once you have a ValidatorFactory, obtaining a validator just requires you to 
> call {{ValidatorFactory#getValidator()}}. The validator implementation is 
> thread-safe, so you can choose to re-use a single instance of it in all your 
> code or create validators on demand: both options are fine and should perform 
> equally well.
>
> Below is an example that will create a singleton ValidatorFactory and will 
> let you obtain validators from it:
>
> {code:java}
> public enum MyValidatorFactory {
>
>    SINGLE_INSTANCE {
>
>        ValidatorFactory avf = 
> Validation.byProvider(ApacheValidationProvider.class).configure().buildValidatorFactory();
>
>       �...@override
>        public Validator getValidator() {
>            return avf.getValidator();
>        }
>
>    };
>
>    public abstract Validator getValidator();
> }
> {code}
>
> Using the above class, obtaining a validator just requires you to call: 
> {{MyValidatorFactory.SINGLE_INSTANCE.getValidator()}}
>
>
> h3. Using Spring
>
> If you are using Spring, you can easily inject validators in your beans. 
> Simply configure the factory in your applicationContext by adding:
>
> {code:xml}
>    <!-- Validator bean -->
>    <bean id="validator" 
> class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
>        <property name="providerClass" 
> value="org.apache.bval.jsr303.ApacheValidationProvider" />
>    </bean>
> {code}
>
> And Spring will be able to inject Validators and the ValidatorFactory in your 
> beans.
>
> h3. Using Google Guice
>
> _Apache Bean Validation_ comes with the {{bval-guice}} module that makes 
> easier the integration with _Google Guice_. That module has multiple 
> purposes, such:
>
> * bootstrap _Apache Bean Validation_ using _Google Guice_;
> * obtain _javax.validation.ConstraintValidator_ instances using the _Google 
> Guice Injector_, to easily support the DI;
> * easily inject the _javax.validation.Validator_ reference into components 
> that require it;
> * easily intercept methods and validate method arguments.
>
> First of all, users have to add the {{bval-guice}} module in the classpath; 
> _Apache Maven_ users can easily include it just by adding the following 
> dependency in the POM:
>
> {code:xml}
> <dependency>
>    <groupId>org.apache.bval</groupId>
>    <artifactId>bval-guice</artifactId>
>    <version>0.1-incubating</version>
> </dependency>
> {code}
>
> Let's have a look at the features:
>
> h5. Apache Bean Validation bootstrapping
>
> Simply, the {{org.apache.bval.guice.ValidationModule}} is the _Google Guice_ 
> module that bootstraps _Apache Bean Validation_, all users have to do is just 
> adding this module when creating the _Google Guice Injector_:
>
> {code:java}
> import com.google.inject.Guice;
> import com.google.inject.Injector;
>
> import org.apache.bval.guice.ValidationModule;
>
> Injector injector = Guice.createInjector([...], new ValidationModule(), 
> [...]);
> {code}
>
> h5. obtain _javax.validation.ConstraintValidator_ instances
>
> Users can implement now _javax.validation.ConstraintValidator_ classes that 
> require _Dependency Injection_ by _Google Guice_:
>
> {code:java}
> import javax.validation.ConstraintValidator;
>
> public class MyCustomValidator implements ConstraintValidator<MyAssert, 
> MyType> {
>
>    private final MyExternalService service;
>
>   �...@inject
>    public MyCustomValidator(MyExternalService service) {
>        this.service = service;
>    }
>
>    public void initialize(MyAssert annotation) {
>        // do something
>    }
>
>    public boolean isValid(MyType value, ConstraintValidatorContext context) {
>        return value == null || this.service.doSomething(value);
>    }
>
> }
> {code}
>
> Don't forget to bind the {{MyExternalService}} class in the _Google Guice 
> Bincer_!!!
>
> h5. Inject the _javax.validation.Validator_ reference
>
> Clients can easily inject {{javax.validation.Validator}} instances into their 
> custom components just marking it using the _Google Guice Inject_ annotation:
>
> {code:java}
> import javax.validation.Validator;
>
> public class MyValidatorClient {
>
>   �...@inject
>    private Validator validator;
>
>    public void setValidator(Validator validator) {
>        this.validator = validator;
>    }
>
>    ...
>
> }
> {code}
>
> When obtaining {{MyValidatorClient}} instances from the _Injector_, the 
> {{javax.validation.Validator}} will be automagically bound.
>
> h5. Intercept methods and validate method arguments
>
> Taking advantage from the _Apache Bean Validation_ extension to validate 
> method arguments, the {{bval-guice}} comes with an _AOP_ interceptor - 
> automatically initialized in the {{org.apache.bval.guice.ValidationModule}} - 
> that makes easier the methods arguments validation.
>
> All users have to do is annotate interested methods with 
> {{org.apache.bval.guice.Validate}} annotation, then annotate arguments with 
> constraints, as follows below:
>
> {code:java}
> import javax.validation.constraints.NotNull;
> import javax.validation.constraints.Size;
>
> import org.apache.bval.guice.Validate;
>
> public class MyService {
>
>   �...@validate(
>            groups = { MyGroup.class },
>            validateReturnedValue = true
>    )
>    public Country insertCountry(@NotNull(groups = { MyGroup.class })
>            String name,
>           �...@notnull(groups = { MyGroup.class })
>           �...@size(max = 2, groups = { MyGroup.class, MyOtherGroup.class })
>            String iso2Code,
>           �...@notnull(groups = { MyGroup.class })
>           �...@size(max = 3, groups = { MyGroup.class, MyOtherGroup.class })
>            String iso3Code) {
>
>        return ...;
>    }
>
> }
> {code}
>
> The {{org.apache.bval.guice.Validate}} supports 2 parameters:
>
> * {{groups}} Class array, _empty_ by default, that marks the groups have to 
> be validated;
> * {{validateReturnedValue}} flag, _false_ by default, that marks that if the 
> returned object by the method execution has to be validated.
>
> h3. Using CDI
>
> TBD
>
> Change your notification preferences: 
> https://cwiki.apache.org/confluence/users/viewnotifications.action
>

Reply via email to