fix: use env to get properties

This commit is contained in:
LUCA 2021-12-13 10:38:28 +01:00
parent 098701a66d
commit 727b34ea22
1 changed files with 5 additions and 4 deletions

View File

@ -1,6 +1,7 @@
package com.baeldung.prefix; package com.baeldung.prefix;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -8,12 +9,12 @@ import org.springframework.web.bind.annotation.GetMapping;
@Controller @Controller
public class PrefixController { public class PrefixController {
@Value(value = "${server.port}") @Autowired
private int serverPort; private Environment environment;
@GetMapping("/prefix") @GetMapping("/prefix")
public String getServerPortInfo(final Model model) { public String getServerPortInfo(final Model model) {
model.addAttribute("serverPort", serverPort); model.addAttribute("serverPort", environment.getProperty("server.port"));
return "prefix"; return "prefix";
} }
} }