/
ApplicationContextConfiguration.java
67 lines (55 loc) · 2.49 KB
/
ApplicationContextConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.tmm.web.configuration;
import java.beans.PropertyVetoException;
import javax.persistence.EntityManagerFactory;
import org.hibernate.ejb.HibernatePersistence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.mchange.v2.c3p0.ComboPooledDataSource;
@Configuration
@EnableTransactionManagement(mode=AdviceMode.ASPECTJ, proxyTargetClass=true)
@ComponentScan("com.tmm.web")
@PropertySource("classpath:META-INF/spring/database.properties")
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class ApplicationContextConfiguration {
@Autowired Environment env;
@Bean
public PlatformTransactionManager txManager() throws PropertyVetoException {
JpaTransactionManager bean = new JpaTransactionManager();
bean.setEntityManagerFactory(entityManagerFactory());
return bean;
}
@Bean
public CachingConfiguration cachingConfiguration(){
return new CachingConfiguration();
}
@Bean
public EntityManagerFactory entityManagerFactory() throws PropertyVetoException {
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(dataSource());
emf.setPackagesToScan("com.tmm.web.domain","com.tmm.web.security");
emf.setPersistenceProvider(new HibernatePersistence());
emf.afterPropertiesSet();
emf.setJpaDialect(new HibernateJpaDialect());
return emf.getObject();
}
@Bean
public ComboPooledDataSource dataSource() throws PropertyVetoException{
ComboPooledDataSource bean = new ComboPooledDataSource();
bean.setDriverClass(env.getProperty("database.driverClassName"));
bean.setJdbcUrl(env.getProperty("database.url"));
bean.setUser(env.getProperty("database.username"));
bean.setPassword(env.getProperty("database.password"));
return bean;
}
}