-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApplicationConfig.kt
57 lines (49 loc) · 1.92 KB
/
ApplicationConfig.kt
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
package com.gp.service
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource
import org.springframework.context.annotation.*
import org.springframework.orm.hibernate5.HibernateTransactionManager
import org.springframework.orm.hibernate5.LocalSessionFactoryBean
import org.springframework.transaction.annotation.EnableTransactionManagement
import java.util.*
import javax.sql.DataSource
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.core.env.Environment
@Configuration
@EnableTransactionManagement
@PropertySource("classpath:db.properties")
@ComponentScans(
value = [
ComponentScan("com.metricstream.grc")
]
)
class ApplicationConfig {
@Autowired
lateinit var env: Environment
@Bean
fun getDataSource(): DataSource {
val dataSource = BasicDataSource()
dataSource.driverClassName = env!!.getProperty("db.driver")
dataSource.url = env!!.getProperty("db.url")
dataSource.username = env!!.getProperty("db.username")
dataSource.password = env!!.getProperty("db.password")
return dataSource
}
@Bean
fun getSessionFactory(): LocalSessionFactoryBean {
val factoryBean = LocalSessionFactoryBean()
factoryBean.setDataSource(getDataSource())
factoryBean.setPackagesToScan("com.metricstream.grc.service.dao")
val props = Properties()
props["hibernate.show_sql"] = env!!.getProperty("hibernate.show_sql")
props["hibernate.hbm2ddl.auto"] = env!!.getProperty("hibernate.hbm2ddl.auto")
props["hibernate.dialect"] = env!!.getProperty("hibernate.dialect")
factoryBean.hibernateProperties = props
return factoryBean
}
@Bean
fun getTransactionManager(): HibernateTransactionManager {
val transactionManager = HibernateTransactionManager()
transactionManager.sessionFactory = getSessionFactory().getObject()
return transactionManager
}
}