当前位置:   article > 正文

关于SpringBoot的application.yml的相关配置(自定义,开发,测试,正式)切换_yml 如何控制测试配置和主配置的切换

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. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import com.redis.model.TestModel;
  4. @ConfigurationProperties(prefix = "test")
  5. public class TestConfig{
  6. private String name;
  7. private String password;
  8. private TestModel testmodel;
  9. public String getName() {
  10. return name;
  11. }
  12. public void setName(String name) {
  13. this.name = name;
  14. }
  15. public String getPassword() {
  16. return password;
  17. }
  18. public void setPassword(String password) {
  19. this.password = password;
  20. }
  21. public TestModel getTestmodel() {
  22. return testmodel;
  23. }
  24. public void setTestmodel(TestModel testmodel) {
  25. this.testmodel = testmodel;
  26. }
  27. }
  1. package com.test.model;
  2. public class TestModel {
  3. private Long id;
  4. private String hostPort;
  5. public Long getId() {
  6. return id;
  7. }
  8. public void setId(Long id) {
  9. this.id = id;
  10. }
  11. public String getHostPort() {
  12. return hostPort;
  13. }
  14. public void setHostPort(String hostPort) {
  15. this.hostPort = hostPort;
  16. }
  17. }

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. spring:
  7. application:
  8. name: test-service
  9. profiles:
  10. active: dev #默认环境(开发环境)
  11. datasource:
  12. type: com.alibaba.druid.pool.DruidDataSource #这里是配置druid连接池,以下都是druid的配置信息
  13. url: jdbc:mysql://192.168.1.209:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
  14. driver-class-name: com.mysql.jdbc.Driver
  15. username: root
  16. password: root
  17. mybatis:
  18. mapper-locations: classpath*:/mapper/**Mapper.xml #把xml文件放在com.XX.mapper.*中可能会出现找不到的问题,这里把他放在resource下的mapper中
  19. typeAliasesPackage: com.test.domain #这里是实体类的位置,#实体扫描,多个package用逗号或者分号分隔
  20. configuration:
  21. map-underscore-to-camel-case: true
  22. cache-enabled: false
  23. logging:
  24. file: test-service.log
  25. level:
  26. com.test: debug
  27. #自己定义的配置
  28. test:
  29. name:
  30. password:
  31. testmodel:
  32. id: 1
  33. host-port: 127.0.0.1:8080
  34. --- #注意前面有短横线
  35. #测试环境###########################################################################################################################################
  36. spring:
  37. application:
  38. name: test-service
  39. profiles: test #测试环境
  40. datasource:
  41. type: com.alibaba.druid.pool.DruidDataSource #这里是配置druid连接池,以下都是druid的配置信息
  42. url: jdbc:mysql://192.168.1.209:3306/test?useUnicode=true&characterEncoding=utf-8
  43. driver-class-name: com.mysql.jdbc.Driver
  44. username: root
  45. password: root
  46. mybatis:
  47. mapper-locations: classpath*:/mapper/**Mapper.xml #把xml文件放在com.XX.mapper.*中可能会出现找到的问题,这里把他放在resource下的mapper中
  48. typeAliasesPackage: com.test.domain #这里是实体类的位置,#实体扫描,多个package用逗号或者分号分隔
  49. configuration:
  50. map-underscore-to-camel-case: true
  51. cache-enabled: false
  52. logging:
  53. file: test-service.log
  54. level:
  55. com.test: debug
  56. --- #注意前面的短横线
  57. #正式环境###########################################################################################################################################
  58. spring:
  59. application:
  60. name: test-service
  61. profiles: prod
  62. datasource:
  63. type: com.alibaba.druid.pool.DruidDataSource
  64. url: jdbc:mysql://192.168.1.209:3306/test?useUnicode=true&characterEncoding=utf-8
  65. driver-class-name: com.mysql.jdbc.Driver
  66. username: test
  67. password: test
  68. mybatis:
  69. mapper-locations: classpath*:/mapper/**Mapper.xml
  70. typeAliasesPackage: com.test.domain
  71. configuration:
  72. map-underscore-to-camel-case: true
  73. cache-enabled: false
  74. logging:
  75. file: test-service.log
  76. level:
  77. com.test: debug

SpringBoot

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号