fix: use env to get properties
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.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@@ -8,12 +9,12 @@ import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class PrefixController {
- @Value(value = "${server.port}")
- private int serverPort;
+ @Autowired
+ private Environment environment;
@GetMapping("/prefix")
public String getServerPortInfo(final Model model) {
- model.addAttribute("serverPort", serverPort);
+ model.addAttribute("serverPort", environment.getProperty("server.port"));
return "prefix";
}
}
