Understanding application.properties and application.yaml

Keywords: Spring Attribute Lombok Redis

In SpringBoot, there are two different formats for configuration files, one is properties and the other is yaml.

Although properties files are common, yaml is more concise and uses more scenarios than properties, and many open source projects are configured using yaml (for example, Hexo).In addition to simplicity, yaml has another feature: the data in yaml is ordered, the data in properties is out of order, and in some configurations that require path matching, the order is particularly important (such as our configuration in Spring Cloud Zuul), where yaml is commonly used.

1. Default application.properties

First, when we create a Spring Boot project, there is an application.properties file in the default resources directory that you can configure in the application.properties file, but it is not the only configuration file. There are four places in the Spring Boot where you can store the application.properties file.

  1. The config directory under the current project root directory

  2. The root directory of the current project

  3. config directory under resources Directory

  4. Under the resources directory

These four locations are the default locations, Spring Boot startup, from which related properties are sequentially found and loaded by default.However, this is not absolute, and we can also customize the profile location at project startup.

application.yaml can also be written in these four locations.

2. Attribute Injection

Attribute injection is divided into normal attribute injection and type-safe attribute injection.

Common property injection, mainly using the @Value annotation to customize the book.properties file

book.name=Romance of the Three Kingdoms
book.author=Luo Guanzhong
book.id=1

Define entity classes to automatically generate getter s and setter s with lombok to implement injection defaults

@Data //Generate getter,setter, etc.
@AllArgsConstructor //Generate full-parameter constructors
@NoArgsConstructor //Generate parameterless constructors
@Component
@PropertySource("classpath:book.properties")
public class Book {
    @Value("${book.id}")
    private Long id;
    @Value("${book.name}")
    private String name;
    @Value("${book.author}")
    private String author;
}

Type-safe property injection into custom author.properties file author.name=Rocchong

  •  
  •  
  •  
  •  
author.name=Luo Guanzhongauthor.age=18author.id=1author.sex=male

Define entity classes, automatically generate getter s and setter s with lombok methods, introduce the @ConfigurationProperties(prefix = "book") annotation, and configure the prefix of the properties, which automatically injects the corresponding data from the Spring container into the corresponding properties of the object

@Data //Generate getter,setter, etc.
@AllArgsConstructor //Generate full-parameter constructors
@NoArgsConstructor //Generate parameterless constructors
@Component //Give Author objects to the Spring container to manage
@PropertySource(value = "classpath:author.properties") //Introducing a configuration file
@ConfigurationProperties(prefix = "author") //Automatically inject the corresponding data from the Spring container into the corresponding properties of the object based on the prefix of the property
public class Author {
    private Long id;
    private Integer age;
    private String sex;
    private String name;
}

yaml supports array injection

for example

my:
  servers:
    - dev.example.com
    - another.example.com

This data can be bound to an array with beans:

@ConfigurationProperties(prefix="my")
@Component
public class Config {

    private List<String> servers = new ArrayList<String>();

    public List<String> getServers() {
        return this.servers;
    }
}

yaml can store not only this simple data, but also objects in collections.

for example

redis:
  redisConfigs:
    - host: 192.168.66.128
      port: 6379
    - host: 192.168.66.129
      port: 6380

This can be injected into the following classes:

@Component
@ConfigurationProperties(prefix = "redis")
public class RedisCluster {
    private List<SingleRedisConfig> redisConfigs;
    //Omit getter/setter
}

test

Using Junit:

@RunWith(SpringRunner.class)
@SpringBootTest
class Springboot2trainApplicationTests {
    @Autowired
    private Book book;

    @Test
    void contextLoads() {
        System.out.println(book);
    }
}

Output:

Book(id=1, name=Journey to the West, author=Luo Guanzhong)

Several other little buddies can try it by themselves.

3. Summary

application.properties is an important carrier of configuration in Spring Boot, where the properties of many components can be customized.Unlike the disorder of properties files, yaml configuration is ordered, which is useful in some configurations, such as Spring Cloud Zuul configuration, where order is particularly important when configuring proxy rules.Of course, yaml configuration is not everything. For example, yaml configuration currently does not support the @PropertySource annotation.

Thirteen original articles were published, 0 won and 1685 visited

Posted by martins on Tue, 04 Feb 2020 20:29:20 -0800