current position:Home>Springboot Foundation (19): loose binding

Springboot Foundation (19): loose binding

2022-06-24 09:21:03There is no retreat when sailing against the current


Loose binding . The configuration file is loosely bound to the parameters .

Loose binding demo

Under normal circumstances , We'll write that . Finally, the results can be output normally .
 Insert picture description here

Write the configuration file differently , It can also output
 Insert picture description here
 Insert picture description here

summary :

because @ConfigurationProperties The parameters of are loosely bound , So its format is arbitrary , All the ways listed below are OK . Case write , Underline , Horizontal bar ,( Underline and bar can be anywhere , There can be multiple ), It does not affect the binding relationship between the configuration file and parameters .

# ipAddress:
# ipaddress:
# ip-address:
# ip_add_ress:
   port: 1000
@ConfigurationProperties(prefix = "servers")
public class ServerConfig {

    private String ipAddress;

    private int port;

@ConfigurationProperties Binding properties support loose binding of property names
Binding prefix name naming convention : Only lower case letters are supported 、 Numbers 、 Underscore as a legal character
@Value Annotations do not support loose binding

Extended reading

Springboot Basics (4): There are three ways to get configuration files

copyright notice
author[There is no retreat when sailing against the current],Please bring the original link to reprint, thank you.

Random recommended