Context Hierarchy Using Spring Boot Fluent Builder API

This commit is contained in:
Josephine Barboza 2018-05-20 22:44:46 +05:30
parent c2590b9d7e
commit e0c1e3711c
13 changed files with 171 additions and 0 deletions

View File

@ -0,0 +1,27 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>ctxexample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ctxexample</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -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 {
}

View File

@ -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";
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.services;
public interface IHomeService {
String getGreeting();
}

View File

@ -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 {}

View File

@ -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";
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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

View File

@ -0,0 +1,3 @@
server.port=8080
spring.application.admin.enabled=false
spring.application.admin.jmx-name=org.springframework.boot:type=WebAdmin,name=SpringWebApplication