欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot获取当前运行环境(1)[email protected]

程序员文章站 2022-05-08 09:58:37
...

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);
        }
    }
}

 

相关标签: springboot