From f1d4024a596e5e048e67a8068ead53908ea78180 Mon Sep 17 00:00:00 2001 From: hemant Date: Wed, 30 May 2018 20:56:16 +0530 Subject: [PATCH 1/5] Added springbootnonwebapp project code --- .../springbootnonwebapp/HelloController.java | 20 ++++++++++++++++ .../baeldung/springbootnonwebapp/Runner.java | 23 +++++++++++++++++++ .../SpringBootNonWebappApplication.java | 21 +++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 spring-boot/src/main/java/com/baeldung/springbootnonwebapp/HelloController.java create mode 100644 spring-boot/src/main/java/com/baeldung/springbootnonwebapp/Runner.java create mode 100644 spring-boot/src/main/java/com/baeldung/springbootnonwebapp/SpringBootNonWebappApplication.java diff --git a/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/HelloController.java b/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/HelloController.java new file mode 100644 index 0000000000..bec02a7b0c --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/HelloController.java @@ -0,0 +1,20 @@ +package com.baeldung.springbootnonwebapp; + +import java.time.LocalDate; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * Controller exposing rest web services + * @author hemant + * + */ +@RestController +public class HelloController { + + @RequestMapping("/") + public LocalDate getMinLocalDate() { + return LocalDate.MIN; + } +} diff --git a/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/Runner.java b/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/Runner.java new file mode 100644 index 0000000000..7eca1c0abe --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/Runner.java @@ -0,0 +1,23 @@ +package com.baeldung.springbootnonwebapp; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +@Component +public class Runner implements CommandLineRunner { + + private static final Logger LOG = LoggerFactory.getLogger(Runner.class); + + /** + * This method will be executed after the application context is loaded and + * right before the Spring Application main method is completed. + */ + @Override + public void run(String... args) throws Exception { + LOG.info("START : command line runner"); + LOG.info("EXECUTING : command line runner"); + LOG.info("END : command line runner"); + } +} diff --git a/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/SpringBootNonWebappApplication.java b/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/SpringBootNonWebappApplication.java new file mode 100644 index 0000000000..de9d1ebd9e --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/SpringBootNonWebappApplication.java @@ -0,0 +1,21 @@ +package com.baeldung.springbootnonwebapp; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootNonWebappApplication { + + private static final Logger LOG = LoggerFactory.getLogger(SpringBootNonWebappApplication.class); + + public static void main(String[] args) { + LOG.info("STARTING THE APPLICATION"); + SpringApplication app = new SpringApplication(SpringBootNonWebappApplication.class); + // This line of code, disables the web app setting + app.setWebEnvironment(false); + app.run(args); + LOG.info("APPLICATION STARTED"); + } +} From 98d37d0f8c8866bd10d6692b5e1d1c48b6a67ecb Mon Sep 17 00:00:00 2001 From: hemantvsn Date: Mon, 11 Jun 2018 23:41:00 +0530 Subject: [PATCH 2/5] Removed the controller Based on the editor comments in the post. --- .../springbootnonwebapp/HelloController.java | 20 ------------------- 1 file changed, 20 deletions(-) delete mode 100644 spring-boot/src/main/java/com/baeldung/springbootnonwebapp/HelloController.java diff --git a/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/HelloController.java b/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/HelloController.java deleted file mode 100644 index bec02a7b0c..0000000000 --- a/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/HelloController.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.springbootnonwebapp; - -import java.time.LocalDate; - -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -/** - * Controller exposing rest web services - * @author hemant - * - */ -@RestController -public class HelloController { - - @RequestMapping("/") - public LocalDate getMinLocalDate() { - return LocalDate.MIN; - } -} From 400f86a65799b6a0f313200434ba06a18110825d Mon Sep 17 00:00:00 2001 From: hemant Date: Sat, 16 Jun 2018 01:01:14 +0530 Subject: [PATCH 3/5] Removed 2 classes for main and other for Runner. Replaced them with a single class serving both the purposes --- .../baeldung/springbootnonwebapp/Runner.java | 23 ------------ .../SpringBootConsoleApplication.java | 37 +++++++++++++++++++ .../SpringBootNonWebappApplication.java | 21 ----------- 3 files changed, 37 insertions(+), 44 deletions(-) delete mode 100644 spring-boot/src/main/java/com/baeldung/springbootnonwebapp/Runner.java create mode 100644 spring-boot/src/main/java/com/baeldung/springbootnonwebapp/SpringBootConsoleApplication.java delete mode 100644 spring-boot/src/main/java/com/baeldung/springbootnonwebapp/SpringBootNonWebappApplication.java diff --git a/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/Runner.java b/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/Runner.java deleted file mode 100644 index 7eca1c0abe..0000000000 --- a/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/Runner.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.springbootnonwebapp; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.boot.CommandLineRunner; -import org.springframework.stereotype.Component; - -@Component -public class Runner implements CommandLineRunner { - - private static final Logger LOG = LoggerFactory.getLogger(Runner.class); - - /** - * This method will be executed after the application context is loaded and - * right before the Spring Application main method is completed. - */ - @Override - public void run(String... args) throws Exception { - LOG.info("START : command line runner"); - LOG.info("EXECUTING : command line runner"); - LOG.info("END : command line runner"); - } -} diff --git a/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/SpringBootConsoleApplication.java b/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/SpringBootConsoleApplication.java new file mode 100644 index 0000000000..6c3fbaff45 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/SpringBootConsoleApplication.java @@ -0,0 +1,37 @@ +package com.baeldung.springbootnonwebapp; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * 1. Act as main class for spring boot application + * 2. Also implements CommandLineRunner, so that code within run method + * is executed before application startup but after all beans are effectively created + * @author hemant + * + */ +@SpringBootApplication +public class SpringBootConsoleApplication implements CommandLineRunner { + + private static final Logger LOG = LoggerFactory.getLogger(SpringBootConsoleApplication.class); + + public static void main(String[] args) { + LOG.info("STARTING THE APPLICATION"); + SpringApplication.run(SpringBootConsoleApplication.class, args); + LOG.info("APPLICATION STARTED"); + } + + /** + * This method will be executed after the application context is loaded and + * right before the Spring Application main method is completed. + */ + @Override + public void run(String... args) throws Exception { + LOG.info("START : command line runner"); + LOG.info("EXECUTING : command line runner"); + LOG.info("END : command line runner"); + } +} diff --git a/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/SpringBootNonWebappApplication.java b/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/SpringBootNonWebappApplication.java deleted file mode 100644 index de9d1ebd9e..0000000000 --- a/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/SpringBootNonWebappApplication.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.springbootnonwebapp; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class SpringBootNonWebappApplication { - - private static final Logger LOG = LoggerFactory.getLogger(SpringBootNonWebappApplication.class); - - public static void main(String[] args) { - LOG.info("STARTING THE APPLICATION"); - SpringApplication app = new SpringApplication(SpringBootNonWebappApplication.class); - // This line of code, disables the web app setting - app.setWebEnvironment(false); - app.run(args); - LOG.info("APPLICATION STARTED"); - } -} From 9241c90660fd2b425ca32c51f2605a2f585850fa Mon Sep 17 00:00:00 2001 From: hemantvsn Date: Fri, 22 Jun 2018 12:24:19 +0530 Subject: [PATCH 4/5] Update SpringBootConsoleApplication.java --- .../springbootnonwebapp/SpringBootConsoleApplication.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/SpringBootConsoleApplication.java b/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/SpringBootConsoleApplication.java index 6c3fbaff45..5b0fda992d 100644 --- a/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/SpringBootConsoleApplication.java +++ b/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/SpringBootConsoleApplication.java @@ -16,12 +16,12 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootConsoleApplication implements CommandLineRunner { - private static final Logger LOG = LoggerFactory.getLogger(SpringBootConsoleApplication.class); + private static Logger LOG = LoggerFactory.getLogger(SpringBootConsoleApplication.class); public static void main(String[] args) { LOG.info("STARTING THE APPLICATION"); SpringApplication.run(SpringBootConsoleApplication.class, args); - LOG.info("APPLICATION STARTED"); + LOG.info("APPLICATION FINISHED"); } /** @@ -30,8 +30,6 @@ public class SpringBootConsoleApplication implements CommandLineRunner { */ @Override public void run(String... args) throws Exception { - LOG.info("START : command line runner"); LOG.info("EXECUTING : command line runner"); - LOG.info("END : command line runner"); } } From 8a960353db912cb743a7cef04c2ffbe78954d0d1 Mon Sep 17 00:00:00 2001 From: hemantvsn Date: Mon, 25 Jun 2018 14:27:30 +0530 Subject: [PATCH 5/5] Logged the arguements of run method --- .../springbootnonwebapp/SpringBootConsoleApplication.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/SpringBootConsoleApplication.java b/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/SpringBootConsoleApplication.java index 5b0fda992d..9faa463378 100644 --- a/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/SpringBootConsoleApplication.java +++ b/spring-boot/src/main/java/com/baeldung/springbootnonwebapp/SpringBootConsoleApplication.java @@ -31,5 +31,8 @@ public class SpringBootConsoleApplication implements CommandLineRunner { @Override public void run(String... args) throws Exception { LOG.info("EXECUTING : command line runner"); + for (int i = 0; i < args.length; ++i) { + LOG.info("args[{}]: {}", i, args[i]); + } } }