Spring 3.1自动装配在自定义约束验证器中不起作用

时间:2012-10-01 15:28:12

标签: spring validation customvalidator hibernate-validator

我在自定义约束验证器中遇到bean自动装配的问题。使用Spring的LocalValidatorFactoryBean不会给出约束验证器实例。  JSR-303提供程序是hibernate-validator 4.2.0.Final。

Spring配置摘录:

<!-- JSR 303 validation -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor"/> 

自定义约束验证器:

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.beans.factory.annotation.Autowired;

import com.model.Subject;
import com.services.SomeTypeService;

public class ReadOnlyValidator implements ConstraintValidator<ReadOnly, String> {

@Autowired
private SomeTypeService someTypeService;

@Override
public void initialize(ReadOnly constraintAnnotation) { }

@Override
public boolean isValid(String type, ConstraintValidatorContext context) {
    try {
         if (null != type) {
                        return !someTypeService.isReadonly(type);
                     }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return false;
}
}

注释:

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;

@Target( { METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER } )
@Retention(RUNTIME)
@Constraint(validatedBy = ReadOnlyValidator.class)
@Documented
public @interface ReadOnly {
String message() default "{constraints.subjecttype.readonly}";
public abstract Class<?>[] groups() default {};
public abstract Class<? extends Payload>[] payload() default {};
}

SomeService:

 @Validated
 public interface SomeService {
    ...
    public void updateType(@ReadOnly String type) throws SomeException;
    ...
 }

SomeServiceImpl:

 @Service
 public class SomeServiceImpl implements SomeService {
  ...
    public void updateType(String type) throws SomeException {
     // do something
    }
  ...
 }

SomeTypeService是另一个@Service注释bean,它不依赖于SomeService ...

问题是我得到一个NPE,因为自动装配不起作用;其他人正在管理自定义验证器实例,而不是Spring ......

提前感谢任何类型的建议。

2 个答案:

答案 0 :(得分:9)

发现问题。 MethodValidationPostProcessor和mvc:annotation-driven声明必须使用相同的“validator”bean引用:

service-context.xml

<!-- JSR 303 validation -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
<bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor">
    <property name="validator" ref="validator"/>
</bean>
    ...

<强>调度-servlet.xml中

...
<mvc:annotation-driven validator="validator" />
...

答案 1 :(得分:4)

Tito,请检查您是否可以使用以下内容进行Java配置:

@Configuration
public class SampleConfig {
  ...

  @Bean
  public Validator validator() {
    return new LocalValidatorFactoryBean().getValidator();
  }

  @Bean
  public MethodValidationPostProcessor mvpp() {
    MethodValidationPostProcessor mvpp = new MethodValidationPostProcessor();
    mvpp.setValidator(validator());

    return mvpp;
  }

  ...
}

要在不同的配置bean中重用验证器bean,可以使用@Import批注。