近期看到前同事写的项目,有关geoserver的许多配置写死在代码中,每次更改环境的时候都需要去修改代码,很麻烦,所以我就做了一下优化,将我解决的办法记录一下
1、修改之前的代码
public class MapConstant {
public static class Status{
//否
public static final int DISABLE = 0;
//是
public static final int ENABLE = 1;
public static final String ESPG4326="latLonBoundingBox";
public static final String ESPG21481="nativeBoundingBox";
//Geoserver服务相关参数
public static final String RESTURLKey="gsmgr_resturl";
public static final String RESTUSERKey="gsmgr_restuser";
public static final String RESTUSERValue="admin";
public static final String RESTPWKey="gsmgr_restpw";
public static final String RESTPWValue="geoserver";
public static final String GS_VERSIONKey="gsmgr_version";
public static final String GS_VERSIONValue="2.15.1";
public static final String GEO_SERVER_PATH="/application/geoserver/apache-tomcat-7.0.42/webapps/geoserver/data/workspaces";
//geoserver相关信息
public static final String BUNDLE_URL ="http://192.168.1.75:18080/geoserver/";
public static final String USERNAME="admin";
public static final String PASSWD="geoserver";
//postgres 数据库链接参数
public final static Map DATABASEPARAMS= new HashMap() {
{
put("dbtype", "postgis" );
put("host","192.168.1.75");
put("port", 5432);
put("database","map");
put("user", "postgres");
put("passwd", "root");//test:root,pro:tckj80@80
}
};
}
}
这是修改前的代码,里面配置了geoserver和postgis相关信息,其他地方引用,导致切换很不方便
2、修改
①第一步,在application.yml配置文件中配置相关信息
server:
geoserver-path: E:/geoserver/apache-tomcat-7.0.42/webapps/geoserver/data/workspaces
geoserver-url: http://192.168.1.75:18080/geoserver/
geoserver-user: admin
geoserver-password: geoserver
postgis:
dbtype: postgis
host: 192.168.1.75
port: 5432
database: map
schema: public
user: postgres
passwd: root
②第二步,修改MapConstant 类
@Value("${server.geoserver-url}")
public static String BUNDLE_URL;
@Value("${server.geoserver-user}")
public static String USERNAME;
@Value("${server.geoserver-password}")
public static String PASSWD;
注意:这是第一次改的,这边出现一个坑,由于变量都被static修饰,而只有普通变量才可以从配置文件获取信息,所以这样写取出来的都是null
由于这个类里面配置信息在多出用到,现在如果把static去掉,那么就要修改其他代码,代价过大,所以我通过其他方式对静态变量赋值
我在此介绍两种方式
第一种,修改MapConstant 类
@Configuration
@Data
public class MapConstant {
//需要配置的项
public static String BUNDLE_URL ;
public static String USERNAME;
public static String PASSWD;
public static String database ;
public static String db_host;
public static String db_port;
public static String db_type ;
public static String db_user;
public static String db_schema;
public static String db_password;
public static String GEO_SERVER_PATH;
//设置geoserver信息
@Value("${server.geoserver-url}")
public void setBundleUrl(String geoserverUrl) {
BUNDLE_URL = geoserverUrl;
}
@Value("${server.geoserver-user}")
public void setUSERNAME(String geoserverUser) {
MapConstant.USERNAME = geoserverUser;
}
@Value("${server.geoserver-password}")
public void setPASSWD(String geoserverPassword) {
MapConstant.PASSWD = geoserverPassword;
}
//设置postgis数据库信息
@Value("${postgis.schema}")
public void setDb_schema(String db_schema) {
MapConstant.db_schema = db_schema;
}
@Value("${postgis.database}")
public void setDatabase(String database) {
MapConstant.database = database;
}
@Value("${postgis.host}")
public void setDb_host(String db_host) {
MapConstant.db_host = db_host;
}
@Value("${postgis.port}")
public void setDb_port(String db_port) {
MapConstant.db_port = db_port;
}
@Value("${postgis.dbtype}")
public void setDb_type(String db_type) {
MapConstant.db_type = db_type;
}
@Value("${postgis.user}")
public void setDb_user(String db_user) {
MapConstant.db_user = db_user;
}
@Value("${postgis.passwd}")
public void setDb_password(String db_password) {
MapConstant.db_password = db_password;
}
@Value("${server.geoserver-path}")
public void setGeoServerPath(String geoServerPath) {
GEO_SERVER_PATH = geoServerPath;
}
//否
public static final int DISABLE = 0;
//是
public static final int ENABLE = 1;
public static final String ESPG4326="latLonBoundingBox";
public static final String ESPG21481="nativeBoundingBox";
//Geoserver服务相关参数
public static final String RESTURLKey="gsmgr_resturl";
public static final String RESTUSERKey="gsmgr_restuser";
public static final String RESTUSERValue="admin";
public static final String RESTPWKey="gsmgr_restpw";
public static final String RESTPWValue="geoserver";
public static final String GS_VERSIONKey="gsmgr_version";
public static final String GS_VERSIONValue="2.15.1";
}
该方式是重写属性set方法,将从配置文件取到的内容赋值给静态属性
第二种,修改MapConstant 类
@Configuration
@Data
public class MapConstant {
@Value("${server.geoserver-url}")
public String geoserverUrl ;
@Value("${server.geoserver-user}")
public String geoserverUsername;
@Value("${server.geoserver-password}")
public String geoserverPassword;
@Value("${server.geoserver-path}")
public String geoserverPath;
@Value("${postgis.database}")
public String db ;
@Value("${postgis.host}")
public String db_host_before;
@Value("${postgis.port}")
public String db_port_before;
@Value("${postgis.dbtype}")
public String db_type_before ;
@Value("${postgis.user}")
public String db_user_before;
@Value("${postgis.schema}")
public String db_schema_before;
@Value("${postgis.passwd}")
public String db_password_before;
//需要配置的项
public static String BUNDLE_URL ;
public static String USERNAME;
public static String PASSWD;
public static String database ;
public static String db_host;
public static String db_port;
public static String db_type ;
public static String db_user;
public static String db_schema;
public static String db_password;
public static String GEO_SERVER_PATH;
@PostConstruct
public void init() {
BUNDLE_URL=geoserverUrl;
USERNAME=geoserverUsername;
PASSWD=geoserverPassword;
GEO_SERVER_PATH=geoserverPath;
database=db;
db_host=db_host_before;
db_port=db_port_before;
db_type=db_type_before;
db_user=db_user_before;
db_schema=db_schema_before;
db_password=db_password_before;
}
//否
public static final int DISABLE = 0;
//是
public static final int ENABLE = 1;
public static final String ESPG4326="latLonBoundingBox";
public static final String ESPG21481="nativeBoundingBox";
//Geoserver服务相关参数
public static final String RESTURLKey="gsmgr_resturl";
public static final String RESTUSERKey="gsmgr_restuser";
public static final String RESTUSERValue="admin";
public static final String RESTPWKey="gsmgr_restpw";
public static final String RESTPWValue="geoserver";
public static final String GS_VERSIONKey="gsmgr_version";
public static final String GS_VERSIONValue="2.15.1";
}
该方式是对每一个静态变量定义一个对应的普通变量,普通变量可以从配置文件获取信息,然后通过@PostConstruct注解方法赋值
测试代码
@Test
public void readConfigProperties() {
System.out.println(MapConstant.BUNDLE_URL);
System.out.println(MapConstant.USERNAME);
System.out.println(MapConstant.PASSWD);
System.out.println(MapConstant.database);
System.out.println(MapConstant.db_host);
System.out.println(MapConstant.db_port);
System.out.println(MapConstant.db_type);
System.out.println(MapConstant.db_user);
System.out.println(MapConstant.db_password);
System.out.println(MapConstant.GEO_SERVER_PATH);
}
成功读取结果
对此成功优化了这段代码,后期会详细介绍有关 @Value注解,有什么问题欢迎指见,感觉有帮距点个赞收藏下
————————————————
版权声明:本文为CSDN博主「码农咖」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:
https://blog.csdn.net/qq_45934356/article/details/107234186
程序员值得收藏的精选11套后台登录页面和管理页面模版
50份优秀Java求职者简历
SpringCloud前后端分离实战项目视频教程分享
2020年全网最全BAT笔试面试题打包分享
感谢点赞支持下哈