File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1- someInitialValue =This is only sample value
2- anotherValue =Another configured value
1+ spring.profiles.active =prod
2+
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments