|
| 1 | +package org.springframework.issues; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import javax.annotation.Resource; |
| 7 | +import javax.sql.DataSource; |
| 8 | + |
| 9 | +import org.hibernate.cache.HashtableCacheProvider; |
| 10 | +import org.hibernate.dialect.HSQLDialect; |
| 11 | +import org.springframework.context.annotation.AdviceMode; |
| 12 | +import org.springframework.context.annotation.Bean; |
| 13 | +import org.springframework.context.annotation.ComponentScan; |
| 14 | +import org.springframework.context.annotation.Configuration; |
| 15 | +import org.springframework.context.annotation.EnableLoadTimeWeaving; |
| 16 | +import org.springframework.context.annotation.EnableLoadTimeWeaving.AspectJWeaving; |
| 17 | +import org.springframework.context.annotation.PropertySource; |
| 18 | +import org.springframework.core.env.Environment; |
| 19 | +import org.springframework.orm.jpa.JpaDialect; |
| 20 | +import org.springframework.orm.jpa.JpaTransactionManager; |
| 21 | +import org.springframework.orm.jpa.JpaVendorAdapter; |
| 22 | +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
| 23 | +import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager; |
| 24 | +import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager; |
| 25 | +import org.springframework.orm.jpa.vendor.Database; |
| 26 | +import org.springframework.orm.jpa.vendor.HibernateJpaDialect; |
| 27 | +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; |
| 28 | +import org.springframework.transaction.PlatformTransactionManager; |
| 29 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 30 | +import org.springframework.transaction.aspectj.AnnotationTransactionAspect; |
| 31 | + |
| 32 | +@Configuration |
| 33 | +@ComponentScan("org.springframework.issues") |
| 34 | +@EnableTransactionManagement(mode = AdviceMode.ASPECTJ, order = 0) |
| 35 | +@EnableLoadTimeWeaving(aspectjWeaving = AspectJWeaving.ENABLED) |
| 36 | +@PropertySource("/config.properties") |
| 37 | +public class AppConfig { |
| 38 | + @Resource |
| 39 | + private Environment env; |
| 40 | + |
| 41 | + @Bean |
| 42 | + public Map<String, Object> jpaProperties() { |
| 43 | + Map<String, Object> props = new HashMap<String, Object>(); |
| 44 | + props.put("hibernate.dialect", HSQLDialect.class.getName()); |
| 45 | + props.put("hibernate.cache.provider_class", |
| 46 | + HashtableCacheProvider.class.getName()); |
| 47 | + props.put("hibernate.show_sql", true); |
| 48 | + props.put("hibernate.format_sql", true); |
| 49 | + return props; |
| 50 | + } |
| 51 | + |
| 52 | + @Bean |
| 53 | + public JpaVendorAdapter jpaVendorAdapter() { |
| 54 | + HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter(); |
| 55 | + hibernateJpaVendorAdapter.setShowSql(true); |
| 56 | + hibernateJpaVendorAdapter.setGenerateDdl(true); |
| 57 | + hibernateJpaVendorAdapter.setDatabase(Database.HSQL); |
| 58 | + return hibernateJpaVendorAdapter; |
| 59 | + } |
| 60 | + |
| 61 | + @Bean |
| 62 | + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { |
| 63 | + LocalContainerEntityManagerFactoryBean lcemfb = new LocalContainerEntityManagerFactoryBean(); |
| 64 | + lcemfb.setJpaVendorAdapter(jpaVendorAdapter()); |
| 65 | + lcemfb.setJpaDialect(jpaDialect()); |
| 66 | + lcemfb.setPersistenceUnitName("default"); |
| 67 | + lcemfb.setPersistenceUnitManager(persistenceUnitManager()); |
| 68 | + lcemfb.setJpaPropertyMap(jpaProperties()); |
| 69 | + lcemfb.setDataSource(dataSource()); |
| 70 | + return lcemfb; |
| 71 | + } |
| 72 | + |
| 73 | + @Bean |
| 74 | + public PersistenceUnitManager persistenceUnitManager() { |
| 75 | + DefaultPersistenceUnitManager dpum = new DefaultPersistenceUnitManager(); |
| 76 | + dpum.setDefaultDataSource(dataSource()); |
| 77 | + return dpum; |
| 78 | + } |
| 79 | + |
| 80 | + @Bean |
| 81 | + public JpaDialect jpaDialect() { |
| 82 | + return new HibernateJpaDialect(); |
| 83 | + } |
| 84 | + |
| 85 | + @Bean |
| 86 | + public AnnotationTransactionAspect annotationTransactionAspect() { |
| 87 | + AnnotationTransactionAspect ata = AnnotationTransactionAspect |
| 88 | + .aspectOf(); |
| 89 | + ata.setTransactionManager(txManager()); |
| 90 | + return ata; |
| 91 | + } |
| 92 | + |
| 93 | + @Bean(destroyMethod = "close") |
| 94 | + public DataSource dataSource() { |
| 95 | + org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource(); |
| 96 | + ds.setDriverClassName(env.getProperty("jdbc.driverClass")); |
| 97 | + ds.setUrl(env.getProperty("jdbc.url")); |
| 98 | + ds.setUsername(env.getProperty("jdbc.user")); |
| 99 | + ds.setPassword(env.getProperty("jdbc.password")); |
| 100 | + ds.setInitialSize(1); |
| 101 | + ds.setMinIdle(1); |
| 102 | + ds.setMaxIdle(1); |
| 103 | + ds.setMaxActive(2); |
| 104 | + ds.setMaxWait(5000); |
| 105 | + return ds; |
| 106 | + } |
| 107 | + |
| 108 | + @Bean |
| 109 | + public PlatformTransactionManager txManager() { |
| 110 | + JpaTransactionManager txManager = new JpaTransactionManager(); |
| 111 | + txManager.setEntityManagerFactory(entityManagerFactory().getObject()); |
| 112 | + txManager.setDataSource(dataSource()); |
| 113 | + return txManager; |
| 114 | + } |
| 115 | +} |
0 commit comments