From e0c1e3711ce76a4f9451b19f758ae22436ae55bc Mon Sep 17 00:00:00 2001 From: Josephine Barboza Date: Sun, 20 May 2018 22:44:46 +0530 Subject: [PATCH] Context Hierarchy Using Spring Boot Fluent Builder API --- spring-boot-ctx-fluent/pom.xml | 27 +++++++++++++++++++ .../src/main/java/com/baeldung/App.java | 22 +++++++++++++++ .../com/baeldung/rest/GreetingController.java | 19 +++++++++++++ .../java/com/baeldung/rest/RestConfig.java | 14 ++++++++++ .../com/baeldung/services/HomeService.java | 11 ++++++++ .../com/baeldung/services/IHomeService.java | 6 +++++ .../com/baeldung/services/ServiceConfig.java | 8 ++++++ .../com/baeldung/web/GreetingService.java | 13 +++++++++ .../java/com/baeldung/web/HomeController.java | 22 +++++++++++++++ .../main/java/com/baeldung/web/WebConfig.java | 21 +++++++++++++++ .../src/main/resources/application.properties | 0 .../src/main/resources/rest-app.properties | 5 ++++ .../src/main/resources/web-app.properties | 3 +++ 13 files changed, 171 insertions(+) create mode 100644 spring-boot-ctx-fluent/pom.xml create mode 100644 spring-boot-ctx-fluent/src/main/java/com/baeldung/App.java create mode 100644 spring-boot-ctx-fluent/src/main/java/com/baeldung/rest/GreetingController.java create mode 100644 spring-boot-ctx-fluent/src/main/java/com/baeldung/rest/RestConfig.java create mode 100644 spring-boot-ctx-fluent/src/main/java/com/baeldung/services/HomeService.java create mode 100644 spring-boot-ctx-fluent/src/main/java/com/baeldung/services/IHomeService.java create mode 100644 spring-boot-ctx-fluent/src/main/java/com/baeldung/services/ServiceConfig.java create mode 100644 spring-boot-ctx-fluent/src/main/java/com/baeldung/web/GreetingService.java create mode 100644 spring-boot-ctx-fluent/src/main/java/com/baeldung/web/HomeController.java create mode 100644 spring-boot-ctx-fluent/src/main/java/com/baeldung/web/WebConfig.java create mode 100644 spring-boot-ctx-fluent/src/main/resources/application.properties create mode 100644 spring-boot-ctx-fluent/src/main/resources/rest-app.properties create mode 100644 spring-boot-ctx-fluent/src/main/resources/web-app.properties diff --git a/spring-boot-ctx-fluent/pom.xml b/spring-boot-ctx-fluent/pom.xml new file mode 100644 index 0000000000..5e308a0134 --- /dev/null +++ b/spring-boot-ctx-fluent/pom.xml @@ -0,0 +1,27 @@ + + 4.0.0 + + com.baeldung + ctxexample + 0.0.1-SNAPSHOT + jar + + ctxexample + http://maven.apache.org + + + UTF-8 + + + org.springframework.boot + spring-boot-starter-parent + 2.0.0.RELEASE + + + + org.springframework.boot + spring-boot-starter-web + + + diff --git a/spring-boot-ctx-fluent/src/main/java/com/baeldung/App.java b/spring-boot-ctx-fluent/src/main/java/com/baeldung/App.java new file mode 100644 index 0000000000..da552a264b --- /dev/null +++ b/spring-boot-ctx-fluent/src/main/java/com/baeldung/App.java @@ -0,0 +1,22 @@ +package com.baeldung; + +import org.springframework.boot.WebApplicationType; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; + +import com.baeldung.rest.RestConfig; +import com.baeldung.services.ServiceConfig; +import com.baeldung.web.WebConfig; + +@SpringBootApplication +public class App { + public static void main(String[] args) { + new SpringApplicationBuilder().parent(ServiceConfig.class) + .web(WebApplicationType.NONE) + .child(WebConfig.class) + .web(WebApplicationType.SERVLET) + .sibling(RestConfig.class) + .web(WebApplicationType.SERVLET) + .run(args); + } +} diff --git a/spring-boot-ctx-fluent/src/main/java/com/baeldung/rest/GreetingController.java b/spring-boot-ctx-fluent/src/main/java/com/baeldung/rest/GreetingController.java new file mode 100644 index 0000000000..563a374dd1 --- /dev/null +++ b/spring-boot-ctx-fluent/src/main/java/com/baeldung/rest/GreetingController.java @@ -0,0 +1,19 @@ +package com.baeldung.rest; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.services.IHomeService; + +@RestController +public class GreetingController { + + @Autowired + IHomeService homeService; + + @GetMapping(value = "/greeting", produces = "application/json") + public String getGreeting() { + return homeService.getGreeting(); + } +} diff --git a/spring-boot-ctx-fluent/src/main/java/com/baeldung/rest/RestConfig.java b/spring-boot-ctx-fluent/src/main/java/com/baeldung/rest/RestConfig.java new file mode 100644 index 0000000000..77b78d01b6 --- /dev/null +++ b/spring-boot-ctx-fluent/src/main/java/com/baeldung/rest/RestConfig.java @@ -0,0 +1,14 @@ +package com.baeldung.rest; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; + +@Configuration +@ComponentScan("com.baeldung.rest") +@EnableAutoConfiguration +@PropertySource("classpath:rest-app.properties") +public class RestConfig { + +} diff --git a/spring-boot-ctx-fluent/src/main/java/com/baeldung/services/HomeService.java b/spring-boot-ctx-fluent/src/main/java/com/baeldung/services/HomeService.java new file mode 100644 index 0000000000..c1cbe027ee --- /dev/null +++ b/spring-boot-ctx-fluent/src/main/java/com/baeldung/services/HomeService.java @@ -0,0 +1,11 @@ +package com.baeldung.services; + +import org.springframework.stereotype.Service; + +@Service +public class HomeService implements IHomeService { + + public String getGreeting() { + return "Welcome User"; + } +} diff --git a/spring-boot-ctx-fluent/src/main/java/com/baeldung/services/IHomeService.java b/spring-boot-ctx-fluent/src/main/java/com/baeldung/services/IHomeService.java new file mode 100644 index 0000000000..0386e13c0b --- /dev/null +++ b/spring-boot-ctx-fluent/src/main/java/com/baeldung/services/IHomeService.java @@ -0,0 +1,6 @@ +package com.baeldung.services; + +public interface IHomeService { + + String getGreeting(); +} diff --git a/spring-boot-ctx-fluent/src/main/java/com/baeldung/services/ServiceConfig.java b/spring-boot-ctx-fluent/src/main/java/com/baeldung/services/ServiceConfig.java new file mode 100644 index 0000000000..6f98bb0457 --- /dev/null +++ b/spring-boot-ctx-fluent/src/main/java/com/baeldung/services/ServiceConfig.java @@ -0,0 +1,8 @@ +package com.baeldung.services; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan("com.baeldung.services") +public class ServiceConfig {} diff --git a/spring-boot-ctx-fluent/src/main/java/com/baeldung/web/GreetingService.java b/spring-boot-ctx-fluent/src/main/java/com/baeldung/web/GreetingService.java new file mode 100644 index 0000000000..1782b35489 --- /dev/null +++ b/spring-boot-ctx-fluent/src/main/java/com/baeldung/web/GreetingService.java @@ -0,0 +1,13 @@ +package com.baeldung.web; + +import org.springframework.stereotype.Service; + +import com.baeldung.services.IHomeService; + +@Service +public class GreetingService implements IHomeService { + + public String getGreeting() { + return "Greetings for the day"; + } +} diff --git a/spring-boot-ctx-fluent/src/main/java/com/baeldung/web/HomeController.java b/spring-boot-ctx-fluent/src/main/java/com/baeldung/web/HomeController.java new file mode 100644 index 0000000000..622470107d --- /dev/null +++ b/spring-boot-ctx-fluent/src/main/java/com/baeldung/web/HomeController.java @@ -0,0 +1,22 @@ +package com.baeldung.web; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import com.baeldung.services.IHomeService; + +@Controller +public class HomeController { + + @Autowired + IHomeService homeService; + + @GetMapping("/home") + @ResponseBody + public String greeting() { + + return homeService.getGreeting(); + } +} diff --git a/spring-boot-ctx-fluent/src/main/java/com/baeldung/web/WebConfig.java b/spring-boot-ctx-fluent/src/main/java/com/baeldung/web/WebConfig.java new file mode 100644 index 0000000000..0b0a9a18f6 --- /dev/null +++ b/spring-boot-ctx-fluent/src/main/java/com/baeldung/web/WebConfig.java @@ -0,0 +1,21 @@ +package com.baeldung.web; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; + +import com.baeldung.services.IHomeService; + +@Configuration +@ComponentScan("com.baeldung.web") +@EnableAutoConfiguration +@PropertySource("classpath:web-app.properties") +public class WebConfig { + + @Bean + public IHomeService homeService() { + return new GreetingService(); + } +} diff --git a/spring-boot-ctx-fluent/src/main/resources/application.properties b/spring-boot-ctx-fluent/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-boot-ctx-fluent/src/main/resources/rest-app.properties b/spring-boot-ctx-fluent/src/main/resources/rest-app.properties new file mode 100644 index 0000000000..dc5a463238 --- /dev/null +++ b/spring-boot-ctx-fluent/src/main/resources/rest-app.properties @@ -0,0 +1,5 @@ +server.port=8081 +server.servlet.context-path=/rest +#logging.level=debug +spring.application.admin.enabled=false +spring.application.admin.jmx-name=org.springframework.boot:type=AdminRest,name=SpringRestApplication \ No newline at end of file diff --git a/spring-boot-ctx-fluent/src/main/resources/web-app.properties b/spring-boot-ctx-fluent/src/main/resources/web-app.properties new file mode 100644 index 0000000000..2b81875901 --- /dev/null +++ b/spring-boot-ctx-fluent/src/main/resources/web-app.properties @@ -0,0 +1,3 @@ +server.port=8080 +spring.application.admin.enabled=false +spring.application.admin.jmx-name=org.springframework.boot:type=WebAdmin,name=SpringWebApplication \ No newline at end of file