-1

我有自定义验证器

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 配置

<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 实例化验证器?

4

1 回答 1

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;   

  }
于 2013-09-01T16:22:08.060 回答