1前言
- 在springboot项目中根据配置文件中配置的环境不同获取不一样的值
2实战代码
2.1 配置文件中的配置书写
#机器人日志url 运行环境 1-测试 2-正式
system.environmental = 1
robot.test.url = https://ip+port/t_Hardware/robotManagement/fileUpload
robot.online.url = https://ip+port/hardware/robotManagement/fileUpload
2.2代码编写
package com.mgj.face.machine.provider.configuration;
import com.mgj.face.machine.client.constant.CodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author lYS
* @date 2020/7/20 0020
*/
@Slf4j
@Component
public class RobotConfig implements InitializingBean { //实现InitializingBean接口
/**
* 运行环境
*/
public static int SYS_ENVION;
/**
* ROBOT-测试domain
*/
public static String ROBOT_TEST_DOMAIN_URL;
/**
* ROBOT-正式domain
*/
public static String ROBOT_ONLINE_DOMAIN_URL;
@Value("${system.environmental}")
private int sysEnvion;
@Value("${robot.test.url}")
private String robotTesDomaintUrl;
@Value("${robot.online.url}")
private String robotOnlineDomainUrl;
//这个方法将在所有的属性被初始化后调用。
@Override
public void afterPropertiesSet() throws Exception {
SYS_ENVION = this.sysEnvion;
ROBOT_TEST_DOMAIN_URL = this.robotTesDomaintUrl;
ROBOT_ONLINE_DOMAIN_URL = this.robotOnlineDomainUrl;
log.info("RobotConfig初始化参数{}{}{}",SYS_ENVION,ROBOT_TEST_DOMAIN_URL,ROBOT_ONLINE_DOMAIN_URL);
}
/**
*
*其他地方调用此方法,根据配置文件配置的运行环境不同返回不一样的值
* @return
*/
public static String getRobotUrl() {
//1测试环境
if (CodeConstants.SYS_ENVIRONMENTAL_1 == SYS_ENVION) {
return ROBOT_TEST_DOMAIN_URL;
} else {
return ROBOT_ONLINE_DOMAIN_URL;
}
}
}
- 完结