From 1dea2b8cbda63df1ac57083e97847c80c66f1c26 Mon Sep 17 00:00:00 2001 From: micropatel <31759369+micropatel@users.noreply.github.com> Date: Thu, 20 Sep 2018 22:57:08 -0300 Subject: [PATCH] BAEL-1980 Access an in-memory h2 db from 2 Spring Boot applications (#5230) * Added Spring-Boot-H2 project * Optimized imports and formatted code * Code Formatting changes * Renamed the client applicaiton * Removed Name from pom.xml * formatting changes - applied formatting style * added souts * Update SpringBootApp.java * Update ClientSpringBootApp.java * Fixed Formatting issues * Fixed formatting and spelling mistakes * added H2 shared db example * a * patch * test * Revert "Fixed formatting and spelling mistakes" This reverts commit ed56ededec903e85c2d9f2b55e7730682455d5f5. * Revert "test" This reverts commit 4a7133dbd1d181cc304348f63998924a9db5acfe. * Revert "patch" This reverts commit 404b9ebe74aef4faa2ad62d9867652b62a5445b8. * Revert "a" This reverts commit af77f841114e19b0a0eafb15530dbaaaf34b5493. * Revert "added H2 shared db example" This reverts commit e16918cae3813fe20172c7e90e27bbec34b5fb5f. * Updated to have client and server in same directory * Deleted old files * removed unwanted directory * formating * removed unwanted module * commentted removed module * Revert "commentted removed module" This reverts commit 211ff1441f6bde920d7af3f8cd52c2a682ba3967. * Revert "removed unwanted module" This reverts commit 657f9ee45e02269bcfdbf380b9a74ff2370857fc. * fixed pom.xml issue * typo fixed * fixed multiple main class issue --- pom.xml | 4 +- .../spring-boot-h2-database/pom.xml | 2 + .../demo/client}/ClientSpringBootApp.java | 8 ++- .../h2db/demo/{ => server}/SpringBootApp.java | 9 +-- .../spring-boot-h2-remote-app/.gitignore | 25 --------- .../spring-boot-h2-remote-app/pom.xml | 55 ------------------- .../src/main/resources/application.properties | 5 -- 7 files changed, 15 insertions(+), 93 deletions(-) rename spring-boot-h2/{spring-boot-h2-remote-app/src/main/java/com/baeldung/h2db/demo => spring-boot-h2-database/src/main/java/com/baeldung/h2db/demo/client}/ClientSpringBootApp.java (85%) rename spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/demo/{ => server}/SpringBootApp.java (88%) delete mode 100644 spring-boot-h2/spring-boot-h2-remote-app/.gitignore delete mode 100644 spring-boot-h2/spring-boot-h2-remote-app/pom.xml delete mode 100644 spring-boot-h2/spring-boot-h2-remote-app/src/main/resources/application.properties diff --git a/pom.xml b/pom.xml index bd06bdfdaa..f4f09b427e 100644 --- a/pom.xml +++ b/pom.xml @@ -675,7 +675,7 @@ spring-boot-custom-starter/greeter spring-boot-h2/spring-boot-h2-database - spring-boot-h2/spring-boot-h2-remote-app + @@ -992,7 +992,7 @@ spring-boot-custom-starter/greeter spring-boot-h2/spring-boot-h2-database - spring-boot-h2/spring-boot-h2-remote-app + diff --git a/spring-boot-h2/spring-boot-h2-database/pom.xml b/spring-boot-h2/spring-boot-h2-database/pom.xml index 4b660334da..94f2293c34 100644 --- a/spring-boot-h2/spring-boot-h2-database/pom.xml +++ b/spring-boot-h2/spring-boot-h2-database/pom.xml @@ -22,6 +22,8 @@ UTF-8 UTF-8 1.8 + + com.mycorp.starter.HelloWorldApplication diff --git a/spring-boot-h2/spring-boot-h2-remote-app/src/main/java/com/baeldung/h2db/demo/ClientSpringBootApp.java b/spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/demo/client/ClientSpringBootApp.java similarity index 85% rename from spring-boot-h2/spring-boot-h2-remote-app/src/main/java/com/baeldung/h2db/demo/ClientSpringBootApp.java rename to spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/demo/client/ClientSpringBootApp.java index 39e52afd2c..7402312e1c 100644 --- a/spring-boot-h2/spring-boot-h2-remote-app/src/main/java/com/baeldung/h2db/demo/ClientSpringBootApp.java +++ b/spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/demo/client/ClientSpringBootApp.java @@ -1,4 +1,4 @@ -package com.baeldung.h2db.demo; +package com.baeldung.h2db.demo.client; import java.sql.ResultSet; import java.sql.SQLException; @@ -7,16 +7,20 @@ import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.PropertySource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; @SpringBootApplication +@ComponentScan("com.baeldung.h2db.demo.client") public class ClientSpringBootApp { @Autowired private JdbcTemplate jdbcTemplate; public static void main(String[] args) { + System.setProperty("spring.datasource.url","jdbc:h2:tcp://localhost:9091/mem:mydb"); SpringApplication.run(ClientSpringBootApp.class, args); } @@ -46,4 +50,4 @@ public class ClientSpringBootApp { } }); } -} \ No newline at end of file +} diff --git a/spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/demo/SpringBootApp.java b/spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/demo/server/SpringBootApp.java similarity index 88% rename from spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/demo/SpringBootApp.java rename to spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/demo/server/SpringBootApp.java index 1fe080ec22..e75b42a934 100644 --- a/spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/demo/SpringBootApp.java +++ b/spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/demo/server/SpringBootApp.java @@ -1,4 +1,4 @@ -package com.baeldung.h2db.demo; +package com.baeldung.h2db.demo.server; import java.sql.ResultSet; import java.sql.SQLException; @@ -9,10 +9,12 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; @SpringBootApplication +@ComponentScan("com.baeldung.h2db.demo.server") public class SpringBootApp { @Autowired @@ -24,8 +26,7 @@ public class SpringBootApp { @PostConstruct private void initDb() { - System.out.println(String.format( - "****** Creating table: %s, and Inserting test data ******", "Employees")); + System.out.println(String.format("****** Creating table: %s, and Inserting test data ******", "Employees")); String sqlStatements[] = { "drop table employees if exists", @@ -57,4 +58,4 @@ public class SpringBootApp { public Server inMemoryH2DatabaseServer() throws SQLException { return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9091"); } -} \ No newline at end of file +} diff --git a/spring-boot-h2/spring-boot-h2-remote-app/.gitignore b/spring-boot-h2/spring-boot-h2-remote-app/.gitignore deleted file mode 100644 index 82eca336e3..0000000000 --- a/spring-boot-h2/spring-boot-h2-remote-app/.gitignore +++ /dev/null @@ -1,25 +0,0 @@ -/target/ -!.mvn/wrapper/maven-wrapper.jar - -### STS ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache - -### IntelliJ IDEA ### -.idea -*.iws -*.iml -*.ipr - -### NetBeans ### -/nbproject/private/ -/build/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ \ No newline at end of file diff --git a/spring-boot-h2/spring-boot-h2-remote-app/pom.xml b/spring-boot-h2/spring-boot-h2-remote-app/pom.xml deleted file mode 100644 index 8eb59d2098..0000000000 --- a/spring-boot-h2/spring-boot-h2-remote-app/pom.xml +++ /dev/null @@ -1,55 +0,0 @@ - - - 4.0.0 - - com.baeldung.h2db - spring-boot-h2-remote-app - 0.0.1-SNAPSHOT - jar - - Demo Spring Boot applications that access H2 in memory database created - in another Spring Boot application - - - - org.springframework.boot - spring-boot-starter-parent - 2.0.4.RELEASE - - - - - UTF-8 - UTF-8 - 1.8 - - - - - org.springframework.boot - spring-boot-starter-data-jpa - - - - com.h2database - h2 - - - - org.springframework.boot - spring-boot-starter-test - test - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - diff --git a/spring-boot-h2/spring-boot-h2-remote-app/src/main/resources/application.properties b/spring-boot-h2/spring-boot-h2-remote-app/src/main/resources/application.properties deleted file mode 100644 index 6c3446f03a..0000000000 --- a/spring-boot-h2/spring-boot-h2-remote-app/src/main/resources/application.properties +++ /dev/null @@ -1,5 +0,0 @@ -spring.datasource.url=jdbc:h2:tcp://localhost:9091/mem:mydb -spring.datasource.driverClassName=org.h2.Driver -spring.datasource.username=sa -spring.datasource.password= -spring.jpa.hibernate.ddl-auto=create \ No newline at end of file