springboot获取当前运行环境(1)-@Profile

1、通过@Profile

1.1、环境切换

@Profile标识在类上,那么只有当前环境匹配,整个配置类才会生效
@Profile标识在Bean上 ,那么只有当前环境的Bean才会被激活
没有标志为@Profile的bean 不管在什么环境都可以被激活 

 激活切换环境的方法

  1. 通过运行时jvm参数来切换 -Dspring.profiles.active=test | dev | prod
  2. 通过代码的方式来激活
  3. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
    context.getEnvironment().setActiveProfiles("test","dev");
    

     

1.2、应用演示一

  • 新建DemoBean

public class DemoBean {
    public String content;
 
    public DemoBean(String content) {
        super();
        this.content = content;
    }
    public String getContent(){
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
}
  • 配置组件
@Configuration
public class ProfileConfig {
 
    @Bean
    @Profile("dev")//Profile为dev时实例化devDemoBean
    public DemoBean devDemoBean(){
        return new DemoBean("from development profile");
    }
 
    @Bean
    @Profile("prod")//Profile为prod时实例化prodDemoBean
    public DemoBean prodDemoBean(){
        return new DemoBean("from production profile");
    }
}
  • 运行
public class Main {
 
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.getEnvironment().setActiveProfiles("prod");//先将活动的Proofile设置为prod
        context.register(ProfileConfig.class);//后置注册Bean的配置类,不然会报Bean未定义的错误
        context.refresh();//刷新容器
 
        DemoBean demoBean = context.getBean(DemoBean.class);
 
        System.out.println(demoBean.getContent());
 
        context.close();
    }

}

 

1.3、应用演示二

通过加载类获取当前属性

  • 新建加载类
public class Environment implements Serializable {
    private static final long serialVersionUID = -2794574965009461938L;
 
    private String envName = "dev";
 
    public Environment() {
    }
 
    public Environment(String envName) {
        this.envName = envName;
    }
 
    public String getEnvName() {
        return envName;
    }
 
    public void setEnvName(String envName) {
        this.envName = envName;
    }
 
    @Override
    public String toString() {
        return "Environment{" +
                "envName='" + envName + '\'' +
                '}';
    }
}
  • 配置组件
//配置类
@Configuration
public class ProfileConfig {
 
    @Profile("dev")
    @Bean
    public Environment dev(){
        return new Environment();
    }
 
    @Profile("test")
    @Bean
    public Environment test(){
        return new Environment("test");
    }
 
    @Profile("prod")
    @Bean
    public Environment prod(){
        return new Environment("prod");
    }
 
    @Profile("default")
    @Bean("default")
    public Environment defau(){
        return new Environment("default");
    }
}
  • 测试
public class ProfileTest {
 
    /**
     *  1、使用命令行动态参数: 在虚拟机参数位置加载 -Dspring.profiles.active=test
     *  2、代码的方式激活某种环境;
     */
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext() ;
        //1、创建一个applicationContext
        //2、设置需要激活的环境
        applicationContext.getEnvironment().setActiveProfiles("test");//若将该行注释,则可以看到注入到环境的env为default
        //3、注册主配置类
        applicationContext.register(ProfileConfig.class);
        //4、启动刷新容器
        applicationContext.refresh();
       String[] environments = applicationContext.getBeanNamesForType(Environment.class);
        for (String name : environments) {
            System.out.println(name);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值