1. 程式人生 > >關於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 

下面是所有程式碼:

package com.test.config;

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

import com.redis.model.TestModel;

@ConfigurationProperties(prefix = "test")
public class TestConfig{
	
	
	private String name;
	
	private String password;
	
	private TestModel testmodel;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public TestModel getTestmodel() {
		return testmodel;
	}
	public void setTestmodel(TestModel testmodel) {
		this.testmodel = testmodel;
	}
}
package com.test.model;

public class TestModel {
	
	private Long id;
	
	private String hostPort;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getHostPort() {
		return hostPort;
	}
	public void setHostPort(String hostPort) {
		this.hostPort = hostPort;
	}
	
}

application.yml配置

# 預設的profile為dev,其他環境通過指定啟動引數使用不同的profile,比如:  
#   測試環境:java -jar test-service.jar --spring.profiles.active=test  
#   生產環境:java -jar test-service.jar --spring.profiles.active=prod
server:
    port: 9005 #指定啟動埠號
    
spring:  
  application:
    name: test-service
  profiles: 
    active: dev #預設環境(開發環境)
    
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource   #這裡是配置druid連線池,以下都是druid的配置資訊
    url: jdbc:mysql://192.168.1.209:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root
    
mybatis:
  mapper-locations: classpath*:/mapper/**Mapper.xml    #把xml檔案放在com.XX.mapper.*中可能會出現找不到的問題,這裡把他放在resource下的mapper中
  typeAliasesPackage: com.test.domain        #這裡是實體類的位置,#實體掃描,多個package用逗號或者分號分隔
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false     

logging:
  file: test-service.log
  level:
    com.test: debug 

#自己定義的配置
test:
  name: 
  password:   
  testmodel:
    id: 1
    host-port: 127.0.0.1:8080
    
--- #注意前面有短橫線
#測試環境###########################################################################################################################################
spring:  
  application:
    name: test-service
  profiles: test #測試環境
  
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource   #這裡是配置druid連線池,以下都是druid的配置資訊
    url: jdbc:mysql://192.168.1.209:3306/test?useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root
    
mybatis:
  mapper-locations: classpath*:/mapper/**Mapper.xml    #把xml檔案放在com.XX.mapper.*中可能會出現找到的問題,這裡把他放在resource下的mapper中
  typeAliasesPackage: com.test.domain         #這裡是實體類的位置,#實體掃描,多個package用逗號或者分號分隔
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false

logging:
  file: test-service.log
  level:
    com.test: debug 
  
--- #注意前面的短橫線
#正式環境###########################################################################################################################################
spring:  
  application:
    name: test-service
  profiles: prod
    
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://192.168.1.209:3306/test?useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.jdbc.Driver
    username: test
    password: test

mybatis:
  mapper-locations: classpath*:/mapper/**Mapper.xml
  typeAliasesPackage: com.test.domain
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false

logging:
  file: test-service.log
  level:
    com.test: debug 
SpringBoot