2
2
.
.
9
9
.
.
2
2
L
L
o
o
c
c
a
a
t
t
i
i
o
o
n
n
-
-
P
P
r
r
o
o
p
p
e
e
r
r
t
t
y
y
I
I
n
n
f
f
o
o
[
[
G
G
]
]
This tutorial shows how to use @Autowired to inject instance into Class Property.
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @Controller and @RequestMapping. Includes Tomcat Server.
MyService
http://localhost:8080/Hello
Tomcat
Browser
MyController
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_autowired_property (add Spring Boot Starters from the table)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside package controllers)
Create Package: services (inside main package)
– Create Class: MyService.java (inside package controllers)
MyController.java
package com.ivoronline.springboot_autowired_property.controllers;
import com.ivoronline.springboot_autowired_property.services.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@Autowired MyService myService;
@ResponseBody
@RequestMapping("/Hello")
public String hello() {
String Results = myService.sayHello();
return Results;
}
}
MyService.java
package com.ivoronline.springboot_autowired_property.services;
import org.springframework.stereotype.Service;
@Service
public class MyService {
public String sayHello() {
return "Hello";
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/Hello
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>