Guest User

Untitled

a guest
Jan 16th, 2018
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. package com.myapp.spring.config;
  2.  
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import java.util.Properties;
  6.  
  7. import javax.persistence.EntityManagerFactory;
  8. import javax.sql.DataSource;
  9.  
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.context.annotation.Bean;
  12. import org.springframework.context.annotation.ComponentScan;
  13. import org.springframework.context.annotation.Conditional;
  14. import org.springframework.context.annotation.Configuration;
  15. import org.springframework.jdbc.core.JdbcTemplate;
  16. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  17. import org.springframework.orm.jpa.JpaTransactionManager;
  18. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  19. import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  20. import org.springframework.scheduling.TaskScheduler;
  21. import org.springframework.scheduling.annotation.EnableScheduling;
  22. import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
  23. import org.springframework.transaction.PlatformTransactionManager;
  24.  
  25. @Configuration()
  26. @ComponentScan(basePackages="com.myapp.spring")
  27.  
  28. public class AppConfig {
  29.  
  30.  
  31.  
  32. @Bean
  33. public DataSource dataSource() {
  34. DriverManagerDataSource dataSource=new DriverManagerDataSource();
  35. dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  36. dataSource.setUrl("jdbc:mysql://localhost:3306/hibernatespring");
  37. dataSource.setUsername("admin");
  38. dataSource.setPassword("admin");
  39. return dataSource;
  40.  
  41. }
  42.  
  43. @Bean
  44. public Properties jpaProperties() {
  45. Properties properties=new Properties();
  46. properties.put("hibernate.hbm2ddl.auto", "update");
  47. properties.put("hibernate.show_sql", "true");
  48. return properties;
  49. }
  50.  
  51. // Spring Provided Class and implements EntityManagerFactory of JPA
  52. @Bean(name="entityManagerFactory")
  53. @Autowired
  54. public LocalContainerEntityManagerFactoryBean entityManagerFactory(
  55. DataSource dataSource,Properties jpaProperties) {
  56. LocalContainerEntityManagerFactoryBean emf=
  57. new LocalContainerEntityManagerFactoryBean();
  58. emf.setDataSource(dataSource);
  59. emf.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
  60. emf.setPackagesToScan("com.myapp.spring.hibernate.model");
  61. emf.setJpaProperties(jpaProperties);
  62. emf.setPersistenceUnitName("default");
  63. emf.afterPropertiesSet();
  64. return emf;
  65.  
  66.  
  67.  
  68. }
  69.  
  70. @Bean
  71. @Autowired
  72. public PlatformTransactionManager transactionManager(
  73. EntityManagerFactory entityManagerFactory) {
  74. return new JpaTransactionManager(entityManagerFactory);
  75. }
  76.  
  77.  
  78.  
  79.  
  80. }
Add Comment
Please, Sign In to add comment