Springは、Beanの検証に3つのハンドルを提供します。
1.abstractクラスAbstractPropertyValidationAnnotationHandler
2.abstractクラスAbstractMethodValidationAnnotationHandler
3.abstractクラスClassValidationAnnotationHandler
この例では、私は、カスタム注釈CustomAnnotationHandle
を実施しています
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
Class CustomAnnotationHandle extends Annotation{
public abstract String value();
}
プロパティの検証にカスタムアノテーションを実装するには、AbstractPropertyValidationAnnotationHandlerクラスを拡張する必要があります。
AbstractPropertyValidationAnnotationHandlerだからcreateValidationRule抽象メソッド
protected abstract AbstractValidationRule createValidationRule(Annotation annotation, Class class1, String s);
を提供し、拡張クラスは
protected abstract AbstractValidationRule createValidationRule(Annotation annotation, Class class1, String s)
public class CustomPropertyAnnotationHandler extends AbstractPropertyValidationAnnotationHandler
{
public CustomPropertyAnnotationHandler()
{
super(new Class[] {
XXX.XXX.PackageLevle.CustomAnnotationHandle // as it takes array of custom annotation ,so we can pass more than one
// overwriting abstract method
protected AbstractValidationRule createValidationRule(Annotation annotation, Class class1, String s){
CustomAnnotationHandle value = (CustomAnnotationHandle)annotation;
return TestValidationRule(value.getValue());
// as you can see it return AbstractValidationRule.So, we need a class to give our bean specific validation rule.In our case it is
//TestValidationRule
}
}
}
public class TestValidationRule extends AbstractValidationRule
{
public TestValidationRule (String valuetest)
{
super();
this.valuetest = valuetest;
}
Private String valuetest;
}
春AnnotationBeanValidationConfigurationLoader Class.Thisクラスはスプリング自身の注釈のために使用される提供の実装を提供しなければなりませんBeanの検証のため。
defaultValidationAnnotationHandlerRegistryクラスがdefaultHandlerRegistryとして使用されます。私たちは私たち自身のannotaionを提供する必要がある場合しかし、その後、私たち
は、メソッドを介して setHandlerRegistry(新しいCustomPropertyAnnotationHandlerを())AnnotationBeanValidationConfigurationLoaderを拡張し、当社の特定handleregistryを設定する必要があります。我々は
に必要なクラスDefaultValidationAnnotationHandlerRegistryは、当社のカスタム注釈のためのSimpleValidationAnnotationHandlerRegistry class.SoのregisterPropertyHandlerメソッドを呼び出す
でBeanを登録validation.It Beanの春独自の注釈を登録するために使用される
registerPropertyHandlerメソッドを呼び出すことによってCustomPropertyAnnotationHandlerを登録SimpleValidationAnnotationHandlerRegistryクラスの
public class OurBeanSpecificValidationLoader extends AnnotationBeanValidationConfigurationLoader
{
public OurBeanSpecificValidationLoader()
{
super();
setHandlerRegistry(new OurSpecificAnnotationHandleRegistery());
}
}
public class OurSpecificAnnotationHandleRegistery extends DefaultValidationAnnotationHandlerRegistry
{
public OurSpecificAnnotationHandleRegistery()
{
registerPropertyHandler(new CustomPropertyAnnotationHandler());
}
}
Beanの注釈valiation.Eg
@CustomAnnotationHandle(value = "test")
private Object test;
これは、新しい承認された回答である必要があります。 – shadowhorst
これは私が使用しているものですが、私は 'JSR-303'バリデーターを* BEFORE * my'カスタムバリデーター 'と呼ぶ必要があります。それを達成することは可能ですか? JSR-303の基本的な検証が失敗した場合(@NotNullsなど) – Doug
私のカスタムバリデーターは、JSR-303が手にしていないのでヌルポインタ例外を投げているのでnullですカスタムバリデータが起動される前にバリデーションが行われていない – Doug