BAEL-5664 code for the using environment varibales in application.properties article
This commit is contained in:
parent
8db0d7b277
commit
ac46a47524
|
@ -1,7 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
@ -10,23 +10,36 @@
|
|||
<name>spring-core-6</name>
|
||||
<url>http://www.baeldung.com</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<spring.boot.version>2.7.2</spring.boot.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.11</version>
|
||||
<scope>test</scope>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>2.0.0.RELEASE</version>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.envvariables.valueinjection;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "baeldung")
|
||||
public class BaeldungProperties {
|
||||
|
||||
private String presentation;
|
||||
|
||||
public String getPresentation() {
|
||||
return presentation;
|
||||
}
|
||||
|
||||
public void setPresentation(String presentation) {
|
||||
this.presentation = presentation;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package com.baeldung.envvariables.valueinjection;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class MyController {
|
||||
|
||||
@Value("${environment.name}")
|
||||
private String environmentName;
|
||||
|
||||
@Value("${java.home.and.environment}")
|
||||
private String javaHomeAndEnvironmentName;
|
||||
|
||||
@Value("${thispropertydoesnotexist}")
|
||||
private String nonExistentProperty;
|
||||
|
||||
@Value("${baeldung.presentation}")
|
||||
private String baeldungPresentation;
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Autowired
|
||||
private BaeldungProperties baeldungProperties;
|
||||
|
||||
@GetMapping("/environment_name")
|
||||
String getEnvironmentName_FromEnvironmentVariables() {
|
||||
return environmentName;
|
||||
}
|
||||
|
||||
@GetMapping("/java_home_and_environment")
|
||||
String getJavaHomeAndEnvironmentName_FromEnvironmentVariables() {
|
||||
return javaHomeAndEnvironmentName;
|
||||
}
|
||||
|
||||
@GetMapping("non_existent_property")
|
||||
String getNonexistentProperty_FromEnvironmentVariables() {
|
||||
return nonExistentProperty;
|
||||
}
|
||||
|
||||
@GetMapping("baeldung_presentation_from_value")
|
||||
String getBaeldungPresentation_FromValue() {
|
||||
return baeldungPresentation;
|
||||
}
|
||||
|
||||
@GetMapping("baeldung_presentation_from_environment")
|
||||
String getBaeldungPresentation_FromEnvironment() {
|
||||
return environment.getProperty("baeldung.presentation");
|
||||
}
|
||||
|
||||
@GetMapping("baeldung_configuration_properties")
|
||||
String getBaeldungPresentation_FromConfigurationProperties() {
|
||||
return baeldungProperties.getPresentation();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
environment.name=${OS}
|
||||
java.home.and.environment=${JAVA_HOME}+${OS}
|
||||
not.existing.system.property=${thispropertydoesnotexist}
|
||||
baeldung.presentation=${HELLO_BAELDUNG}. Java is installed in the folder: ${JAVA_HOME}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.envvariables.valueinjection;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
@SpringBootTest(classes = MyController.class)
|
||||
@AutoConfigureMockMvc
|
||||
public class MyControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
/** NB : these tests are commented out because they are environment dependent
|
||||
* If you want to run one of them on your machine, follow the instruction above it
|
||||
*
|
||||
* expects the value of your system environment property 'OS' (it is already defined at least in Windows_NT)
|
||||
@Test void givenExistingSystemProperty_whenInjected_thenHasSystemPropertyValue() throws Exception {
|
||||
mockMvc.perform(get("/environment_name"))
|
||||
.andExpect(content().string(equalTo("Windows_NT")));
|
||||
}
|
||||
|
||||
* expects the value of the JAVA_HOME environment variable (you need to define it if you haven't yet), with a + and the 'OS' environment property in the end
|
||||
@Test void givenCombinationOfSystemPropertyAndEnvironmentVariable_whenInjected_thenHasExpectedValue() throws Exception {
|
||||
mockMvc.perform(get("/java_home_and_environment"))
|
||||
.andExpect(content().string(equalTo("C:\\Program Files\\Java\\jdk-11.0.14+Windows_NT")));
|
||||
}
|
||||
|
||||
* expects the content to be ${thispropertydoesnotexist} ; if you have defined an environment property called thispropertydoesnotexist, it would fail
|
||||
@Test void givenNonExistentProperty_whenInjected_thenKeepsTheStringValue() throws Exception {
|
||||
mockMvc.perform(get("/non_existent_property"))
|
||||
.andExpect(content().string(equalTo("${thispropertydoesnotexist}")));
|
||||
}
|
||||
*/
|
||||
}
|
Loading…
Reference in New Issue