BAEL-4106 (#9887)
* BAEL-4106 application.yml vs application.properties for Spring Boot * Update README.md
This commit is contained in:
parent
0c657fdd79
commit
16ae922c36
@ -1,3 +1,9 @@
|
|||||||
|
|
||||||
|
## Spring Boot Properties
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
|
|
||||||
- [How to Define a Map in YAML for a POJO?](https://www.baeldung.com/yaml-map-pojo)
|
- [How to Define a Map in YAML for a POJO?](https://www.baeldung.com/yaml-map-pojo)
|
||||||
|
|
||||||
|
@ -17,13 +17,11 @@
|
|||||||
<properties>
|
<properties>
|
||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter</artifactId>
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
@ -50,5 +48,4 @@
|
|||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.propertiesvsyaml;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
|
||||||
|
@ConfigurationProperties(prefix = "app")
|
||||||
|
public class ConfigProperties {
|
||||||
|
|
||||||
|
String name;
|
||||||
|
String description;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDescription(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.propertiesvsyaml;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class EnvironmentProperties {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Environment env;
|
||||||
|
|
||||||
|
public String getSomeKey() {
|
||||||
|
return env.getProperty("key.something");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.baeldung.propertiesvsyaml;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
|
||||||
|
public class ValueProperties {
|
||||||
|
|
||||||
|
@Value("${key.something}")
|
||||||
|
private String injectedProperty;
|
||||||
|
|
||||||
|
public String getAppName() {
|
||||||
|
return injectedProperty;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
application.servers[0].ip=127.0.0.1
|
||||||
|
application.servers[0].path=/path1
|
||||||
|
application.servers[1].ip=127.0.0.2
|
||||||
|
application.servers[1].path=/path2
|
||||||
|
application.servers[2].ip=127.0.0.3
|
||||||
|
application.servers[2].path=/path3
|
||||||
|
spring.datasource.url=jdbc:h2:dev
|
||||||
|
spring.datasource.username=SA
|
||||||
|
spring.datasource.password=password
|
||||||
|
app.name=MyApp
|
||||||
|
app.description=${app.name} is a Spring Boot application
|
@ -1,3 +1,25 @@
|
|||||||
|
logging:
|
||||||
|
file:
|
||||||
|
name: myapplication.log
|
||||||
|
spring:
|
||||||
|
datasource:
|
||||||
|
password: 'password'
|
||||||
|
url: jdbc:h2:dev
|
||||||
|
username: SA
|
||||||
|
---
|
||||||
|
spring:
|
||||||
|
datasource:
|
||||||
|
password: 'password'
|
||||||
|
url: jdbc:mysql://localhost:3306/db_production
|
||||||
|
username: user
|
||||||
|
application:
|
||||||
|
servers:
|
||||||
|
- ip: '127.0.0.1'
|
||||||
|
path: '/path1'
|
||||||
|
- ip: '127.0.0.2'
|
||||||
|
path: '/path2'
|
||||||
|
- ip: '127.0.0.3'
|
||||||
|
path: '/path3'
|
||||||
t-shirt-size:
|
t-shirt-size:
|
||||||
simple-mapping:
|
simple-mapping:
|
||||||
XS: 6
|
XS: 6
|
||||||
|
Loading…
x
Reference in New Issue
Block a user