From 727b34ea22d05dca6ed9ab4fb22764d17278e881 Mon Sep 17 00:00:00 2001 From: LUCA Date: Mon, 13 Dec 2021 10:38:28 +0100 Subject: [PATCH] fix: use env to get properties --- .../main/java/com/baeldung/prefix/PrefixController.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/spring-boot-modules/spring-boot-environment/src/main/java/com/baeldung/prefix/PrefixController.java b/spring-boot-modules/spring-boot-environment/src/main/java/com/baeldung/prefix/PrefixController.java index 00b728c7ae..dc6b0cd513 100644 --- a/spring-boot-modules/spring-boot-environment/src/main/java/com/baeldung/prefix/PrefixController.java +++ b/spring-boot-modules/spring-boot-environment/src/main/java/com/baeldung/prefix/PrefixController.java @@ -1,6 +1,7 @@ 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"; } }