SpringConfiguration.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package pl.sudra.configuration;
  2. import jakarta.annotation.Resource;
  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.format.FormatterRegistry;
  9. import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
  10. import org.springframework.web.servlet.LocaleResolver;
  11. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  12. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  13. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  14. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  15. import org.springframework.web.servlet.i18n.CookieLocaleResolver;
  16. import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
  17. import org.springframework.web.servlet.view.InternalResourceViewResolver;
  18. import pl.sudra.service.AddressService;
  19. import pl.sudra.service.AppUserRoleService;
  20. import pl.sudra.utils.AddressConverter;
  21. import pl.sudra.utils.AppUserRoleConverter;
  22. import pl.sudra.utils.AppUserRoleListConverter;
  23. import java.time.Duration;
  24. import java.util.Locale;
  25. @Configuration
  26. @EnableWebMvc
  27. @ComponentScan("pl.sudra")
  28. //@Import({SecurityConfiguration.class})
  29. public class SpringConfiguration implements WebMvcConfigurer {
  30. @Bean
  31. public InternalResourceViewResolver viewResolver() {
  32. InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
  33. viewResolver.setPrefix("/");
  34. viewResolver.setSuffix(".jsp");
  35. return viewResolver;
  36. }
  37. @Bean
  38. public MessageSource messageSource() {
  39. ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
  40. messageSource.setBasename("/resources/i18n/messages");
  41. messageSource.setDefaultEncoding("UTF-8");
  42. return messageSource;
  43. }
  44. @Bean
  45. public LocaleResolver localeResolver() {
  46. CookieLocaleResolver resolver = new CookieLocaleResolver("myLocaleCookie");
  47. resolver.setDefaultLocale(new Locale("en"));
  48. resolver.setCookieMaxAge(Duration.ofSeconds(4800));
  49. return resolver;
  50. }
  51. @Override
  52. public void addInterceptors(InterceptorRegistry registry) {
  53. LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
  54. interceptor.setParamName("lang");
  55. registry.addInterceptor(interceptor);
  56. }
  57. @Override
  58. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  59. registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
  60. }
  61. @Bean
  62. @Override
  63. public LocalValidatorFactoryBean getValidator() {
  64. LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
  65. bean.setValidationMessageSource(messageSource());
  66. return bean;
  67. }
  68. @Resource(name = "addressService")
  69. private AddressService addressService;
  70. @Resource(name = "appUserRoleService")
  71. private AppUserRoleService appUserRoleService;
  72. @Override
  73. public void addFormatters(FormatterRegistry formatterRegistry) {
  74. formatterRegistry.addConverter(getMyAddressConverter());
  75. formatterRegistry.addConverter(getMyUserRoleConverter());
  76. formatterRegistry.addConverter(getMyUserRoleListConverter());
  77. }
  78. @Bean
  79. public AddressConverter getMyAddressConverter() {
  80. return new AddressConverter(addressService);
  81. }
  82. @Bean
  83. public AppUserRoleConverter getMyUserRoleConverter() {
  84. return new AppUserRoleConverter(appUserRoleService);
  85. }
  86. @Bean
  87. public AppUserRoleListConverter getMyUserRoleListConverter() {
  88. return new AppUserRoleListConverter(appUserRoleService);
  89. }
  90. }