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