daily pastebin goal
86%
SHARE
TWEET

Untitled

a guest Jun 30th, 2018 121 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package tech.istrategies.init;
  2.  
  3. import java.util.Properties;
  4.  
  5. import javax.annotation.Resource;
  6. import javax.sql.DataSource;
  7.  
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.ComponentScan;
  10. import org.springframework.context.annotation.Configuration;
  11. import org.springframework.context.annotation.PropertySource;
  12. import org.springframework.core.env.Environment;
  13. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  14. import org.springframework.orm.hibernate4.HibernateTransactionManager;
  15. import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
  16. import org.springframework.transaction.annotation.EnableTransactionManagement;
  17. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  18. import org.springframework.web.servlet.view.JstlView;
  19. import org.springframework.web.servlet.view.UrlBasedViewResolver;
  20.  
  21. @Configuration
  22. @ComponentScan("tech.istrategies")
  23. @EnableWebMvc
  24. @EnableTransactionManagement
  25. @PropertySource("classpath:application.properties")
  26. public class WebAppConfig {
  27.  
  28.     private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
  29.     private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
  30.     private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
  31.     private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";
  32.    
  33.     private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
  34.     private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
  35.     private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";
  36.    
  37.     @Resource
  38.     private Environment env;
  39.    
  40.     @Bean
  41.     public DataSource dataSource() {
  42.         DriverManagerDataSource dataSource = new DriverManagerDataSource();
  43.        
  44.         dataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
  45.         dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
  46.         dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
  47.         dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));
  48.        
  49.         return dataSource;
  50.     }
  51.    
  52.     @Bean
  53.     public LocalSessionFactoryBean sessionFactory() {
  54.         LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
  55.         sessionFactoryBean.setDataSource(dataSource());
  56.         sessionFactoryBean.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
  57.         sessionFactoryBean.setHibernateProperties(hibProperties());
  58.         return sessionFactoryBean;
  59.     }
  60.    
  61.     private Properties hibProperties() {
  62.         Properties properties = new Properties();
  63.         properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
  64.         properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
  65.         return properties; 
  66.     }
  67.    
  68.     @Bean
  69.     public HibernateTransactionManager transactionManager() {
  70.         HibernateTransactionManager transactionManager = new HibernateTransactionManager();
  71.         transactionManager.setSessionFactory(sessionFactory().getObject());
  72.         return transactionManager;
  73.     }
  74.    
  75.     @Bean
  76.     public UrlBasedViewResolver setupViewResolver() {
  77.         UrlBasedViewResolver resolver = new UrlBasedViewResolver();
  78.         resolver.setPrefix("/WEB-INF/pages/");
  79.         resolver.setSuffix(".jsp");
  80.         resolver.setViewClass(JstlView.class);
  81.         return resolver;
  82.     }
  83.    
  84. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top