zoukankan      html  css  js  c++  java
  • Spring/Spring boot中静态变量赋值

    情形1:静态变量为自动注入的对象

     解决方案:设置两个变量非静态变量使用@resource注入Bean,然后使用@PostConstruct在Spring初始化Bean成功后为静态变量赋值

    @Component
    public class XXUtils {
    
        @Resource
        private XXXProperties xxxPropertiesAutowired;
    
        private static XXXProperties xxxProperties;
    
        @PostConstruct
        public void init() {
           xxxProperties = this.xxxPropertiesAutowired;
        }
    }

    情形2:静态变量为普通的基本数据类型,并且从配置文件中读取初始化值

     解决方案:不要在静态变量上使用@Value注解(spring不允许/不支持把值注入到静态变量中)

                      在其对应的set方法是使用@Value注解(set方法不能是静态的)

    /**
         * 渠道号(XX提供,从配置中读取并初始化)
         */
        public static String SFT_NOTIFY_CEB_CHANNEL;
    
        /**
         * 机构号(XX’提供,从配置中读取并初始化)
         */
        public static String INST_CODE;
    
        //===================get/Set Method======================
        @Value("${mf.cebconfig.SFT_NOTIFY_CEB_CHANNEL}")
        public void setSFT_NOTIFY_CEB_CHANNEL(String sftNotifyCebChannel) {
            SFT_NOTIFY_CEB_CHANNEL = sftNotifyCebChannel;
            logger.info("init SFT_NOTIFY_CEB_CHANNEL value suss,sftNotifyCebChannel={}",sftNotifyCebChannel);
        }
    
        @Value("${mf.cebconfig.INST_CODE}")
        public void setINST_CODE(String instCode) {
            INST_CODE = instCode;
            logger.info("init INST_CODE value suss,instCode={}",instCode);
        }

    参考:

    https://www.jianshu.com/p/127310cb90e0

    http://blog.csdn.net/zhayuyao/article/details/78553417

  • 相关阅读:
    序列化
    python_模块与包
    python_常用内置模块
    python_生成器
    python_文件操作
    你好,mysql
    2017年12月20日 内置对象
    2017年12月17日 ASP.NET 12个表单元素&&简单控件/复合控件
    2017年12月16日 ASP.NET基本用法
    2017年12月14日 LinQ高级查&&Asp.net WebForm Asp.net MVC
  • 原文地址:https://www.cnblogs.com/huahua035/p/8529579.html
Copyright © 2011-2022 走看看