File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .services ;
2+
3+ public interface IHomeService {
4+
5+ String getGreeting ();
6+ }
Original file line number Diff line number Diff line change 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 {}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments