From 2dfdb515922a1f0be1be7af1fef7c03592d24b10 Mon Sep 17 00:00:00 2001 From: lucaCambi77 Date: Wed, 13 Oct 2021 18:21:35 +0200 Subject: [PATCH] feat: env variable prefixes spring boot app (#11323) --- .../baeldung/prefix/PrefixApplication.java | 14 ++++++++++++++ .../com/baeldung/prefix/PrefixController.java | 19 +++++++++++++++++++ .../src/main/resources/templates/prefix.html | 9 +++++++++ 3 files changed, 42 insertions(+) create mode 100644 spring-boot-modules/spring-boot-environment/src/main/java/com/baeldung/prefix/PrefixApplication.java create mode 100644 spring-boot-modules/spring-boot-environment/src/main/java/com/baeldung/prefix/PrefixController.java create mode 100644 spring-boot-modules/spring-boot-environment/src/main/resources/templates/prefix.html diff --git a/spring-boot-modules/spring-boot-environment/src/main/java/com/baeldung/prefix/PrefixApplication.java b/spring-boot-modules/spring-boot-environment/src/main/java/com/baeldung/prefix/PrefixApplication.java new file mode 100644 index 0000000000..29fe3d8930 --- /dev/null +++ b/spring-boot-modules/spring-boot-environment/src/main/java/com/baeldung/prefix/PrefixApplication.java @@ -0,0 +1,14 @@ +package com.baeldung.prefix; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class PrefixApplication { + + public static void main(String[] args) { + SpringApplication application = new SpringApplication(PrefixApplication.class); + application.setEnvironmentPrefix("prefix"); + application.run(args); + } +} 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 new file mode 100644 index 0000000000..00b728c7ae --- /dev/null +++ b/spring-boot-modules/spring-boot-environment/src/main/java/com/baeldung/prefix/PrefixController.java @@ -0,0 +1,19 @@ +package com.baeldung.prefix; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; + +@Controller +public class PrefixController { + + @Value(value = "${server.port}") + private int serverPort; + + @GetMapping("/prefix") + public String getServerPortInfo(final Model model) { + model.addAttribute("serverPort", serverPort); + return "prefix"; + } +} diff --git a/spring-boot-modules/spring-boot-environment/src/main/resources/templates/prefix.html b/spring-boot-modules/spring-boot-environment/src/main/resources/templates/prefix.html new file mode 100644 index 0000000000..7bb5a76537 --- /dev/null +++ b/spring-boot-modules/spring-boot-environment/src/main/resources/templates/prefix.html @@ -0,0 +1,9 @@ + + + + Prefix Example Page + + +It is working as we expected. Your server is running at port : + + \ No newline at end of file