Priority SpringBoot profile

Disclaimer: This article is a blogger original article, reproduced, please attach Bowen link! https://blog.csdn.net/Little_fxc/article/details/88717997

Priority SpringBoot profile

Project structure

Here Insert Picture Description

Priority profile

application.properties and application.yml file by priority in descending order at the following four positions:

  1. file: ./ config / (current project path config directory);
  2. file: ./ (under the current project path);
  3. classpath: / config / (classpath directory config);
  4. classpath: / (classpath config).

Here Insert Picture Description

Source code shows:

public class ConfigFileApplicationListener
		implements EnvironmentPostProcessor, SmartApplicationListener, Ordered {
// Note the order is from least to most specific (last one wins)
private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/";

// 省略其它代码
}

To port configuration, for example

  • In the resources / config file directory configuration is provided to the port 8888;
  • In the resources / settings file directory configuration port 8080;
  • In the config directory path the class profile is set to 6666 ports;
  • In class path profile is set to 5555 ports;

operation result:

Here Insert Picture Description

Custom binding profile

  1. CustomizedFile 类

    /**
     * 自定义配置文件, 需要配合使用后@Configuration和@PropertySource("classpath:customized-file.properties")来指定
     * @author fengxuechao
     */
    @Configuration
    @ConfigurationProperties(prefix = "customizedFile")
    @PropertySource("classpath:customized-file-${spring.profiles.active}.properties")
    public class CustomizedFile {
        private String name;
        private String author;
        private String path;
        private String description;
        // 省略 setter/getter
    }
    

    See ${spring.profiles.active}, you must know the smart way to achieve this is spring boot environment and more custom configuration files.
    Effective configuration file is ${spring.profiles.active}specified in the configuration file, the entry into force of the case herein is customized-file-dev.properties.
    Then continue to create a profile verification

  2. customized-file.properties

    customizedFile.name=自定义配置文件名
    customizedFile.author=作者名
    customizedFile.path=路径地址
    customizedFile.description=看到这个就表明自定义配置文件成功了
    
  3. customized-file-dev.properties

    customizedFile.description=DEV:看到这个就表明自定义配置文件成功了
    
  4. operation result:

    Here Insert Picture Description

    Conclusion: Only customized-file-dev.propertiesconfigured in property to take effect

Guess you like

Origin blog.csdn.net/Little_fxc/article/details/88717997