Master Spring Boot Configuration: Properties vs YAML and Value Extraction Techniques
This article explains Spring Boot's two configuration file formats—properties and YAML—detailing their syntax, how to map them to Java classes, and the various annotations (@ConfigurationProperties, @Value, @PropertySource) for retrieving configuration values, including custom file handling.
Introduction
Spring Boot supports two main configuration file formats: application.properties and application.yml. Both can be used to bind configuration values to Java objects.
Properties format
The classic key=value syntax is simple and widely used in Java projects.
userinfo.name=myjszl
userinfo.age=25
userinfo.active=true
userinfo.created-date=2018/03/31 16:54:30
userinfo.map.k1=v1
userinfo.map.k2=v2A matching POJO can be defined as:
@Data
@ToString
public class UserInfo {
private String name;
private Integer age;
private Boolean active;
private Map<String, Object> map;
private Date createdDate;
private List<String> hobbies;
}YAML format
YAML uses indentation (spaces only) to express hierarchy. The following structures are supported:
Literal values : strings, booleans, numbers, dates (default format yyyy/MM/dd HH:mm:ss).
Objects : key: value (a space after the colon is mandatory) or inline {k1: v1, k2: v2}.
Arrays : each element starts with - (dash and space) or inline [a, b, c].
Composite structures : any combination of the above.
Example application.yml placed under src/main/resources:
userinfo:
name: myjszl
age: 25
active: true
created-date: 2018/03/31 16:54:30
map: {k1: v1, k2: v2}
hobbies:
- one
- two
- threeThe same POJO shown for the properties format can be used.
Retrieving configuration values
@ConfigurationProperties
This annotation binds a group of properties (identified by a common prefix) to a POJO. It supports complex types such as List and Map, flexible property‑name matching, and JSR‑303 validation. It cannot evaluate SpEL expressions.
Usage on a POJO
@Component
@ConfigurationProperties(prefix = "userinfo")
@Data
@ToString
public class UserInfo {
private String name;
private Integer age;
private Boolean active;
private Map<String, Object> map;
private Date createdDate;
private List<String> hobbies;
}Usage on a @Bean method
@ConfigurationProperties(prefix = "userinfo")
@Bean
public UserInfo userInfo() {
return new UserInfo();
}Advantages:
Batch injection via a common prefix.
Supports complex collection types.
Property names are tolerant of different naming conventions (e.g., user-name, user_name, userName, USER_NAME).
Integrates with JSR‑303 validation.
Note: @ConfigurationProperties reads only from Spring Boot’s default configuration files ( application.properties or application.yml).
@Value
The classic Spring annotation injects a single property value and can evaluate SpEL expressions, but it does not bind complex types.
@Value("${userinfo.name}")
private String userName;Loading custom configuration files
Spring Boot automatically loads application.* and bootstrap.*. To load additional files you can use @PropertySource.
Custom .properties file
@SpringBootApplication
@PropertySource("classpath:custom.properties")
public class DemoApplication { }The value attribute accepts an array, allowing multiple files.
Custom YAML file
By default @PropertySource only supports .properties. To load a YAML file you need a custom PropertySourceFactory:
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;
import java.io.IOException;
import java.util.Properties;
public class YmlConfigFactory extends DefaultPropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
String sourceName = (name != null) ? name : resource.getResource().getFilename();
if (!resource.getResource().exists()) {
return new PropertiesPropertySource(sourceName, new Properties());
} else if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) {
Properties props = loadYml(resource);
return new PropertiesPropertySource(sourceName, props);
} else {
return super.createPropertySource(name, resource);
}
}
private Properties loadYml(EncodedResource resource) throws IOException {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
}
}Register the factory in @PropertySource:
@SpringBootApplication
@PropertySource(value = "classpath:custom.yml", factory = YmlConfigFactory.class)
public class DemoApplication { }With this setup the custom YAML file is treated like the default configuration files and can be accessed via @ConfigurationProperties or @Value.
Summary
Spring Boot supports both .properties and .yml configuration formats. Use @ConfigurationProperties for type‑safe binding of complex structures, and @Value for simple value injection with SpEL support. Custom configuration files can be loaded with @PropertySource; for YAML you must provide a custom PropertySourceFactory such as the YmlConfigFactory shown above.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
