Skip to content

Commit 087f0c2

Browse files
authored
Merge pull request eugenp#4297 from josephine-barboza/spring-boot-ctx-fluent
Context Hierarchy Using Spring Boot Fluent Builder API (BAEL-1689)
2 parents 8bc8488 + e0c1e37 commit 087f0c2

13 files changed

Lines changed: 171 additions & 0 deletions

File tree

‎spring-boot-ctx-fluent/pom.xml‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>ctxexample</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>ctxexample</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
<parent>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-parent</artifactId>
19+
<version>2.0.0.RELEASE</version>
20+
</parent>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
</dependencies>
27+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung;
2+
3+
import org.springframework.boot.WebApplicationType;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.builder.SpringApplicationBuilder;
6+
7+
import com.baeldung.rest.RestConfig;
8+
import com.baeldung.services.ServiceConfig;
9+
import com.baeldung.web.WebConfig;
10+
11+
@SpringBootApplication
12+
public class App {
13+
public static void main(String[] args) {
14+
new SpringApplicationBuilder().parent(ServiceConfig.class)
15+
.web(WebApplicationType.NONE)
16+
.child(WebConfig.class)
17+
.web(WebApplicationType.SERVLET)
18+
.sibling(RestConfig.class)
19+
.web(WebApplicationType.SERVLET)
20+
.run(args);
21+
}
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.rest;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
import com.baeldung.services.IHomeService;
8+
9+
@RestController
10+
public class GreetingController {
11+
12+
@Autowired
13+
IHomeService homeService;
14+
15+
@GetMapping(value = "/greeting", produces = "application/json")
16+
public String getGreeting() {
17+
return homeService.getGreeting();
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.rest;
2+
3+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.PropertySource;
7+
8+
@Configuration
9+
@ComponentScan("com.baeldung.rest")
10+
@EnableAutoConfiguration
11+
@PropertySource("classpath:rest-app.properties")
12+
public class RestConfig {
13+
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.services;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
@Service
6+
public class HomeService implements IHomeService {
7+
8+
public String getGreeting() {
9+
return "Welcome User";
10+
}
11+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.services;
2+
3+
public interface IHomeService {
4+
5+
String getGreeting();
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.services;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
@ComponentScan("com.baeldung.services")
8+
public class ServiceConfig {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.web;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
import com.baeldung.services.IHomeService;
6+
7+
@Service
8+
public class GreetingService implements IHomeService {
9+
10+
public String getGreeting() {
11+
return "Greetings for the day";
12+
}
13+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.web;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.ResponseBody;
7+
8+
import com.baeldung.services.IHomeService;
9+
10+
@Controller
11+
public class HomeController {
12+
13+
@Autowired
14+
IHomeService homeService;
15+
16+
@GetMapping("/home")
17+
@ResponseBody
18+
public String greeting() {
19+
20+
return homeService.getGreeting();
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.web;
2+
3+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.PropertySource;
8+
9+
import com.baeldung.services.IHomeService;
10+
11+
@Configuration
12+
@ComponentScan("com.baeldung.web")
13+
@EnableAutoConfiguration
14+
@PropertySource("classpath:web-app.properties")
15+
public class WebConfig {
16+
17+
@Bean
18+
public IHomeService homeService() {
19+
return new GreetingService();
20+
}
21+
}

0 commit comments

Comments
 (0)