spring boot环境抽象

753 阅读3分钟

在实际开发中,开发人员在编写springboot的时候通常要在本地环境测试然后再部署到Production环境,这两种环境一般来讲是不同的,最主要的区别就是数据源的不同。

在应用环境中,集成在容器的抽象环境模型有两个方面:profiles和properties。只有给出的profile被激活,一组逻辑命名的bean定义才会在容器中注册。

环境变量对象角色和profiles的关系来决定哪个profiles(如果有)处于当前激活状态,哪个profiles默认被激活。

@Profile

基于Java类的环境配置

@Profile注解可以用来标注@Configuration注解的类。表示该特定环境下激活该类下的所有bean。当然也可以专门用来标注@Bean,因为许多时候本地环境和Production环境的区别只是数据源不同罢了。

@Configuration
public class ProfileConf {


    @Bean
    @Profile("dev")
    public UserInfo devUserInfo() {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(1);
        userInfo.setName("dev");
        return userInfo;
    }

    @Bean
    @Profile("production")
    public UserInfo productionUserInfo() {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(1);
        userInfo.setName("production");
        return userInfo;
    }

}

激活profile

现在我们已经更新了我们的配置,我们仍然需要说明哪个profile是激活的。如果直接注册@Configuration标注的类,这将会看到一个NoSuchBeanDefinitionException被抛出,因为容器找不到一个对应的环境下的bean。

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.getEnvironment().setActiveProfiles("dev");
        context.register(UserConf.class);
        context.refresh();
        System.out.println(context.getBean(UserInfo.class));
    }

默认的profile

默认配置文件表示默认启用的配置文件。

    @Bean
    @Profile("default")
    public UserInfo defaultUserInfo() {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(1);
        userInfo.setName("default");
        return userInfo;
    }

如果没有profile是激活状态,上面的bean将会被创建;这种方式可以被看做是对一个或者多个bean提供了一种默认的定义方式。如果启用任何的profile,那么默认的profile都不会被应用。

属性源抽象

Spring 环境抽象提供了可配置的属性源层次结构的搜索操作。

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
//        context.getEnvironment().setActiveProfiles("dev");
        context.getEnvironment().setActiveProfiles("dev");
        context.register(ProfileConf.class);
        context.refresh();
        ConfigurableEnvironment environment = context.getEnvironment();
        Map<String, Object> maps = environment.getSystemProperties();
        maps.keySet().forEach(k -> System.out.println(k + "->" + maps.get(k)));

        System.out.println("===========================");
        Map<String, Object> environment1 = environment.getSystemEnvironment();
        environment1.keySet().forEach(k -> System.out.println(k + "->" + environment1.get(k)));

        System.out.println(environment.containsProperty("java.vm.version"));
    }

在上面的例子中可以获取Environment的两个系统变量以及环境变量。

一个PropertySource是对任何key-value资源的简单抽象,并且Spring 的标准环境是由两个PropertySource配置的,一个表示一系列的JVM 系统属性(System.getProperties()),一个表示一系列的系统环境变量(System.getenv())。

具体的说,当使用StandardEnvironment时,如果在运行时系统属性或者环境变量中包括foo,那么调用env.containsProperty(“java.vm.version”)方法将会返回true。

更重要的是,整个机制都是可配置的。也许你有个自定义的属性来源,你想把它集成到这个搜索里面。这也没问题,只需简单的实现和实例化自己的PropertySource,并把它添加到当前环境的PropertySources集合中:

    ConfigurableApplicationContext ctx = new GenericApplicationContext();
    MutablePropertySources sources = ctx.getEnvironment().getPropertySources();
    sources.addFirst(new MyPropertySource());

@PropertySource

上一篇文章讲到,基于Java的配置很多时候会和xml混合使用。其中@Import还可以导入其他Java配置类,这里要说的@PropertySource注解表示导入.properties文件。

@Configuration
@PropertySource("classpath:user.properties")
public class UserConf {

    @Autowired
    Environment environment;

    @Bean
    //每次调用就创建一个新的bean
    @Scope("prototype")
    public UserInfo userInfo() {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(Integer.valueOf(environment.getProperty("user.id")));
        System.out.println(environment.getProperty("user.name"));
        userInfo.setName(environment.getProperty("user.name"));
        return userInfo;
    }

}
user.id=11
user.name=asdasd

任何出现在@PropertySource中的资源位置占位符都会被注册在环境变量中的资源解析。

假设”user.name”已经在其中的一个资源中被注册,例如:系统属性或环境变量,占位符将会被正确的值解析。

如果没有,”default/path”将会使用默认值。如果没有默认值,而且无法解释属性,则抛出IllegalArgumentException异常。