Spring Boot was supposedly created to simplify spring’s cumbersome XML configuration, promoting the idea of “convention over configuration.” It wasn’t built.

Take the configuration file (*.properties) in Spring Boot for example.

1. All *.properties files are treated as configuration files

No discrimination. Properties or application.yml (application.properties is said to take precedence over Application.yml), For example, if I create a web.properties in the same directory, the configuration items in it can also be read without special declaration.



Read code:

@Configuration
@PropertySource(value = "classpath:webconfig.properties",encoding = "UTF-8")// This sentence can not be written
@ConfigurationProperties("web")
public class WebConfig {
    private String pubzy;// Public static repository
    public String getPubzy(a) {
        return pubzy;
    }
    public void setPubzy(String pubzy) {
        this.pubzy = pubzy;
    }
    ……
}
Copy the code

2. Hump naming rules

It’s amazing how the various configuration items in a configuration file can be recognized no matter how they are named, whether they are camel humps, underscore separations, or minus links.

For example, a configuration item in the configuration file can be written in the following three ways (the last two notes), the system can recognize, can be bound to the property in the code.

Configuration file web.properties:

Cas. The server url -- prefix = http://192.168.0.22:9080/cas248 # cas. Server_url_prefix = http://192.168.0.22:9080/cas248 # cas. ServerUrlPrefix = http://192.168.0.22:9080/cas248Copy the code

Code:

@Configuration
@PropertySource(value = "classpath:webconfig.properties",encoding = "UTF-8")
@ConfigurationProperties("cas")
public class CasConfig {

    private String serverUrlPrefix;// Can accurately point to the above three configuration items

    public String getServerUrlPrefix(a) {
        return serverUrlPrefix;
    }

    public void setServerUrlPrefix(String serverUrlPrefix) {
        this.serverUrlPrefix = serverUrlPrefix; }}Copy the code