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

Preface

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 .

servers:
# ipAddress: 192.168.1.2
# ipaddress: 192.168.1.3
# ip-address: 192.168.1.4
# ip_add_ress: 192.168.1.5
# IPADDRESS: 192.168.1.6
   iPADDResS: 192.168.1.6
   port: 1000
//@Component
@Data
@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.
https://en.cdmana.com/2022/175/202206240802283389.html

Random recommended