springboot学习(一):多环境配置

说明

      之前通过翟永超的博客对springboot的基本用法进行了学习,现在在这里总结回顾下,加深印象。博客所涉及的功能知识点都来自于翟永超的博客,这里只总结了我认为比较重要的以及结合了平时使用的经验。

正文

      在平时工作中,一个项目最基本的会有三种配置,开发dev,测试test,生产 prod。每个环境下的配置文件的内容都会不同,所以需要在不同环境下使用不同的配置文件。
      这里,使用了四个配置文件,一主三从,所谓一主就是在一个配置文件中,配置通用的内容,如spring.application.name等属性,该配置文件为创建项目时的默认配置文件,名称为application.properties 其他三从配置文件,则是在不同环境下有差异化的属性,文件名称的命令需满足application-{profile}.properties,其中{profile}为不同环境的标识,如
application-dev.properties dev是开发环境
application-test.properties test是测试环境
application-prod.properties prod是生产环境
在使用时可以通过在主配置文件application.properties中,使用spring.profiles.active进行指定

不同配置文件

application.properties,这里默认指定了使用开发环境的配置文件

spring.application.name=multiple_envrionment_test

spring.profiles.active=dev

在配置文件中为不同的环境指定端口
application-dev.properties

server.port=1000

application-test.properties

server.port=1002

application-prod.properties

server.port=1001

运行结果

通过java -jar xxx.jar –spring.profiles.active=test 命令启动项目使用不同的配置文件
这里写图片描述
这里写图片描述

这里写图片描述
这里写图片描述

这里写图片描述
这里写图片描述

之前一直使用SPRING INITIALIZR页面工具创建springboot项目,再一次创建spirngboot项目实现多环境配置时,使用了Intellij中的Spring Initialize工具构建项目,但是在运行时没有正常启动,报以下异常:Unregistering JMX-exposed beans on shutdown

2018-07-15 21:06:35.633  INFO 14324 --- [           main] com.example.demo.MulEnvApplication       : Starting MulEnvApplication on WDS with PID 14324 (C:\Users\wds\Desktop\springboot_study\mult_env\target\classes started by wds in C:\Users\wds\Desktop\springboot_study\mult_env)
2018-07-15 21:06:35.636  INFO 14324 --- [           main] com.example.demo.MulEnvApplication       : The following profiles are active: dev
2018-07-15 21:06:35.690  INFO 14324 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@641147d0: startup date [Sun Jul 15 21:06:35 CST 2018]; root of context hierarchy
2018-07-15 21:06:36.461  INFO 14324 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-07-15 21:06:36.483  INFO 14324 --- [           main] com.example.demo.MulEnvApplication       : Started MulEnvApplication in 1.197 seconds (JVM running for 2.478)
2018-07-15 21:06:36.484  INFO 14324 --- [       Thread-5] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@641147d0: startup date [Sun Jul 15 21:06:35 CST 2018]; root of context hierarchy
2018-07-15 21:06:36.486  INFO 14324 --- [       Thread-5] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

经查资料,发现是没有引入web依赖,没有tomcat导致该异常。解决方法:在pom.xml文件中加入以下依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

在公司开发中,会有多种配置文件,如log4j2,mq,mongodb.redis,db等等不同的配置文件,每种配置文件在不同环境下的配置数据也不尽相同,所以就需要配置好不同环境下的数据,在使用时直接粘贴替换。
比如,项目中有resoures包,这里是项目真正使用的配置文件,在不同环境下可以进行替换。可以再创建一个resource文件夹,其中存放dev,test,prod三个环境下的配置文件,项目打包时直接替换。
这里写图片描述

参考资料:
http://blog.didispace.com/spring-initializr-in-intellij/
http://blog.didispace.com/springbootproperties/
https://www.cnblogs.com/winner-0715/p/6754994.html

猜你喜欢

转载自blog.csdn.net/sinat_36553913/article/details/81058099