From f56d707bf41869c8a024267272c8926beef1b233 Mon Sep 17 00:00:00 2001 From: anuragkumawat Date: Fri, 15 Jul 2022 14:39:17 +0530 Subject: [PATCH] JAVA-13088 Check whether spring-boot-multiple-datasources and spring-data-jdbc modules have duplicate code (#12495) --- spring-boot-modules/pom.xml | 1 - .../.gitignore | 1 - .../spring-boot-multiple-datasources/pom.xml | 58 ------------------- .../MultipleDatasourcesApplication.java | 13 ----- .../spring/datasources/todos/Todo.java | 48 --------------- .../todos/TodoDatasourceConfiguration.java | 28 --------- .../todos/TodoJpaConfiguration.java | 40 ------------- .../datasources/todos/TodoRepository.java | 6 -- .../spring/datasources/topics/Topic.java | 39 ------------- .../topics/TopicDatasourceConfiguration.java | 26 --------- .../topics/TopicJpaConfiguration.java | 41 ------------- .../datasources/topics/TopicRepository.java | 6 -- .../src/main/resources/application.yml | 23 -------- .../MultipleDatasourcesIntegrationTest.java | 39 ------------- 14 files changed, 369 deletions(-) delete mode 100644 spring-boot-modules/spring-boot-multiple-datasources/.gitignore delete mode 100644 spring-boot-modules/spring-boot-multiple-datasources/pom.xml delete mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/MultipleDatasourcesApplication.java delete mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/Todo.java delete mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoDatasourceConfiguration.java delete mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoJpaConfiguration.java delete mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoRepository.java delete mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/Topic.java delete mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicDatasourceConfiguration.java delete mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicJpaConfiguration.java delete mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicRepository.java delete mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/main/resources/application.yml delete mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/test/java/com/baeldung/spring/datasources/MultipleDatasourcesIntegrationTest.java diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index 1fd6202376..8bd8068ab7 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -51,7 +51,6 @@ spring-boot-libraries-2 spring-boot-libraries-comparison spring-boot-logging-log4j2 - spring-boot-multiple-datasources spring-boot-mvc spring-boot-mvc-2 spring-boot-mvc-3 diff --git a/spring-boot-modules/spring-boot-multiple-datasources/.gitignore b/spring-boot-modules/spring-boot-multiple-datasources/.gitignore deleted file mode 100644 index 87a3fce287..0000000000 --- a/spring-boot-modules/spring-boot-multiple-datasources/.gitignore +++ /dev/null @@ -1 +0,0 @@ -.local-db diff --git a/spring-boot-modules/spring-boot-multiple-datasources/pom.xml b/spring-boot-modules/spring-boot-multiple-datasources/pom.xml deleted file mode 100644 index 9355de8a36..0000000000 --- a/spring-boot-modules/spring-boot-multiple-datasources/pom.xml +++ /dev/null @@ -1,58 +0,0 @@ - - - 4.0.0 - spring-boot-multiple-datasources - 0.1.0-SNAPSHOT - spring-boot-multiple-datasources - jar - Module For Spring Boot With Multiple Datasources - - - com.baeldung.spring-boot-modules - spring-boot-modules - 1.0.0-SNAPSHOT - ../pom.xml - - - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-data-jpa - - - com.h2database - h2 - provided - - - org.springframework.boot - spring-boot-devtools - runtime - true - - - org.springframework.boot - spring-boot-starter-test - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - - - 2.6.3 - - - \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/MultipleDatasourcesApplication.java b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/MultipleDatasourcesApplication.java deleted file mode 100644 index efdff387df..0000000000 --- a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/MultipleDatasourcesApplication.java +++ /dev/null @@ -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); - } - -} diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/Todo.java b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/Todo.java deleted file mode 100644 index 6f8557e258..0000000000 --- a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/Todo.java +++ /dev/null @@ -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; - } - -} diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoDatasourceConfiguration.java b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoDatasourceConfiguration.java deleted file mode 100644 index 3bfc8e2855..0000000000 --- a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoDatasourceConfiguration.java +++ /dev/null @@ -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(); - } - -} diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoJpaConfiguration.java b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoJpaConfiguration.java deleted file mode 100644 index 655a3a55c2..0000000000 --- a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoJpaConfiguration.java +++ /dev/null @@ -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())); - } -} diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoRepository.java b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoRepository.java deleted file mode 100644 index 09fb8c6500..0000000000 --- a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoRepository.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.baeldung.spring.datasources.todos; - -import org.springframework.data.jpa.repository.JpaRepository; - -public interface TodoRepository extends JpaRepository { -} diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/Topic.java b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/Topic.java deleted file mode 100644 index 1d1f20f111..0000000000 --- a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/Topic.java +++ /dev/null @@ -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; - } - -} diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicDatasourceConfiguration.java b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicDatasourceConfiguration.java deleted file mode 100644 index a06983d681..0000000000 --- a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicDatasourceConfiguration.java +++ /dev/null @@ -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(); - } - -} diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicJpaConfiguration.java b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicJpaConfiguration.java deleted file mode 100644 index d800813b8c..0000000000 --- a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicJpaConfiguration.java +++ /dev/null @@ -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())); - } -} diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicRepository.java b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicRepository.java deleted file mode 100644 index 499b72650a..0000000000 --- a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicRepository.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.baeldung.spring.datasources.topics; - -import org.springframework.data.jpa.repository.JpaRepository; - -public interface TopicRepository extends JpaRepository { -} diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/main/resources/application.yml b/spring-boot-modules/spring-boot-multiple-datasources/src/main/resources/application.yml deleted file mode 100644 index 4754acfc0f..0000000000 --- a/spring-boot-modules/spring-boot-multiple-datasources/src/main/resources/application.yml +++ /dev/null @@ -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 diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/test/java/com/baeldung/spring/datasources/MultipleDatasourcesIntegrationTest.java b/spring-boot-modules/spring-boot-multiple-datasources/src/test/java/com/baeldung/spring/datasources/MultipleDatasourcesIntegrationTest.java deleted file mode 100644 index 397992f6c8..0000000000 --- a/spring-boot-modules/spring-boot-multiple-datasources/src/test/java/com/baeldung/spring/datasources/MultipleDatasourcesIntegrationTest.java +++ /dev/null @@ -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 result= todoRepo.findById(saved.getId()); - assertThat(result).isPresent(); - } - - @Test - void shouldSaveTopicToTopicDB() { - Topic todo = new Topic("test"); - Topic saved =topicRepo.save(todo); - Optional result= topicRepo.findById(saved.getId()); - assertThat(result).isPresent(); - } - -}