关于SpringBoot的application.yml的相关配置

关于SpringBoot的application.yml的相关配置(自定义,开发,测试,正式)切换

spring boot遵循“约定优于配置”的原则,使用annotation对一些常规的配置项做默认配置,减少或不使用xml配置,让你的项目快速运行起来。spring boot的神奇不是借助代码的生成来实现的,而是通过条件注解来实现的。

1.自定义配置,我们用yml配置我们自己的配置类:@ConfigurationProperties,

@ConfigurationProperties映射application.yml以test为前缀的配置,我就不详细介绍了。

可以参考:https://www.cnblogs.com/ginponson/p/6188432.html

在yml中的配置

 

指定端口:

指定默认启动环境

mybatis配置

mybatis在启动时可能会出现找不到的问题,我们把xml文件放到resource下的mapper中:如下

然后就是你在开发环境和测试环境想相互切换的配置

你把dev替换为test就ok了。

这里就有两种了:

1.一个是没有打包之前,我用的STS开发工具,我只想调试一个模块,而其他的模块在测试环境中,你可能没有其他模块的jar包,又不可能在本地运行,那我们可以直接在你启动的项目去切换你的环境。找到你的项目右键点击,找到OpenConfig如下:

找到Profile,将你在application.yml配置的环境填入就ok,然后启动。

2.你已经打包了,然后我想切换环境,总不可能在去把application.yml配置的环境修改在去打包吧,然后你在启动jar包时,修改你的环境,如下:

java -jar test-service.jar --spring.profiles.active=test 

下面是所有代码:

 
  1. package com.test.config;

  2.  
  3. import org.springframework.boot.context.properties.ConfigurationProperties;

  4.  
  5. import com.redis.model.TestModel;

  6.  
  7. @ConfigurationProperties(prefix = "test")

  8. public class TestConfig{

  9.  
  10.  
  11. private String name;

  12.  
  13. private String password;

  14.  
  15. private TestModel testmodel;

  16.  
  17. public String getName() {

  18. return name;

  19. }

  20. public void setName(String name) {

  21. this.name = name;

  22. }

  23. public String getPassword() {

  24. return password;

  25. }

  26. public void setPassword(String password) {

  27. this.password = password;

  28. }

  29. public TestModel getTestmodel() {

  30. return testmodel;

  31. }

  32. public void setTestmodel(TestModel testmodel) {

  33. this.testmodel = testmodel;

  34. }

  35. }

 
  1. package com.test.model;

  2.  
  3. public class TestModel {

  4.  
  5. private Long id;

  6.  
  7. private String hostPort;

  8.  
  9. public Long getId() {

  10. return id;

  11. }

  12. public void setId(Long id) {

  13. this.id = id;

  14. }

  15. public String getHostPort() {

  16. return hostPort;

  17. }

  18. public void setHostPort(String hostPort) {

  19. this.hostPort = hostPort;

  20. }

  21.  
  22. }

application.yml配置

 
  1. # 默认的profile为dev,其他环境通过指定启动参数使用不同的profile,比如:

  2. # 测试环境:java -jar test-service.jar --spring.profiles.active=test

  3. # 生产环境:java -jar test-service.jar --spring.profiles.active=prod

  4. server:

  5. port: 9005 #指定启动端口号

  6.  
  7. spring:

  8. application:

  9. name: test-service

  10. profiles:

  11. active: dev #默认环境(开发环境)

  12.  
  13. datasource:

  14. type: com.alibaba.druid.pool.DruidDataSource #这里是配置druid连接池,以下都是druid的配置信息

  15. url: jdbc:mysql://192.168.1.209:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false

  16. driver-class-name: com.mysql.jdbc.Driver

  17. username: root

  18. password: root

  19.  
  20. mybatis:

  21. mapper-locations: classpath*:/mapper/**Mapper.xml #把xml文件放在com.XX.mapper.*中可能会出现找不到的问题,这里把他放在resource下的mapper中

  22. typeAliasesPackage: com.test.domain #这里是实体类的位置,#实体扫描,多个package用逗号或者分号分隔

  23. configuration:

  24. map-underscore-to-camel-case: true

  25. cache-enabled: false

  26.  
  27. logging:

  28. file: test-service.log

  29. level:

  30. com.test: debug

  31.  
  32. #自己定义的配置

  33. test:

  34. name:

  35. password:

  36. testmodel:

  37. id: 1

  38. host-port: 127.0.0.1:8080

  39.  
  40. --- #注意前面有短横线

  41. #测试环境###########################################################################################################################################

  42. spring:

  43. application:

  44. name: test-service

  45. profiles: test #测试环境

  46.  
  47. datasource:

  48. type: com.alibaba.druid.pool.DruidDataSource #这里是配置druid连接池,以下都是druid的配置信息

  49. url: jdbc:mysql://192.168.1.209:3306/test?useUnicode=true&characterEncoding=utf-8

  50. driver-class-name: com.mysql.jdbc.Driver

  51. username: root

  52. password: root

  53.  
  54. mybatis:

  55. mapper-locations: classpath*:/mapper/**Mapper.xml #把xml文件放在com.XX.mapper.*中可能会出现找到的问题,这里把他放在resource下的mapper中

  56. typeAliasesPackage: com.test.domain #这里是实体类的位置,#实体扫描,多个package用逗号或者分号分隔

  57. configuration:

  58. map-underscore-to-camel-case: true

  59. cache-enabled: false

  60.  
  61. logging:

  62. file: test-service.log

  63. level:

  64. com.test: debug

  65.  
  66. --- #注意前面的短横线

  67. #正式环境###########################################################################################################################################

  68. spring:

  69. application:

  70. name: test-service

  71. profiles: prod

  72.  
  73. datasource:

  74. type: com.alibaba.druid.pool.DruidDataSource

  75. url: jdbc:mysql://192.168.1.209:3306/test?useUnicode=true&characterEncoding=utf-8

  76. driver-class-name: com.mysql.jdbc.Driver

  77. username: test

  78. password: test

  79.  
  80. mybatis:

  81. mapper-locations: classpath*:/mapper/**Mapper.xml

  82. typeAliasesPackage: com.test.domain

  83. configuration:

  84. map-underscore-to-camel-case: true

  85. cache-enabled: false

  86.  
  87. logging:

  88. file: test-service.log

  89. level:

  90. com.test: debug

SpringBoot

猜你喜欢

转载自blog.csdn.net/wang_j_p/article/details/113099203
今日推荐