From 333dad149fcaec6fe36f64d0375d4b00f9029828 Mon Sep 17 00:00:00 2001 From: marcodenisi Date: Thu, 18 Jun 2020 17:49:10 +0200 Subject: [PATCH 1/4] BAEL-4083 - sample spring boot app with xml config and properties --- spring-boot-modules/pom.xml | 1 + spring-boot-modules/spring-boot-xml/pom.xml | 32 +++++++++++++++++++ .../java/com/baeldung/springbootxml/Pojo.java | 17 ++++++++++ .../SpringBootXmlApplication.java | 25 +++++++++++++++ .../SpringBootXmlWithConfigApplication.java | 23 +++++++++++++ .../src/main/resources/application.properties | 1 + .../src/main/resources/beans.xml | 13 ++++++++ .../src/main/resources/beansconf.xml | 15 +++++++++ 8 files changed, 127 insertions(+) create mode 100644 spring-boot-modules/spring-boot-xml/pom.xml create mode 100644 spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/Pojo.java create mode 100644 spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlApplication.java create mode 100644 spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlWithConfigApplication.java create mode 100644 spring-boot-modules/spring-boot-xml/src/main/resources/application.properties create mode 100644 spring-boot-modules/spring-boot-xml/src/main/resources/beans.xml create mode 100644 spring-boot-modules/spring-boot-xml/src/main/resources/beansconf.xml diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index 7992c0ce12..8ef3d34056 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -57,6 +57,7 @@ spring-boot-springdoc spring-boot-testing spring-boot-vue + spring-boot-xml diff --git a/spring-boot-modules/spring-boot-xml/pom.xml b/spring-boot-modules/spring-boot-xml/pom.xml new file mode 100644 index 0000000000..dd575ff414 --- /dev/null +++ b/spring-boot-modules/spring-boot-xml/pom.xml @@ -0,0 +1,32 @@ + + + + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + 4.0.0 + + spring-boot-xml + + + + org.springframework.boot + spring-boot-starter + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/Pojo.java b/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/Pojo.java new file mode 100644 index 0000000000..8c8b47ed40 --- /dev/null +++ b/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/Pojo.java @@ -0,0 +1,17 @@ +package com.baeldung.springbootxml; + +public class Pojo { + + private String field; + + public Pojo() { + } + + public String getField() { + return field; + } + + public void setField(String field) { + this.field = field; + } +} diff --git a/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlApplication.java b/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlApplication.java new file mode 100644 index 0000000000..1a0350fd26 --- /dev/null +++ b/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlApplication.java @@ -0,0 +1,25 @@ +package com.baeldung.springbootxml; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; + +@Configuration +@EnableAutoConfiguration +@ImportResource("classpath:beans.xml") +public class SpringBootXmlApplication implements CommandLineRunner { + + @Autowired private Pojo pojo; + + public static void main(String[] args) { + SpringApplication.run(SpringBootXmlApplication.class, args); + } + + public void run(String... args) { + System.out.println(pojo.getField()); + } + +} diff --git a/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlWithConfigApplication.java b/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlWithConfigApplication.java new file mode 100644 index 0000000000..7c36ac7905 --- /dev/null +++ b/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlWithConfigApplication.java @@ -0,0 +1,23 @@ +package com.baeldung.springbootxml; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; + +@Configuration +@ImportResource("classpath:beansconf.xml") +public class SpringBootXmlWithConfigApplication implements CommandLineRunner { + + @Autowired private Pojo pojo; + + public static void main(String[] args) { + SpringApplication.run(SpringBootXmlWithConfigApplication.class, args); + } + + @Override + public void run(String... args) throws Exception { + System.out.println(pojo.getField()); + } +} diff --git a/spring-boot-modules/spring-boot-xml/src/main/resources/application.properties b/spring-boot-modules/spring-boot-xml/src/main/resources/application.properties new file mode 100644 index 0000000000..ab9de92c82 --- /dev/null +++ b/spring-boot-modules/spring-boot-xml/src/main/resources/application.properties @@ -0,0 +1 @@ +sample=string loaded from properties! \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-xml/src/main/resources/beans.xml b/spring-boot-modules/spring-boot-xml/src/main/resources/beans.xml new file mode 100644 index 0000000000..9dac0d9e65 --- /dev/null +++ b/spring-boot-modules/spring-boot-xml/src/main/resources/beans.xml @@ -0,0 +1,13 @@ + + + + + + + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-xml/src/main/resources/beansconf.xml b/spring-boot-modules/spring-boot-xml/src/main/resources/beansconf.xml new file mode 100644 index 0000000000..7f328370a8 --- /dev/null +++ b/spring-boot-modules/spring-boot-xml/src/main/resources/beansconf.xml @@ -0,0 +1,15 @@ + + + + + + + + + \ No newline at end of file From 3cb402ced71073a70875cc770e2abc47b0e9ef5e Mon Sep 17 00:00:00 2001 From: marcodenisi Date: Thu, 18 Jun 2020 17:58:25 +0200 Subject: [PATCH 2/4] BAEL-4083 - removed sample with xml context-config --- .../SpringBootXmlApplication.java | 6 ++--- .../SpringBootXmlWithConfigApplication.java | 23 ------------------- .../src/main/resources/beansconf.xml | 15 ------------ 3 files changed, 2 insertions(+), 42 deletions(-) delete mode 100644 spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlWithConfigApplication.java delete mode 100644 spring-boot-modules/spring-boot-xml/src/main/resources/beansconf.xml diff --git a/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlApplication.java b/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlApplication.java index 1a0350fd26..bd75a95d7d 100644 --- a/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlApplication.java +++ b/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlApplication.java @@ -3,12 +3,10 @@ package com.baeldung.springbootxml; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.context.annotation.Configuration; +import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; -@Configuration -@EnableAutoConfiguration +@SpringBootApplication @ImportResource("classpath:beans.xml") public class SpringBootXmlApplication implements CommandLineRunner { diff --git a/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlWithConfigApplication.java b/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlWithConfigApplication.java deleted file mode 100644 index 7c36ac7905..0000000000 --- a/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlWithConfigApplication.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.springbootxml; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.CommandLineRunner; -import org.springframework.boot.SpringApplication; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.ImportResource; - -@Configuration -@ImportResource("classpath:beansconf.xml") -public class SpringBootXmlWithConfigApplication implements CommandLineRunner { - - @Autowired private Pojo pojo; - - public static void main(String[] args) { - SpringApplication.run(SpringBootXmlWithConfigApplication.class, args); - } - - @Override - public void run(String... args) throws Exception { - System.out.println(pojo.getField()); - } -} diff --git a/spring-boot-modules/spring-boot-xml/src/main/resources/beansconf.xml b/spring-boot-modules/spring-boot-xml/src/main/resources/beansconf.xml deleted file mode 100644 index 7f328370a8..0000000000 --- a/spring-boot-modules/spring-boot-xml/src/main/resources/beansconf.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - \ No newline at end of file From eaf59355892d58c92d04354d4a77c28d91b45a9f Mon Sep 17 00:00:00 2001 From: marcodenisi Date: Thu, 18 Jun 2020 22:41:36 +0200 Subject: [PATCH 3/4] add slfj logger --- .../springbootxml/SpringBootXmlApplication.java | 12 +++++++++--- .../spring-boot-xml/src/main/resources/beans.xml | 12 +++++------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlApplication.java b/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlApplication.java index bd75a95d7d..addf24c427 100644 --- a/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlApplication.java +++ b/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlApplication.java @@ -1,15 +1,21 @@ package com.baeldung.springbootxml; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; -@SpringBootApplication +@Configuration +@EnableAutoConfiguration @ImportResource("classpath:beans.xml") public class SpringBootXmlApplication implements CommandLineRunner { + private static final Logger logger = LoggerFactory.getLogger(SpringBootXmlApplication.class); + @Autowired private Pojo pojo; public static void main(String[] args) { @@ -17,7 +23,7 @@ public class SpringBootXmlApplication implements CommandLineRunner { } public void run(String... args) { - System.out.println(pojo.getField()); + logger.info(pojo.getField()); } } diff --git a/spring-boot-modules/spring-boot-xml/src/main/resources/beans.xml b/spring-boot-modules/spring-boot-xml/src/main/resources/beans.xml index 9dac0d9e65..200afecbc9 100644 --- a/spring-boot-modules/spring-boot-xml/src/main/resources/beans.xml +++ b/spring-boot-modules/spring-boot-xml/src/main/resources/beans.xml @@ -1,11 +1,9 @@ - + From c5f8489f1d347ea83939cba014dc3f857810a5b1 Mon Sep 17 00:00:00 2001 From: Marco Denisi Date: Sun, 28 Jun 2020 10:15:48 +0200 Subject: [PATCH 4/4] BAEL-4083 - add app integration test --- spring-boot-modules/spring-boot-xml/pom.xml | 8 ++++++ ...ringBootXmlApplicationIntegrationTest.java | 26 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlApplicationIntegrationTest.java diff --git a/spring-boot-modules/spring-boot-xml/pom.xml b/spring-boot-modules/spring-boot-xml/pom.xml index dd575ff414..e1ddd8f437 100644 --- a/spring-boot-modules/spring-boot-xml/pom.xml +++ b/spring-boot-modules/spring-boot-xml/pom.xml @@ -18,6 +18,14 @@ org.springframework.boot spring-boot-starter + + org.springframework.boot + spring-boot-starter-test + + + junit + junit + diff --git a/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlApplicationIntegrationTest.java b/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlApplicationIntegrationTest.java new file mode 100644 index 0000000000..2c3993d0d8 --- /dev/null +++ b/spring-boot-modules/spring-boot-xml/src/main/java/com/baeldung/springbootxml/SpringBootXmlApplicationIntegrationTest.java @@ -0,0 +1,26 @@ +package com.baeldung.springbootxml; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = SpringBootXmlApplication.class) +public class SpringBootXmlApplicationIntegrationTest { + + @Autowired private Pojo pojo; + @Value("${sample}") private String sample; + + @Test + public void whenCallingGetter_thenPrintingProperty() { + assertThat(pojo.getField()) + .isNotBlank() + .isEqualTo(sample); + } + +} \ No newline at end of file