spring boot环境抽象

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

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

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

@Profile

基于Java类的环境配置

@Profile注解能够用来标注@Configuration注解的类。表示该特定环境下激活该类下的全部bean。固然也能够专门用来标注@Bean,由于许多时候本地环境和Production环境的区别只是数据源不一样罢了。bash

@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。post

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都不会被应用。spa

属性源抽象

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

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的两个系统变量以及环境变量。code

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

具体的说,当使用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异常。

相关文章
相关标签/搜索