Advertisement
ImHungryHi

webconfig

Sep 17th, 2021
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. package cc.serviceops.config;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.context.MessageSource;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.ComponentScan;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.context.support.ReloadableResourceBundleMessageSource;
  8. import org.springframework.validation.Validator;
  9. import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
  10. import org.springframework.web.context.WebApplicationContext;
  11. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  12.  
  13. @Configuration
  14. @ComponentScan("cc.serviceops")
  15. public class WebConfig implements WebMvcConfigurer {
  16. private WebApplicationContext context;
  17.  
  18. @Autowired
  19. public void setContext(WebApplicationContext context) {
  20. this.context = context;
  21. }
  22.  
  23. // ========= Hibernate validation ========= //
  24. @Bean
  25. public LocalValidatorFactoryBean createValidator() {
  26. LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean();
  27. validatorFactoryBean.setValidationMessageSource(messageSource());
  28. return validatorFactoryBean;
  29. }
  30.  
  31. // Custom messages
  32. @Bean
  33. public MessageSource messageSource() {
  34. ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
  35. messageSource.setBasename("classpath:message");
  36. messageSource.setDefaultEncoding("UTF-8");
  37. return messageSource;
  38. }
  39.  
  40. // Replace the existing configurer class' validator method with ours
  41. @Override
  42. public Validator getValidator() {
  43. LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
  44. bean.setValidationMessageSource(messageSource());
  45. return bean;
  46. //return createValidator();
  47. }
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement