Skip to content

Commit 0fab04f

Browse files
TehreemNisapivovarit
authored andcommitted
Spring YAML Configuration (eugenp#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
1 parent b507740 commit 0fab04f

5 files changed

Lines changed: 97 additions & 3 deletions

File tree

‎spring-core/pom.xml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,17 @@
5858
<artifactId>lombok</artifactId>
5959
<version>${lombok.version}</version>
6060
</dependency>
61+
<dependency>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-starter</artifactId>
64+
<version>1.5.2.RELEASE</version>
65+
</dependency>
6166
<dependency>
6267
<groupId>org.springframework.boot</groupId>
6368
<artifactId>spring-boot-test</artifactId>
6469
<version>${mockito.spring.boot.version}</version>
6570
<scope>test</scope>
66-
</dependency>
71+
</dependency>
6772
</dependencies>
6873

6974
<build>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.baeldung.yaml;
7+
8+
import java.util.Collections;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.CommandLineRunner;
11+
import org.springframework.boot.SpringApplication;
12+
import org.springframework.boot.autoconfigure.SpringBootApplication;
13+
14+
@SpringBootApplication
15+
public class MyApplication implements CommandLineRunner {
16+
17+
@Autowired
18+
private YAMLConfig myConfig;
19+
20+
public static void main(String[] args) {
21+
SpringApplication app = new SpringApplication(MyApplication.class);
22+
app.run();
23+
}
24+
25+
public void run(String... args) throws Exception {
26+
System.out.println("using environment:" + myConfig.getEnvironment());
27+
System.out.println("name:" + myConfig.getName());
28+
System.out.println("servers:" + myConfig.getServers());
29+
}
30+
31+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.yaml;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import org.springframework.boot.context.properties.ConfigurationProperties;
6+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
7+
import org.springframework.context.annotation.Configuration;
8+
9+
@Configuration
10+
@EnableConfigurationProperties
11+
@ConfigurationProperties
12+
public class YAMLConfig {
13+
private String name;
14+
private String environment;
15+
private List<String> servers = new ArrayList<String>();
16+
17+
public List<String> getServers() {
18+
return servers;
19+
}
20+
21+
public void setServers(List<String> servers) {
22+
this.servers = servers;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
public String getEnvironment() {
34+
return environment;
35+
}
36+
37+
public void setEnvironment(String environment) {
38+
this.environment = environment;
39+
}
40+
41+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
someInitialValue=This is only sample value
2-
anotherValue=Another configured value
1+
spring.profiles.active=prod
2+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
spring:
2+
profiles: test
3+
name: test-YAML
4+
environment: test
5+
servers:
6+
- www.abc.test.com
7+
- www.xyz.test.com
8+
9+
---
10+
11+
spring:
12+
profiles: prod
13+
name: prod-YAML
14+
environment: production
15+
servers:
16+
- www.abc.com
17+
- www.xyz.com

0 commit comments

Comments
 (0)