由于之前也做过不少项目,每个项目都会要经过开发版,测试版,仿真版,正式版,每次切换环境需要修改服务器地址和一些配置信息,总结了一些,写了个配置类来切换不同环境,会方便很多,直接上代码:
ServerManager管理类:
/** * Description: 服务器地址管理类 * Copyright: Copyright (c)2017 * Company: * author: Corwin * version: 1.0 * date: 2017/4/28 11:04 * Modification History: * Date Author Version Description * ------------------------------------------------------------------ * 2017/4/28 Carry 1.0 1.0 Version */ public class ServerManager { //单例实例 public static ServerManager mInstance; //当前服务器 public static final String SERVER = ServerKey.TEST; //服务器地址接口 private IServerUrl mServer; public static class ServerKey{ /** * 开发环境 */ public static final String DEV = "dev"; /** * 测试环境 */ public static final String TEST = "test"; /** * uat环境 */ public static final String UAT = "uat"; /** * 正式环境 */ public static final String PRO = "pro"; } /** * 获取单例实例 * @return */ public static ServerManager getInstance(){ if (mInstance == null) { synchronized (ServerManager.class) { if (mInstance == null) { mInstance = new ServerManager(); } } } return mInstance; } /** * 获取环境 * @return */ public IServerUrl getServer(){ if (mServer == null) { synchronized (this) { if (mServer == null) { if(SERVER.equals(ServerKey.DEV)){ mServer = new DevServer(); }else if(SERVER.equals(ServerKey.TEST)){ mServer = new TestServer(); }else if(SERVER.equals(ServerKey.UAT)){ mServer = new UatServer(); }else if(SERVER.equals(ServerKey.PRO)){ mServer = new ProServer(); }else{ //默认开发环境 mServer = new DevServer(); } } } } return mServer; } }
IServerUrl 接口类:
/** * Description: 服务器地址接口 * Copyright: Copyright (c)2017 * Company: * author: Corwin * version: 1.0 * date: 2017/4/28 10:53 * Modification History: * Date Author Version Description * ------------------------------------------------------------------ * 2017/4/28 Carry 1.0 1.0 Version */ public interface IServerUrl { /** * 获取前台服务器地址 * @return */ String getMainUrl(); /** * 获取gis服务器地址 * @return */ String getGisUrl(); /** * 获取H5静态服务器地址 * @return */ String getFrontUrl(); /** * 是否可以更改服务器地址 * @return */ boolean getUpdateConnect(); /** * 是否打印输出日志 * @return */ boolean getDebugLog(); }
DevServer开发环境服务器地址配置类:
/** * Description: 开发环境 * Copyright: Copyright (c)2017 * Company: 广州七一一信息科技有限公司 * author: Carry * version: 1.0 * date: 2017/4/28 10:56 * Modification History: * Date Author Version Description * ------------------------------------------------------------------ * 2017/4/28 Carry 1.0 1.0 Version */ public class DevServer implements IServerUrl { @Override public String getMainUrl() { return "http://192.168.1.51:8081"; } @Override public String getGisUrl() { return "http://192.168.1.51:6061"; } @Override public String getFrontUrl() { return "http://192.168.1.51:7071"; } @Override public boolean getUpdateConnect() { return true; } @Override public boolean getDebugLog() { return true; } }
test,uat,pro同理,这样配置好了之后,每次修改服务器,只需要在public static final String SERVER = ServerKey.TEST;修改dev,uat,pro即可,每个环境的配置都写在了实体类中。完毕。