spring自定义验证器构造函数

时间:2013-09-01 15:55:16

标签: java spring validation

我有自定义验证器

public class SimpleValidator implements ConstraintValidator<Simple, String> {

    private SimpleDao dao;   

    //@Autowired
    public SimpleValidator (SimpleDao<String> dao) {
        this.dao = dao;
    }

    @Override
    public void initialize(Simple annotation) {

    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        return dao.someAction(value);
    }
}

public class MyService {
    //@Autowired
    private Validator validator;
    public MyService (Validator validator) {
        this.validator = validator;  
    }

    public BindingResult SomeMethod(SimpleDto dto) {
        BindingResult result = new BeanPropertyBindingResult(dto, "dto");
        validator.validate(dto, result);
        return result;
    }
}

当我使用注释@Autowired(对于MyService类中的验证器)并尝试验证某些dto时,它已成功验证。但是当我尝试使用xml config

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
<bean class="org.springframework.validation.beanvalidation.BeanValidationPostProcessor">
  <property name="validator" ref="validator"/>
</bean>

<bean id="simpleValidator" class="org.jtalks.jcommune.model.validation.validators.SimpleValidator">
    <constructor-arg ref="validatorDAO"/>  
</bean>

<bean id="myService" class="org.jtalks.jcommune.service.transactional.MyService">
    <constructor-arg ref="validator"/>
</bean>

<bean id="validatorDAO"
    class="org.jtalks.jcommune.model.dao.hibernate.ValidatorHibernateDao">
  <constructor-arg ref="sessionFactory"/>
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
 ...
</property>
<property name="hibernateProperties">
  <props>
    <prop key="hibernate.query.factory_class">
      org.hibernate.hql.ast.ASTQueryTranslatorFactory
    </prop>
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
    <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
    <prop key="hibernate.connection.charset">${encoding}</prop>
    <prop key="hibernate.connection.release_mode">auto</prop>
  </props>
</property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"/>
</bean>

我得到例外:

Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.jtalks.jcommune.model.validation.validators.SimpleValidator]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.jtalks.jcommune.model.validation.validators.SimpleValidator.<init>()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1006)
... 126 more
Caused by: java.lang.NoSuchMethodException: org.jtalks.jcommune.model.validation.validators.SimpleValidator.<init>()
at java.lang.Class.getConstructor0(Class.java:2730)
at java.lang.Class.getDeclaredConstructor(Class.java:2004)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78)
... 127 more

是否可以将xml配置用于验证器?如何在不使用@Autowired注释的情况下使用SimpleDao实例化验证器?

1 个答案:

答案 0 :(得分:0)

替换以下

<bean id="simpleValidator" class="com.example.validators.SimpleValidator">
    <property name="dao" ref="validatorDao"/>
</bean>

使用此功能。

<bean id="simpleValidator" class="com.example.validators.SimpleValidator">
      <constructor-arg ref="validatorDao"/>     
</bean>

<强>更新

考虑在ValidatorHibernateDao中实现的SimpleDao接口。

 <bean id="simpleDao"  class"org.jtalks.jcommune.model.dao.hibernate.SimpleDao" abstract="true"/>

<bean id="validatorDAO"
   class="org.jtalks.jcommune.model.dao.hibernate.ValidatorHibernateDao" parent="simpleDao">
    <constructor-arg ref="sessionFactory"/>
</bean>

  public class SimpleValidator implements ConstraintValidator<Simple, String> {

     private SimpleDao<String> dao;   

  }