Skip to content

Commit fb63897

Browse files
author
Rocard
committed
* created standard spring-boot project structure
* add datasource config for dbOne and dbTwo
1 parent c555371 commit fb63897

File tree

14 files changed

+228
-3
lines changed

14 files changed

+228
-3
lines changed

Dockerfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM maven:3.6.3-jdk-11 AS builder
2+
WORKDIR /workdir/server
3+
COPY pom.xml /workdir/server/pom.xml
4+
RUN mvn dependency:go-offline
5+
6+
COPY src /workdir/server/src
7+
RUN mvn install
8+
RUN mkdir -p target/dependency
9+
WORKDIR /workdir/server/target/dependency
10+
RUN jar -xf ../*.jar
11+
12+
FROM openjdk:11-jre-slim
13+
14+
VOLUME /tmp
15+
ARG DEPENDENCY=/workdir/server/target/dependency
16+
COPY --from=builder ${DEPENDENCY}/BOOT-INF/lib /app/lib
17+
COPY --from=builder ${DEPENDENCY}/META-INF /app/META-INF
18+
COPY --from=builder ${DEPENDENCY}/BOOT-INF/classes /app
19+
ENTRYPOINT ["java","-cp","app:app/lib/*","com.roc41d.springbootmultipledatasources.Application"]
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55

66
@SpringBootApplication
7-
public class SpringBootMultipleDatasourcesApplication {
7+
public class Application {
88

99
public static void main(String[] args) {
10-
SpringApplication.run(SpringBootMultipleDatasourcesApplication.class, args);
10+
SpringApplication.run(Application.class, args);
1111
}
1212

1313
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.roc41d.springbootmultipledatasources.config;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.Primary;
7+
import org.springframework.core.env.Environment;
8+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
9+
import org.springframework.jdbc.datasource.DriverManagerDataSource;
10+
import org.springframework.orm.jpa.JpaTransactionManager;
11+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
12+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
13+
import org.springframework.transaction.PlatformTransactionManager;
14+
15+
import javax.sql.DataSource;
16+
import java.util.HashMap;
17+
import java.util.Objects;
18+
19+
@Configuration
20+
@EnableJpaRepositories(
21+
basePackages = "com.roc41d.springbootmultipledatasources.model.dbOne",
22+
entityManagerFactoryRef = "primaryEntityManager",
23+
transactionManagerRef = "primaryTransactionManager"
24+
)
25+
public class DBOneConfig {
26+
27+
@Autowired
28+
private Environment env;
29+
30+
@Bean
31+
@Primary
32+
public LocalContainerEntityManagerFactoryBean primaryEntityManager() {
33+
LocalContainerEntityManagerFactoryBean em
34+
= new LocalContainerEntityManagerFactoryBean();
35+
em.setDataSource(primaryDataSource());
36+
em.setPackagesToScan(
37+
new String[] { "com.roc41d.springbootmultipledatasources.model.dbOne.dal" });
38+
39+
HibernateJpaVendorAdapter vendorAdapter
40+
= new HibernateJpaVendorAdapter();
41+
em.setJpaVendorAdapter(vendorAdapter);
42+
HashMap<String, Object> properties = new HashMap<>();
43+
properties.put("hibernate.hbm2ddl.auto",
44+
env.getProperty("spring.jpa.hibernate.ddl-auto"));
45+
properties.put("hibernate.dialect",
46+
env.getProperty("spring.jpa.properties.hibernate.dialect"));
47+
em.setJpaPropertyMap(properties);
48+
49+
return em;
50+
}
51+
52+
@Primary
53+
@Bean
54+
public DataSource primaryDataSource() {
55+
56+
DriverManagerDataSource dataSource
57+
= new DriverManagerDataSource();
58+
dataSource.setDriverClassName(
59+
Objects.requireNonNull(env.getProperty("spring.dbone.driverClassName")));
60+
dataSource.setUrl(env.getProperty("spring.dbone.url"));
61+
dataSource.setUsername(env.getProperty("spring.dbone.username"));
62+
dataSource.setPassword(env.getProperty("spring.dbone.password"));
63+
64+
return dataSource;
65+
}
66+
67+
@Primary
68+
@Bean
69+
public PlatformTransactionManager primaryTransactionManager() {
70+
71+
JpaTransactionManager transactionManager
72+
= new JpaTransactionManager();
73+
transactionManager.setEntityManagerFactory(
74+
primaryEntityManager().getObject());
75+
return transactionManager;
76+
}
77+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.roc41d.springbootmultipledatasources.config;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.core.env.Environment;
7+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
8+
import org.springframework.jdbc.datasource.DriverManagerDataSource;
9+
import org.springframework.orm.jpa.JpaTransactionManager;
10+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
11+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
12+
import org.springframework.transaction.PlatformTransactionManager;
13+
14+
import javax.sql.DataSource;
15+
import java.util.HashMap;
16+
import java.util.Objects;
17+
18+
@Configuration
19+
@EnableJpaRepositories(
20+
basePackages = "com.roc41d.springbootmultipledatasources.model.dbTwo",
21+
entityManagerFactoryRef = "secondaryEntityManager",
22+
transactionManagerRef = "secondaryTransactionManager"
23+
)
24+
public class DBTwoConfig {
25+
26+
@Autowired
27+
private Environment env;
28+
29+
@Bean
30+
public LocalContainerEntityManagerFactoryBean secondaryEntityManager() {
31+
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
32+
em.setDataSource(secondaryDataSource());
33+
em.setPackagesToScan(new String[] { "com.roc41d.springbootmultipledatasources.model.dbTwo.dal" });
34+
35+
HibernateJpaVendorAdapter vendorAdapter
36+
= new HibernateJpaVendorAdapter();
37+
em.setJpaVendorAdapter(vendorAdapter);
38+
HashMap<String, Object> properties = new HashMap<>();
39+
properties.put("hibernate.hbm2ddl.auto",
40+
env.getProperty("spring.jpa.hibernate.ddl-auto"));
41+
properties.put("hibernate.dialect",
42+
env.getProperty("spring.jpa.properties.hibernate.dialect"));
43+
em.setJpaPropertyMap(properties);
44+
45+
return em;
46+
}
47+
48+
@Bean
49+
public DataSource secondaryDataSource() {
50+
51+
DriverManagerDataSource dataSource
52+
= new DriverManagerDataSource();
53+
dataSource.setDriverClassName(
54+
Objects.requireNonNull(env.getProperty("spring.dbtwo.driverClassName")));
55+
dataSource.setUrl(env.getProperty("spring.dbtwo.url"));
56+
dataSource.setUsername(env.getProperty("spring.dbtwo.username"));
57+
dataSource.setPassword(env.getProperty("spring.dbtwo.password"));
58+
59+
return dataSource;
60+
}
61+
62+
@Bean
63+
public PlatformTransactionManager secondaryTransactionManager() {
64+
65+
JpaTransactionManager transactionManager
66+
= new JpaTransactionManager();
67+
transactionManager.setEntityManagerFactory(
68+
secondaryEntityManager().getObject());
69+
return transactionManager;
70+
}
71+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.roc41d.springbootmultipledatasources.model.dbOne.dal;
2+
3+
public class UserOne {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.roc41d.springbootmultipledatasources.model.dbOne.repository;
2+
3+
public interface UserOneRepo {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.roc41d.springbootmultipledatasources.model.dbTwo.dal;
2+
3+
public class UserTwo {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.roc41d.springbootmultipledatasources.model.dbTwo.repository;
2+
3+
public interface UserTwoRepo {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.roc41d.springbootmultipledatasources.service.contract;
2+
3+
public interface IUserOneContract {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.roc41d.springbootmultipledatasources.service.contract;
2+
3+
public interface IUserTwoContract {
4+
}

0 commit comments

Comments
 (0)