使用EnableAutoConfiguration加载外部的配置文件
创建配置类
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE) //设置装配bean优先级
public class ActiveMysqlProfileConfig implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
environment.addActiveProfile("mysql"); //对应配置文件application-mysql.yml
}
}
自动装配(让其他模块能使用)
在resources/META-INF
文件夹下创建spring.factories
文件,并加入一下代码
org.springframework.boot.env.EnvironmentPostProcessor=\
pineux.common.config.ActiveMysqlProfileConfig
在springboot3中,MybatisPlusConfig
配置会失效,解决办法参考在SpringBoot3中spring.factories配置不起作用问题