微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

spring boot环境抽象的实现方法

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

在实际开发中,开发人员在编写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 maps = environment.getSystemProperties(); maps.keySet().forEach(k -> System.out.println(k + "->" + maps.get(k))); System.out.println("==========================="); Map 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异常。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐