I am completely new to Spring, and I looked at several answers on SO for the given problem. Here are the links:
Spring 3.1 Autoinstall does not work inside validator validation
Validator Service Authentication
The auto-installed repository is null in the user limiter
Basically, I'm not good enough at Spring to understand what is happening in the answers. Please come with me. I am posting my code so that I would really appreciate it if you could point me in the right direction.
I have a Spring project in which I want to use the Hibernate Validator to validate an object. Based on what I read on the Internet and in several forums, I tried to introduce a validator as follows:
@Bean
public Validator validator() {
return new LocalValidatorFactoryBean().getValidator();
}
,
@Autowired
Validator validator;
Hibernate Spring. , Hibernate validator Autowire , , Java Config :
@Bean
public Validator validator() {
return new ValidatorImpl();
}
( , - , Hibernate Validator )
:
@Target( { METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER } )
@Retention(RUNTIME)
@Constraint(validatedBy = EmployeeValidator.class)
@Documented
public @interface EmployeeValidation {
String message() default "{constraints.employeeConstraints}";
public abstract Class<?>[] groups() default {};
public abstract Class<? extends Payload>[] payload() default {};
}
public class EmployeeValidator implements ConstraintValidator<EmployeeValidation , Object> {
@Autowired
private EmployeeService employeeService;
@Override
public void initialize(EmployeeValidation constraintAnnotation) {
}
@Override
public boolean isValid(String type, ConstraintValidatorContext context) {
return false;
}
}
null employeeService. , ConstraintValidator Spring, , ValidatorImpl() . .
, . -, , .
P.S. Java:
import org.hibernate.validator.HibernateValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.env.Environment;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.view.InternalResourceViewResolver;