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
Priority profile
application.properties and application.yml file by priority in descending order at the following four positions:
- file: ./ config / (current project path config directory);
- file: ./ (under the current project path);
- classpath: / config / (classpath directory config);
- classpath: / (classpath config).
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:
Custom binding profile
-
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 iscustomized-file-dev.properties
.
Then continue to create a profile verification -
customized-file.properties
customizedFile.name=自定义配置文件名 customizedFile.author=作者名 customizedFile.path=路径地址 customizedFile.description=看到这个就表明自定义配置文件成功了
-
customized-file-dev.properties
customizedFile.description=DEV:看到这个就表明自定义配置文件成功了
-
operation result:
Conclusion: Only
customized-file-dev.properties
configured in property to take effect