Attribute Loading Details of SpringBook Tutorial

Keywords: Programming Spring Attribute Java less


  • Loading property order
  • Random attribute
  • Command Line Properties
  • Application property file
  • Profile-specific attributes
  • Placeholders in attributes
  • YAML attribute
  • Access attribute
  • Multi-profile configuration
  • Disadvantages of YAML
  • Attribute prefix
  • Loose binding rules for attributes
  • Attribute transformation
  • Time Unit Conversion
  • Data Size Conversion
  • Check attribute

Loading property order

Spring Boot loads property in the following order:

  1. Devtools global configuration (When devtools are activated \/.
  2. =" "> Test Property Source Annotation Configuration in Test Environment
  3. properties in the test environment: @SpringBootTest Sum Test notes.
  4. Command line parameters
  6. ServletConfig initialization parameters
  7. ServletContext initialization parameters
  8. JNDI attributes from configures JNDI attributes via java:comp/env
  9. Java System Properties (System.getProperties())
  10. Operating system environment is better than that
  11. Random Value Property Source loads properties in the form of random. *
  12. application-{profile}.properties or application-{profile}.yml configuration outside the jar package
  13. application-{profile}.properties or application-{profile}.yml configuration in the jar package
  14. or application.yml configuration outside the jar package
  15. or application.yml configuration in the jar package
  16. @ Configuration of Property Source Binding
  17. Default properties (specified by SpringApplication.setDefaultProperties)

Random attribute

The RandomValuePropertySource class is used to configure random values.



Command Line Properties

By default, Spring Application takes -- parameters (for example -- server.port=9000) and adds this property to Spring's Environment.

If you don't want to load command line properties, you can disable them by SpringApplication.setAddCommandLineProperties(false).

Application property file

Spring Application automatically loads the configuration file in the following path and reads the properties into Spring's Environment.

  1. The / config subdirectory of the current directory
  2. current directory
  3. /config package under classpath path path path
  4. classpath root path
The configuration files listed above will be in order, and the configuration in the latter order will override the configuration in the former order.
You can choose. YAML(yml) Replace the properties configuration file with the configuration file.

If you don't like as a configuration file name, you can replace it with environment variable:

$ java -jar myproject.jar

The configuration file path can be specified using the spring.config.location environment variable:

$ java -jar myproject.jar --spring.config.location=classpath:/,classpath:/

Profile-specific attributes

If you define a configuration file in the form of application-{profile}.properties, it will be considered a specific configuration in the profile environment.

The profile can be activated by the parameter. If no profile is activated, the configuration in will be loaded by default.

Placeholders in attributes

Values in are filtered by the Environment, so you can refer to previously defined properties.
app.description=${} is a Spring Boot application
Note: You can use this technique to create Spring Book property variables. Please refer to: Section 77.4, "Use 'Short' Command Line Arguments

YAML attribute

Spring Framework provides two convenient classes that can be used to load YAML documents. The YamlPropertiesFactoryBean loads YAML as Properties and the YamlMapFactoryBean loads YAML as a Map.

The Spring framework has two classes that support loading YAML files.

  • Yaml Properties FactoryBean loads the configuration of the YAML file as Properties.
  • YamlMapFactoryBean loads the configuration of the YAML file as Map.

Example 1

		name: Developer Setup
		name: My Cool App

Equivalent to: Setup Cool App

YAML supports tabular form and is equivalent to [index] in property:


Equivalent to


Access attribute

The YamlPropertySourceLoader class converts the YAML configuration into PropertySource in the Spring Environment class. Then, you can use the same properties as in the properties file. @Value Annotations to access the properties configured in YAML.

Multi-profile configuration

  profiles: development
  profiles: production & eu-central

Disadvantages of YAML

Note: Attributes in YAML annotations cannot be accessed through the @PropertySource annotation. So, if you use some custom property files in your project, it is recommended not to use YAML.

Attribute prefix

package com.example;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class AcmeProperties {

	private boolean enabled;

	private InetAddress remoteAddress;

	private final Security security = new Security();

	public boolean isEnabled() { ... }

	public void setEnabled(boolean enabled) { ... }

	public InetAddress getRemoteAddress() { ... }

	public void setRemoteAddress(InetAddress remoteAddress) { ... }

	public Security getSecurity() { ... }

	public static class Security {

		private String username;

		private String password;

		private List<String> roles = new ArrayList<>(Collections.singleton("USER"));

		public String getUsername() { ... }

		public void setUsername(String username) { ... }

		public String getPassword() { ... }

		public void setPassword(String password) { ... }

		public List<String> getRoles() { ... }

		public void setRoles(List<String> roles) { ... }


Equivalent to supporting configuration of the following attributes:

  • acme.enabled
  • acme.remote-address

Then, you need to inject the attribute class into the configuration class using the @EnableConfiguration Properties annotation.

public class MyConfiguration {

Loose binding rules for attributes

Spring Boot attribute names are loosely bound.

The following attributes key are equivalent:

Property acme.myProject.person.first Name hump named acme.my_project.person.first_name_separated ACME_MYPROJECT_PERSON_FIRSTNAME capital letters

Attribute transformation

If type conversion is required, you can provide a Conversion Service bean (a bean called Conversion Service) or a custom attribute configuration (a Custom Editor Configurer bean) or a custom Converters (a Bena modified by the @Configuration Properties Binding annotation).

Time Unit Conversion

Spring uses the java.time.Duration class to represent the time size. The following scenarios apply:

  • Unless @Duration Unit is specified, a long represents milliseconds.
  • ISO-8601 Standard Format( java.time.Duration The implementation refers to this standard.
  • You can also use the following support units:
    • ns nanosecond
    • us - microsecond
    • ms milliseconds
    • s sec
    • m - points
    • h - time
    • d - day



public class AppSystemProperties {

	private Duration sessionTimeout = Duration.ofSeconds(30);

	private Duration readTimeout = Duration.ofMillis(1000);

	public Duration getSessionTimeout() {
		return this.sessionTimeout;

	public void setSessionTimeout(Duration sessionTimeout) {
		this.sessionTimeout = sessionTimeout;

	public Duration getReadTimeout() {
		return this.readTimeout;

	public void setReadTimeout(Duration readTimeout) {
		this.readTimeout = readTimeout;


Data Size Conversion

Spring uses the DataSize class to represent the data size. The following scenarios apply:

  • long value (default is byte)
  • You can also use the following support units:
    • B
    • KB
    • MB
    • GB
    • TB



public class AppIoProperties {

	private DataSize bufferSize = DataSize.ofMegabytes(2);

	private DataSize sizeThreshold = DataSize.ofBytes(512);

	public DataSize getBufferSize() {
		return this.bufferSize;

	public void setBufferSize(DataSize bufferSize) {
		this.bufferSize = bufferSize;

	public DataSize getSizeThreshold() {
		return this.sizeThreshold;

	public void setSizeThreshold(DataSize sizeThreshold) {
		this.sizeThreshold = sizeThreshold;


Check attribute

public class AcmeProperties {

	private InetAddress remoteAddress;

	private final Security security = new Security();

	// ... getters and setters

	public static class Security {

		public String username;

		// ... getters and setters



You can also customize a checker Bean called configurationProperties Validator. The method to get this @Bean must be declared static.

Usage method:

mvn clean package
cd target
java -jar sbe-core-property.jar

I organize Java advanced materials for free, covering Java, Redis, MongoDB, MySQL, Zookeeper, Spring Cloud, Dubbo, high concurrent and distributed tutorials, a total of 30G, need to collect.

Posted by chaser7016 on Thu, 10 Oct 2019 04:08:12 -0700