我AnnotationConfigApplicationContext
创建了带@Configuration
注释的类作为参数:
@Configuration
class Config {
@Bean
public LocalValidatorFactoryBean localValidatorFactoryBean() {
return new LocalValidatorFactoryBean();
}
@Bean
public A aBean() {
return new A();
}
@Bean
public B aBean() {
return new B();
}
}
地点A
和地点B
:
class A {
@Min(1)
public int myInt;
}
class B {
@Autowire(required = true)
@Valid
public A aBean;
}
Q:这种情况下是否可以让Spring来处理@Valid
注解?
PS:目前我有以下工作实施B
:
class B {
public A aBean;
public void setABean(A aBean, Validator validator) {
if (validator.validate(aBean).size() > 0) {
throw new ValidationException();
}
this.aBean = aBean;
}
}
这个 impl 对我来说似乎有点笨拙,我想替换它。请帮忙 :)