Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cc.serviceops.config;
- 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.support.ReloadableResourceBundleMessageSource;
- import org.springframework.validation.Validator;
- import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
- import org.springframework.web.context.WebApplicationContext;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
- @Configuration
- @ComponentScan("cc.serviceops")
- public class WebConfig implements WebMvcConfigurer {
- private WebApplicationContext context;
- @Autowired
- public void setContext(WebApplicationContext context) {
- this.context = context;
- }
- // ========= Hibernate validation ========= //
- @Bean
- public LocalValidatorFactoryBean createValidator() {
- LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean();
- validatorFactoryBean.setValidationMessageSource(messageSource());
- return validatorFactoryBean;
- }
- // Custom messages
- @Bean
- public MessageSource messageSource() {
- ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
- messageSource.setBasename("classpath:message");
- messageSource.setDefaultEncoding("UTF-8");
- return messageSource;
- }
- // Replace the existing configurer class' validator method with ours
- @Override
- public Validator getValidator() {
- LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
- bean.setValidationMessageSource(messageSource());
- return bean;
- //return createValidator();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement