2
2
.
.
1
1
4
4
.
.
1
1
R
R
e
e
a
a
d
d
P
P
r
r
o
o
p
p
e
e
r
r
t
t
y
y
-
-
F
F
r
r
o
o
m
m
C
C
o
o
n
n
t
t
r
r
o
o
l
l
l
l
e
e
r
r
I
I
n
n
f
f
o
o
[
[
R
R
]
]
This tutorial shows how to load Property from application.properties into Variable inside Controller.
Syntax
@Value("${messages.hello1}")
private String message;
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @RequestMapping. Includes Tomcat HTTP Server.
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: properties_read_fromcontroller (add Spring Boot Starters from the table)
Edit File: application.properties (inside src/main/resources)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside controllers package)
application.properties
messages.hello1 : Hello1 from Controller
messages.hello2 = Hello2 from Controller
MyController.java
package com.ivoronline.properties_read_fromcontroller.controllers;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@Value("${messages.hello1}")
private String message;
@ResponseBody
@RequestMapping("/Hello")
public String hello() {
return message;
}
}
hello()
MyController
http://localhost:8080/Hello
Browser
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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>