JAVA-13088 Check whether spring-boot-multiple-datasources and spring-data-jdbc modules have duplicate code (#12495)
This commit is contained in:
parent
ab3a728721
commit
f56d707bf4
|
@ -51,7 +51,6 @@
|
|||
<module>spring-boot-libraries-2</module>
|
||||
<module>spring-boot-libraries-comparison</module>
|
||||
<module>spring-boot-logging-log4j2</module>
|
||||
<module>spring-boot-multiple-datasources</module>
|
||||
<module>spring-boot-mvc</module>
|
||||
<module>spring-boot-mvc-2</module>
|
||||
<module>spring-boot-mvc-3</module>
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
.local-db
|
|
@ -1,58 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-boot-multiple-datasources</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>spring-boot-multiple-datasources</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Module For Spring Boot With Multiple Datasources</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
<scope>runtime</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<spring-boot.version>2.6.3</spring-boot.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,13 +0,0 @@
|
|||
package com.baeldung.spring.datasources;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class MultipleDatasourcesApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MultipleDatasourcesApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,48 +0,0 @@
|
|||
package com.baeldung.spring.datasources.todos;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Todo {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
private String title;
|
||||
private boolean completed;
|
||||
|
||||
public Todo() {
|
||||
}
|
||||
|
||||
public Todo(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public boolean isCompleted() {
|
||||
return completed;
|
||||
}
|
||||
|
||||
public void setCompleted(boolean completed) {
|
||||
this.completed = completed;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
package com.baeldung.spring.datasources.todos;
|
||||
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
|
||||
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 javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
public class TodoDatasourceConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties("spring.datasource.todos")
|
||||
public DataSourceProperties todosDataSourceProperties() {
|
||||
return new DataSourceProperties();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public DataSource todosDataSource() {
|
||||
return todosDataSourceProperties()
|
||||
.initializeDataSourceBuilder()
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
package com.baeldung.spring.datasources.todos;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Objects;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@EnableJpaRepositories(
|
||||
basePackageClasses = Todo.class,
|
||||
entityManagerFactoryRef = "todosEntityManagerFactory",
|
||||
transactionManagerRef = "todosTransactionManager"
|
||||
)
|
||||
public class TodoJpaConfiguration {
|
||||
|
||||
@Bean
|
||||
public LocalContainerEntityManagerFactoryBean todosEntityManagerFactory(
|
||||
@Qualifier("todosDataSource") DataSource dataSource,
|
||||
EntityManagerFactoryBuilder builder) {
|
||||
return builder
|
||||
.dataSource(dataSource)
|
||||
.packages(Todo.class)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlatformTransactionManager todosTransactionManager(
|
||||
@Qualifier("todosEntityManagerFactory") LocalContainerEntityManagerFactoryBean todosEntityManagerFactory) {
|
||||
return new JpaTransactionManager(Objects.requireNonNull(todosEntityManagerFactory.getObject()));
|
||||
}
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
package com.baeldung.spring.datasources.todos;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface TodoRepository extends JpaRepository<Todo, Long> {
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
package com.baeldung.spring.datasources.topics;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Topic {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
private String title;
|
||||
|
||||
public Topic() {
|
||||
}
|
||||
|
||||
public Topic(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
package com.baeldung.spring.datasources.topics;
|
||||
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
public class TopicDatasourceConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties("spring.datasource.topics")
|
||||
public DataSourceProperties topicsDataSourceProperties() {
|
||||
return new DataSourceProperties();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource topicsDataSource() {
|
||||
return topicsDataSourceProperties()
|
||||
.initializeDataSourceBuilder()
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
package com.baeldung.spring.datasources.topics;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Objects;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@EnableJpaRepositories(
|
||||
basePackageClasses = Topic.class,
|
||||
entityManagerFactoryRef = "topicsEntityManagerFactory",
|
||||
transactionManagerRef = "topicsTransactionManager"
|
||||
)
|
||||
public class TopicJpaConfiguration {
|
||||
|
||||
@Bean
|
||||
public LocalContainerEntityManagerFactoryBean topicsEntityManagerFactory(
|
||||
@Qualifier("topicsDataSource") DataSource dataSource,
|
||||
EntityManagerFactoryBuilder builder
|
||||
) {
|
||||
return builder
|
||||
.dataSource(dataSource)
|
||||
.packages(Topic.class)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlatformTransactionManager topicsTransactionManager(
|
||||
@Qualifier("topicsEntityManagerFactory") LocalContainerEntityManagerFactoryBean topicsEntityManagerFactory) {
|
||||
return new JpaTransactionManager(Objects.requireNonNull(topicsEntityManagerFactory.getObject()));
|
||||
}
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
package com.baeldung.spring.datasources.topics;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface TopicRepository extends JpaRepository<Topic, Long> {
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
spring:
|
||||
datasource:
|
||||
todos:
|
||||
url: jdbc:h2:./.local-db/todos;DB_CLOSE_DELAY=-1;MODE=DB2;AUTO_SERVER=TRUE
|
||||
username: sa
|
||||
password: null
|
||||
driverClassName: org.h2.Driver
|
||||
topics:
|
||||
url: jdbc:h2:./.local-db/topics;DB_CLOSE_DELAY=-1;MODE=DB2;AUTO_SERVER=TRUE
|
||||
username: sa
|
||||
password: null
|
||||
driverClassName: org.h2.Driver
|
||||
h2:
|
||||
console:
|
||||
enabled: true
|
||||
path: /h2-console
|
||||
jpa:
|
||||
generate-ddl: true
|
||||
hibernate:
|
||||
ddl-auto: update
|
||||
properties:
|
||||
hibernate:
|
||||
dialect: org.hibernate.dialect.H2Dialect
|
|
@ -1,39 +0,0 @@
|
|||
package com.baeldung.spring.datasources;
|
||||
|
||||
import com.baeldung.spring.datasources.todos.Todo;
|
||||
import com.baeldung.spring.datasources.todos.TodoRepository;
|
||||
import com.baeldung.spring.datasources.topics.Topic;
|
||||
import com.baeldung.spring.datasources.topics.TopicRepository;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DataJpaTest // no test database!
|
||||
class MultipleDatasourcesIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
TodoRepository todoRepo;
|
||||
@Autowired
|
||||
TopicRepository topicRepo;
|
||||
|
||||
@Test
|
||||
void shouldSaveTodoToTodoDB() {
|
||||
Todo todo = new Todo("test");
|
||||
Todo saved =todoRepo.save(todo);
|
||||
Optional<Todo> result= todoRepo.findById(saved.getId());
|
||||
assertThat(result).isPresent();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSaveTopicToTopicDB() {
|
||||
Topic todo = new Topic("test");
|
||||
Topic saved =topicRepo.save(todo);
|
||||
Optional<Topic> result= topicRepo.findById(saved.getId());
|
||||
assertThat(result).isPresent();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue