Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. import java.util.Properties;
  2.  
  3. import javax.sql.DataSource;
  4.  
  5. import org.hibernate.ejb.HibernatePersistence;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.ComponentScan;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.context.annotation.PropertySource;
  11. import org.springframework.core.env.Environment;
  12. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  13. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  14. import org.springframework.orm.jpa.JpaTransactionManager;
  15. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  16. import org.springframework.transaction.annotation.EnableTransactionManagement;
  17. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  18. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  19. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  20. import org.springframework.web.servlet.view.InternalResourceViewResolver;
  21.  
  22. @Configuration
  23. @EnableWebMvc
  24. @ComponentScan
  25. @EnableJpaRepositories
  26. @EnableTransactionManagement
  27. @PropertySource(value = { "classpath:application.properties" })
  28. public class MvcConfig extends WebMvcConfigurerAdapter {
  29. @Override
  30. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  31.  
  32. System.out.println("WebMvcConfigurerAdapter: addResourceHandlers");
  33.  
  34. registry
  35. .addResourceHandler("/resources/**")
  36. .addResourceLocations("(/resources/");
  37. }
  38.  
  39. @Autowired
  40. private Environment env;
  41.  
  42. private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
  43. private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
  44. private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
  45. private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";
  46.  
  47. private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
  48. private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
  49. private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";
  50.  
  51. @Bean
  52. public InternalResourceViewResolver internalResourceViewResolver() {
  53. InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  54. resolver.setPrefix("/WEB-INF/views/");
  55. resolver.setSuffix(".jsp");
  56. return resolver;
  57. }
  58.  
  59. @Bean
  60. public DataSource dataSource() {
  61. DriverManagerDataSource dataSource = new DriverManagerDataSource();
  62.  
  63. dataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
  64. dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
  65. dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
  66. dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));
  67.  
  68. return dataSource;
  69. }
  70.  
  71.  
  72. private Properties hibernateProperties() {
  73. Properties properties = new Properties();
  74. properties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));
  75. properties.put("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql"));
  76. properties.put("hibernate.format_sql", env.getRequiredProperty("hibernate.format_sql"));
  77. properties.put("hibernate.hbm2ddl.auto", env.getRequiredProperty("hibernate.hbm2ddl.auto"));
  78. return properties;
  79. }
  80.  
  81.  
  82. @Bean
  83. public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
  84. LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
  85. entityManagerFactoryBean.setDataSource(dataSource());
  86. entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistence.class);
  87. entityManagerFactoryBean.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
  88.  
  89. entityManagerFactoryBean.setJpaProperties(hibernateProperties());
  90.  
  91. return entityManagerFactoryBean;
  92. }
  93.  
  94.  
  95. @Bean
  96. public JpaTransactionManager transactionManager() {
  97. JpaTransactionManager transactionManager = new JpaTransactionManager();
  98. transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
  99. return transactionManager;
  100. }
  101.  
  102.  
  103. }
  104.  
  105. import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
  106.  
  107. public class ServletSpringMVC extends AbstractAnnotationConfigDispatcherServletInitializer {
  108.  
  109. @Override
  110. protected Class<?>[] getRootConfigClasses() {
  111. // TODO Auto-generated method stub
  112. return null;
  113. }
  114.  
  115. @Override
  116. protected Class<?>[] getServletConfigClasses() {
  117. return new Class[]{MvcConfig.class};
  118. }
  119.  
  120. @Override
  121. protected String[] getServletMappings() {
  122. return new String[] {"/"};
  123. }
  124.  
  125. }
  126.  
  127. <link rel="stylesheet" type="text/css" href="<c:url value="/resources/bootstrap/bootstrap.min.css" />">
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement