Guest User

Untitled

a guest
Feb 19th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. package com.gmail.exet.brother.TechTask;
  2.  
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.context.annotation.PropertySource;
  7. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  8. import org.springframework.orm.jpa.JpaTransactionManager;
  9. import org.springframework.orm.jpa.JpaVendorAdapter;
  10. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  11. import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  12. import org.springframework.transaction.PlatformTransactionManager;
  13. import org.springframework.transaction.annotation.EnableTransactionManagement;
  14. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  15. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  16. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  17. import org.springframework.web.servlet.view.JstlView;
  18. import org.springframework.web.servlet.view.UrlBasedViewResolver;
  19.  
  20. import javax.persistence.EntityManagerFactory;
  21. import javax.sql.DataSource;
  22. import java.util.Properties;
  23.  
  24. @Configuration
  25. @PropertySource("classpath:config.properties")
  26. @EnableTransactionManagement
  27. @EnableWebMvc
  28. public class AppConfig extends WebMvcConfigurerAdapter {
  29.  
  30. @Value("${hibernate.dialect}")
  31. private String sqlDialect;
  32.  
  33. @Value("${hbm2ddl.auto}")
  34. private String hbm2dllAuto;
  35.  
  36. @Bean
  37. public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
  38. return new JpaTransactionManager(emf);
  39. }
  40.  
  41. @Bean
  42. public LocalContainerEntityManagerFactoryBean entityManagerFactory
  43. (DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
  44. Properties jpaProp = new Properties();
  45. jpaProp.put("hibernate.hbm2ddl.auto", hbm2dllAuto);
  46.  
  47. LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
  48. entityManagerFactory.setDataSource(dataSource);
  49. entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter);
  50. entityManagerFactory.setPackagesToScan("com.gmail.exet.brother.TechTask");
  51. entityManagerFactory.setJpaProperties(jpaProp);
  52.  
  53. return entityManagerFactory;
  54. }
  55.  
  56. @Bean
  57. public JpaVendorAdapter jpaVendorAdapter() {
  58. HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
  59. adapter.setDatabasePlatform(sqlDialect);
  60. return adapter;
  61. }
  62.  
  63. @Bean
  64. public DataSource dataSource() {
  65. DriverManagerDataSource ds = new DriverManagerDataSource();
  66. ds.setDriverClassName("com.mysql.jdbc.Driver");
  67. ds.setUrl("jdbc:mysql://localhost:3306/urltester");
  68. ds.setUsername("root");
  69. ds.setPassword("password");
  70. return ds;
  71. }
  72.  
  73. @Bean
  74. public UrlBasedViewResolver setupViewResolver() {
  75. UrlBasedViewResolver resolver = new UrlBasedViewResolver();
  76. resolver.setPrefix("/WEB-INF/");
  77. resolver.setSuffix(".jsp");
  78. resolver.setViewClass(JstlView.class);
  79. resolver.setOrder(1);
  80. return resolver;
  81. }
  82.  
  83. @Override
  84. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  85. registry
  86. .addResourceHandler("/resources/**")
  87. .addResourceLocations("/resources/");
  88. }
  89. }
Add Comment
Please, Sign In to add comment