温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用springboot怎么实现环境抽象

发布时间:2021-04-20 16:43:28 来源:亿速云 阅读:120 作者:Leah 栏目:编程语言

本篇文章为大家展示了使用springboot怎么实现环境抽象,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

springboot是什么

springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。

@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异常。

上述内容就是使用springboot怎么实现环境抽象,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI