Spring YAML Configuration (#1807)
* Spring YAML Configuration * Spring YAML Configuration * Spring YAML Configuration * Update application.yml removing ^M * Update application.yml * Spring YAML Configuration * Update pom.xml * Update pom.xml * Spring YAML Configuration * Spring YAMl Configuration
This commit is contained in:
parent
b5077409f4
commit
0fab04f1b6
@ -58,12 +58,17 @@
|
|||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
<version>${lombok.version}</version>
|
<version>${lombok.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
|
<version>1.5.2.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-test</artifactId>
|
<artifactId>spring-boot-test</artifactId>
|
||||||
<version>${mockito.spring.boot.version}</version>
|
<version>${mockito.spring.boot.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package com.baeldung.yaml;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.CommandLineRunner;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class MyApplication implements CommandLineRunner {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private YAMLConfig myConfig;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication app = new SpringApplication(MyApplication.class);
|
||||||
|
app.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run(String... args) throws Exception {
|
||||||
|
System.out.println("using environment:" + myConfig.getEnvironment());
|
||||||
|
System.out.println("name:" + myConfig.getName());
|
||||||
|
System.out.println("servers:" + myConfig.getServers());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
41
spring-core/src/main/java/com/baeldung/yaml/YAMLConfig.java
Normal file
41
spring-core/src/main/java/com/baeldung/yaml/YAMLConfig.java
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package com.baeldung.yaml;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableConfigurationProperties
|
||||||
|
@ConfigurationProperties
|
||||||
|
public class YAMLConfig {
|
||||||
|
private String name;
|
||||||
|
private String environment;
|
||||||
|
private List<String> servers = new ArrayList<String>();
|
||||||
|
|
||||||
|
public List<String> getServers() {
|
||||||
|
return servers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setServers(List<String> servers) {
|
||||||
|
this.servers = servers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEnvironment() {
|
||||||
|
return environment;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnvironment(String environment) {
|
||||||
|
this.environment = environment;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,2 +1,2 @@
|
|||||||
someInitialValue=This is only sample value
|
spring.profiles.active=prod
|
||||||
anotherValue=Another configured value
|
|
||||||
|
17
spring-core/src/main/resources/application.yml
Normal file
17
spring-core/src/main/resources/application.yml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
spring:
|
||||||
|
profiles: test
|
||||||
|
name: test-YAML
|
||||||
|
environment: test
|
||||||
|
servers:
|
||||||
|
- www.abc.test.com
|
||||||
|
- www.xyz.test.com
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
spring:
|
||||||
|
profiles: prod
|
||||||
|
name: prod-YAML
|
||||||
|
environment: production
|
||||||
|
servers:
|
||||||
|
- www.abc.com
|
||||||
|
- www.xyz.com
|
Loading…
x
Reference in New Issue
Block a user