Guest User

Untitled

a guest
Feb 25th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. package org.alexey.mvcbooks.config;
  2.  
  3. import org.alexey.mvcbooks.dao.BooksDAO;
  4. import org.alexey.mvcbooks.dao.impl.BookDAOImpl;
  5. import org.hibernate.SessionFactory;
  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.context.support.ResourceBundleMessageSource;
  12. import org.springframework.core.env.Environment;
  13. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  14. import org.springframework.orm.hibernate5.HibernateTransactionManager;
  15. import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
  16. import org.springframework.transaction.annotation.EnableTransactionManagement;
  17. import org.springframework.web.servlet.view.InternalResourceViewResolver;
  18.  
  19. import javax.sql.DataSource;
  20. import java.util.Properties;
  21.  
  22. @Configuration
  23. @ComponentScan("org.alexey.mvcbooks*")
  24. @EnableTransactionManagement
  25. // Load to Environment.
  26. @PropertySource("classpath:ds-hibernate-cfg.properties")
  27. public class ApplicationContextConfig {
  28.  
  29. // The Environment class serves as the property holder
  30. // and stores all the properties loaded by the @PropertySource
  31. @Autowired
  32. private Environment env;
  33.  
  34.  
  35.  
  36. @Bean
  37. public ResourceBundleMessageSource messageSource() {
  38. ResourceBundleMessageSource rb = new ResourceBundleMessageSource();
  39. // Load property in message/validator.properties
  40. rb.setBasenames(new String[] { "messages/validator"});
  41. return rb;
  42. }
  43.  
  44.  
  45. @Bean(name = "viewResolver")
  46. public InternalResourceViewResolver getViewResolver() {
  47. InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
  48. viewResolver.setPrefix("/WEB-INF/pages/");
  49. viewResolver.setSuffix(".jsp");
  50. return viewResolver;
  51. }
  52.  
  53. @Bean(name = "dataSource")
  54. public DataSource getDataSource() {
  55. DriverManagerDataSource dataSource = new DriverManagerDataSource();
  56.  
  57. dataSource.setDriverClassName(env.getProperty("ds.database-driver"));
  58. dataSource.setUrl(env.getProperty("ds.url"));
  59. dataSource.setUsername(env.getProperty("ds.username"));
  60. dataSource.setPassword(env.getProperty("ds.password"));
  61.  
  62. return dataSource;
  63. }
  64.  
  65. @Autowired
  66. @Bean(name = "sessionFactory")
  67. public SessionFactory getSessionFactory(DataSource dataSource) throws Exception {
  68. Properties properties = new Properties();
  69.  
  70. // See: ds-hibernate-cfg.properties
  71. properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
  72. properties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
  73. properties.put("current_session_context_class", env.getProperty("current_session_context_class"));
  74.  
  75.  
  76. LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
  77. factoryBean.setPackagesToScan(new String[] { "org.alexey.mvcbooks.entity" });
  78. factoryBean.setDataSource(dataSource);
  79. factoryBean.setHibernateProperties(properties);
  80. factoryBean.afterPropertiesSet();
  81. //
  82. SessionFactory sf = factoryBean.getObject();
  83. return sf;
  84. }
  85.  
  86. @Autowired
  87. @Bean(name = "transactionManager")
  88. public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
  89. HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
  90.  
  91. return transactionManager;
  92. }
  93.  
  94. @Bean(name = "bookDAO")
  95. public BooksDAO getBookDAO() {
  96. return new BookDAOImpl();
  97. }
  98.  
  99. }
Add Comment
Please, Sign In to add comment