@value 静态变量_springboot项目使用静态变量通过@Value注解获取配置文件内容

b720d3598682c971984126d1fbc1aead.png

近期看到前同事写的项目,有关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);
    }

成功读取结果

eb7f2f1050d7f906d84684ece576078d.png

对此成功优化了这段代码,后期会详细介绍有关 @Value注解,有什么问题欢迎指见,感觉有帮距点个赞收藏下

————————————————

版权声明:本文为CSDN博主「码农咖」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:

https://blog.csdn.net/qq_45934356/article/details/107234186

39162b2681f5b2de243f2079ff15c7c5.png

73de463223f0580eba1ee732552de142.png

  • 程序员值得收藏的精选11套后台登录页面和管理页面模版

  • 50份优秀Java求职者简历

  • SpringCloud前后端分离实战项目视频教程分享

  • 2020年全网最全BAT笔试面试题打包分享

感谢点赞支持下哈 7e9147273ba303e7f944c8013ce5d396.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值