SpringBoot profiles
点击上方蓝色字体,选择“标星公众号”
优质文章,第一时间送达
作者 | 硬石头
来源 | urlify.cn/uQbQFr
SpringBoot 提供了profiles,可以实现在不同环境下应用程序配置的隔离性。
application.yaml
spring:
profiles:
active: prod
application:
appId: app01
application-dev.yaml
env:
name: 'this is dev'
application-test.yaml
env:
name: 'this is test'
application-prod.yaml
env:
name: 'this is prod'
Runner.java
package com.stone.springboot.study;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
@Component
public class Runner implements ApplicationRunner {
@Autowired
Environment environment;
@Override
public void run(ApplicationArguments args) throws Exception {
String[] activeProfiles = environment.getActiveProfiles();
System.out.println("actvie profiles->"+ StringUtils.arrayToDelimitedString(activeProfiles, ","));
String property = environment.getProperty("env.name");
System.out.println("env.name->"+property);
String property1 = environment.getProperty("application.appId");
System.out.println("application.appId->"+property1);
}
}
输出结果:
actvie profiles->prod
env.name->this is prod
application.appId->app01
方式一:application.yaml中 定义spring.profiles.active
修改application.yaml
spring:
profiles:
active: test
application:
appId: app01
输出结果:
actvie profiles->test
env.name->this is test
application.appId->app01
方式二:命令行参数 --spring.profiles.active=prod
输出如下:
actvie profiles->prod
env.name->this is prod
application.appId->app01
说明:命令行属性优先级高与application.yaml中定义的属性
方式三:编码形式 SpringApplication.setAdditionalProfiles();
Application.java
package com.stone.springboot.study;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.setAdditionalProfiles("my");
app.run(args);
}
}
application-my.yaml
my:
name: '9wan'
Runner.java
package com.stone.springboot.study;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
@Component
public class Runner implements ApplicationRunner {
@Autowired
Environment environment;
@Override
public void run(ApplicationArguments args) throws Exception {
String[] activeProfiles = environment.getActiveProfiles();
System.out.println("actvie profiles->"+ StringUtils.arrayToDelimitedString(activeProfiles, ","));
String property = environment.getProperty("env.name");
System.out.println("env.name->"+property);
String property1 = environment.getProperty("application.appId");
System.out.println("application.appId->"+property1);
String property2 = environment.getProperty("my.name");
System.out.println("my.name->"+property2);
}
}
输出结果:
actvie profiles->my,prod
env.name->this is prod
application.appId->app01
my.name->9wan
说明:SpringApplication.setAdditionalProfiles("my") 添加的profile会自动设置为激活状态并加载
总结
SpringBoot 提供profiles可进行多环境配置,再通过spring.profiles.actvife来激活特定配置。在多环境下节省了配置修改的成本。方式一可以把不同环境下相同的属性配置放到application.yaml,避免了重复定义; 个人也更推荐这种配置方式。
关于application-{profile}.yaml或properties文件命名,这个是SpringBoot约定的这种格式。当没有激活的配置时,默认会加载application-default文件
粉丝福利:Java从入门到入土学习路线图
👇👇👇
👆长按上方微信二维码 2 秒
感谢点赞支持下哈