I'm trying to use my service into custom ContextValidator annotation using @Autowired. I looking for some help in old questions on SOF but I was unfortunately so I've created is one new. My goal is valid bean's property with annotation @MyId above it.
Here is my code:
@Documented
@Constraint(validatedBy = { MyIdValidator.class })
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
public @interface MyIdSpring {
String message() default "Invalid ID!";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
Valdiator class:
@Configurable(autowire = Autowire.BY_TYPE, dependencyCheck = true)
public class MyIdValidator implements ConstraintValidator<MyId, TestID> {
@Autowired
private ITestService testService;
@Override
public void initialize(MyId constraintAnnotation) {
}
@Override
public boolean isValid(TestID value, ConstraintValidatorContext context) {
System.out.println(testService);
return false;
}
}
configuration:
@Configuration
public class ValidatorConfig {
@Bean
public LocalValidatorFactoryBean validator() {
LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
return bean;
}
}
service:
@Service
public class TestService implements ITestService { .. }
ITestService is always null. Can you please help me to fix it? I am using spring boot 1.5.
Use @Qualifier annotation to tell Spring about your implementation class.
@Autowired
@Qualifier("testService")
private ITestService testService;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.