diff --git a/spring-core-3/static-value-injection/README.md b/spring-core-3/static-value-injection/README.md new file mode 100644 index 0000000000..06dfa29f80 --- /dev/null +++ b/spring-core-3/static-value-injection/README.md @@ -0,0 +1,22 @@ +# Inject a value to a static field + +## How to run +```sh +mvn clean install +mvn spring-boot:run +``` + +## Request + +**GET** +http://localhost:8080/properties + + +## Response +```json +[ + "Inject a value to a static field", + "Inject a value to a static field", + null +] +``` diff --git a/spring-core-3/static-value-injection/pom.xml b/spring-core-3/static-value-injection/pom.xml new file mode 100644 index 0000000000..aa45fde886 --- /dev/null +++ b/spring-core-3/static-value-injection/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.2.4.RELEASE + + + com.baeldung + static.value.injection + 0.0.1-SNAPSHOT + static.value.injection + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-core-3/static-value-injection/src/main/java/com/baeldung/Application.java b/spring-core-3/static-value-injection/src/main/java/com/baeldung/Application.java new file mode 100644 index 0000000000..c1875216b5 --- /dev/null +++ b/spring-core-3/static-value-injection/src/main/java/com/baeldung/Application.java @@ -0,0 +1,13 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/spring-core-3/static-value-injection/src/main/java/com/baeldung/controller/PropertyController.java b/spring-core-3/static-value-injection/src/main/java/com/baeldung/controller/PropertyController.java new file mode 100644 index 0000000000..03a2518117 --- /dev/null +++ b/spring-core-3/static-value-injection/src/main/java/com/baeldung/controller/PropertyController.java @@ -0,0 +1,31 @@ +package com.baeldung.controller; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Arrays; +import java.util.List; + +@RestController +public class PropertyController { + + @Value("${name}") + private String name; + + @Value("${name}") + private static String NAME_NULL; + + private static String NAME_STATIC; + + @Value("${name}") + public void setNameStatic(String name){ + PropertyController.NAME_STATIC = name; + } + + @GetMapping("/properties") + public List getProperties(){ + return Arrays.asList(this.name, NAME_STATIC, NAME_NULL) ; + } +} diff --git a/spring-core-3/static-value-injection/src/main/resources/application.properties b/spring-core-3/static-value-injection/src/main/resources/application.properties new file mode 100644 index 0000000000..828fa9cd2a --- /dev/null +++ b/spring-core-3/static-value-injection/src/main/resources/application.properties @@ -0,0 +1 @@ +name = Inject a value to a static field diff --git a/spring-core-3/static-value-injection/src/test/java/com/baeldung/ApplicationTests.java b/spring-core-3/static-value-injection/src/test/java/com/baeldung/ApplicationTests.java new file mode 100644 index 0000000000..4ad83bc539 --- /dev/null +++ b/spring-core-3/static-value-injection/src/test/java/com/baeldung/ApplicationTests.java @@ -0,0 +1,13 @@ +package com.baeldung; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class ApplicationTests { + + @Test + void contextLoads() { + } + +}