Advertisement
Guest User

Untitled

a guest
Apr 9th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. package first.test;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.ComponentScan;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
  7. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  8. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  9. import org.springframework.orm.jpa.JpaTransactionManager;
  10. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  11. import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
  12. import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  13. import org.springframework.transaction.PlatformTransactionManager;
  14. import org.springframework.transaction.annotation.EnableTransactionManagement;
  15. import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
  16. import org.springframework.web.servlet.LocaleContextResolver;
  17. import org.springframework.web.servlet.ViewResolver;
  18. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  19. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  20. import org.springframework.web.servlet.i18n.SessionLocaleResolver;
  21. import org.springframework.web.servlet.view.InternalResourceViewResolver;
  22. import org.springframework.web.servlet.view.JstlView;
  23.  
  24. import javax.persistence.EntityManagerFactory;
  25. import javax.sql.DataSource;
  26. import javax.validation.Validator;
  27. import java.util.Locale;
  28. import java.util.Properties;
  29.  
  30. @Configuration
  31. @EnableWebMvc
  32. @ComponentScan(basePackages = "first.test")
  33. @EnableTransactionManagement
  34. public class WebConfig implements WebMvcConfigurer {
  35. @Bean
  36. public ViewResolver internalResourceViewResolver() {
  37. InternalResourceViewResolver bean = new InternalResourceViewResolver();
  38. bean.setViewClass(JstlView.class);
  39. bean.setPrefix("/WEB-INF/templates/");
  40. bean.setSuffix(".jsp");
  41. return bean;
  42. }
  43.  
  44. @Bean(name="localeResolver")
  45. public LocaleContextResolver getLocaleContextResolver() {
  46. SessionLocaleResolver localeResolver = new SessionLocaleResolver();
  47. localeResolver.setDefaultLocale(new Locale("pl","PL"));
  48. return localeResolver;
  49. }
  50.  
  51. @Bean
  52. public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
  53. LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
  54. em.setDataSource(dataSource());
  55. em.setPackagesToScan(new String[] {"first.test"});
  56. em.setJpaDialect(new HibernateJpaDialect());
  57. em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
  58. em.setJpaProperties(additionalProperties());
  59. return em;
  60. }
  61.  
  62. Properties additionalProperties() {
  63. Properties properties = new Properties();
  64. properties.setProperty("hibernate.hbm2ddl.auto", "drop-and-create");
  65. properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
  66. properties.setProperty("hibernate.show_sql", "true");
  67. properties.setProperty("hibernate.format_sql", "true");
  68. return properties;
  69. }
  70.  
  71. @Bean
  72. public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
  73. JpaTransactionManager transactionManager = new JpaTransactionManager();
  74. transactionManager.setEntityManagerFactory(emf);
  75. return transactionManager;
  76. }
  77.  
  78. @Bean
  79. public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
  80. return new PersistenceExceptionTranslationPostProcessor();
  81. }
  82.  
  83. @Bean
  84. public DataSource dataSource() {
  85. DriverManagerDataSource dataSource = new DriverManagerDataSource();
  86. dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  87. dataSource.setUrl("jdbc:mysql://localhost:3306/testdb?useSSL=false");
  88. dataSource.setUsername("root");
  89. dataSource.setPassword("tajne123");
  90. return dataSource;
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement