在springboot启动是可以使用加载自定义的yml配置文件:
1.使用原理
Environment在应用程序上下文刷新之前使用自定义EnvironmentPostProcessor,每个实现都应该在 中注册META-INF/spring.factories
示例:org.springframework.boot.env.EnvironmentPostProcessor=com.example.YourEnvironmentPostProcessor
2.使用方式
自定义处理类MyEnvironmentPostProcessor实现EnvironmentPostProcessor接口
resources目录下创建config/MyEnvironmentPostProcessor.yml文件,并定义参数school值为北京大学
resources目录下创建META-INF/spring.factories,并将自定义的MyEnvironmentPostProcessor添加到里面
在springboot启动时就会将自定义的MyEnvironmentPostProcessor.yml加载到Environment中
3.测试
springboot启动日志:
获取自定义的值: