DataSourceConfig.java
3.16 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.sigem.gis.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
import java.util.HashMap;
@Configuration
public class DataSourceConfig {
// --- DataSource 1: Master (.254) ---
@Bean
@Primary
@ConfigurationProperties("spring.datasource.master")
public org.springframework.boot.autoconfigure.jdbc.DataSourceProperties masterDataSourceProperties() {
return new org.springframework.boot.autoconfigure.jdbc.DataSourceProperties();
}
@Bean(name = "masterDataSource")
@Primary
public DataSource masterDataSource() {
return masterDataSourceProperties().initializeDataSourceBuilder().build();
}
// --- DataSource 2: GIS (.123) ---
@Bean
@ConfigurationProperties("spring.datasource.gis")
public org.springframework.boot.autoconfigure.jdbc.DataSourceProperties gisDataSourceProperties() {
return new org.springframework.boot.autoconfigure.jdbc.DataSourceProperties();
}
@Bean(name = "gisDataSource")
public DataSource gisDataSource() {
return gisDataSourceProperties().initializeDataSourceBuilder().build();
}
// --- JdbcTemplates ---
@Primary
@Bean(name = "masterJdbcTemplate")
public JdbcTemplate masterJdbcTemplate(@Qualifier("masterDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "gisJdbcTemplate")
public JdbcTemplate gisJdbcTemplate(@Qualifier("gisDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
// --- Configuración Explícita de JPA ---
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
@Qualifier("masterDataSource") DataSource dataSource) {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource);
em.setPackagesToScan("com.sigem.gis");
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
em.setJpaPropertyMap(properties);
return em;
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") LocalContainerEntityManagerFactoryBean entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory.getObject());
}
}