SpringBoot使用@Value从yml文件取值为空--注入静态变量

 
 

1.application.yml中配置内容如下:

  1.  
    pcacmgr:
  2.  
    publicCertFilePath: E:\\pcacmgr\\CerFiles\\xh_public.cer
  3.  
    encPublicCertFilePath: E:\\pcacmgr\\CerFiles\\hjzf_encPublic.cer
  4.  
    encPfxFilePath: E:\\pcacmgr\\CerFiles\\hjzf_encPfx.pfx
  5.  
    encPfxFilePwd: 11111111

2.通过@Value获取值:

  1.  
    @Configuration
  2.  
    public class PcacIntegrationUtil {
  3.  
    @Value("${pcacmgr.publicCertFilePath}")
  4.  
    private static String publicCertFilePath;
  5.  
     
  6.  
    @Value("${pcacmgr.encPfxFilePath}")
  7.  
    private static String encPfxFilePath;
  8.  
     
  9.  
    @Value("${pcacmgr.encPfxFilePwd}")
  10.  
    private static String encPfxFilePwd;
  11.  
     
  12.  
    @Value("${pcacmgr.encPublicCertFilePath}")
  13.  
    private static String encPublicCertFilePath;
  14.  
     
  15.  
    public static String signData(String sourceData) {
  16.  
    System.out.println(publicCertFilePath);
  17.  
    }
  18.  
    }

3.启动项目调用过程中发现获取值为null。

4.发现是static导致,以下为解决方案:

  1.  
    @Configuration
  2.  
    public class PcacIntegrationUtil {
  3.  
    private static Logger logger = LoggerFactory.getLogger(PcacIntegrationUtil.class);
  4.  
     
  5.  
    private static String publicCertFilePath;
  6.  
    public static String getPublicCertFilePath() {
  7.  
    return publicCertFilePath;
  8.  
    }
  9.  
    @Value("${pcacmgr.publicCertFilePath}")
  10.  
    public void setPublicCertFilePath(String publicCertFilePath) {
  11.  
    PcacIntegrationUtil.publicCertFilePath = publicCertFilePath;
  12.  
    }
  13.  
     
  14.  
    public static String signData(String sourceData) {
  15.  
    System.out.println(publicCertFilePath);
  16.  
    }
  17.  
    }

问题解决,打印结果与yml文件配置的内容相符。

心得:使用注解的方式,不过注解写在非static的方法上(Spring的注解不支持静态的变量和方法)。

SpringBoot使用@Value从yml文件取值为空--注入静态变量的更多相关文章

  1. setlocale(LC_ALL, ""); 取值为空字符串" "(注意,不是NULL),则locale与本地环境所使用的编码方式相同(在本地化时,应该很有用);

    在C运行库提供的多字节字符-宽字符转换函数:mbstowcs()/wcstombs()中,需要用到全局变量locale( locale encoding ),以指定多字节字符的编码类型 1. 功能: ...

  2. springboot的yaml基础语法与取值,配置类,配置文件加载优先级

    1.基本语法k:(空格)v:表示一对键值对(一个空格必须有):以空格的缩进来控制层级关系:只要是左对齐的一列数据,都是同一个层级的属性和值也是大小写敏感: server: port: 8081 pat ...

  3. SpringBoot加载自定义yml文件

    自定义配置文件(跟SpringBoot的application.yml同一目录下): nlu-parse-rule: title: "NLU响应结果解析规则" desc: &quo ...

  4. springboot:读取application.yml文件

    现在开发主要使用微服务框架springboot,在springboot中经常遇到读取application.yml文件的情形. 一.概述 开发过程中经常遇到要读取application.yml文件中的 ...

  5. Springboot读取自定义的yml文件中的List对象

    Yml文件(novellist.xml)如下: novellist:   list:     - name: 笑傲江湖       type: 武侠       master: 令狐冲       a ...

  6. ASP.NET页面使用JQuery EasyUI生成Dialog后台取值为空

    原因: JQuery EasyUI生成Dialog后原来的文档结构发生了变化,原本在form里的内容被移动form外面,提交到后台后就没有办法取值了. 解决办法: 在生成Dialog后将它append ...

  7. springboot加载application.yml文件null

    话不多说,直接上代码 本人项目为maven项目 以下是项目结构 pom.xml文件 <?xml version="1.0" encoding="UTF-8" ...

  8. springboot配置多个yml文件

    新接触了springboot项目,yml一大堆,启动不知道用的哪个,各种百度后: <profiles> <profile> <id>dev</id> & ...

  9. 从xml文件取值

    假设有个 test.xml,包含以下字段: <config> <property name="login_protocol" value="http&q ...

随机推荐

  1. DataGrip 连接数据库查询出来的结果乱码的问题

    打开连接数据源选项 选择 Advanced----Charset  填入 GBK 应用即可 目前遇到的是连接 SYbase数据库

  2. Lazarus下改变DBGrid记录的颜色,与Delphi不同了。

    功能:根据一条记录满足特定条件,使用不同的颜色进行显示. 来源:http://www.aliyagoo.com/blog 主要事件是在PrepareCanvas 不是DrawColumnCell 而且 ...

  3. Emac

    https://emacs-china.org/ 启动 console模式的emacs,$emacs -nw 学习参考: 1)<21天精通emacs> 2)Emacs常用命令简集. 3)& ...

  4. Tomcat虚拟目录设置

    ssh $host "rm -fr /var/www/$tomcat_name/webapps/*" 远程分发war包部署tomcat项目时,需要先清除项目目录. -------- ...

  5. Visual Studio for Mac 使用笔记

    无法打开控制台运行程序 Project -> Project Options -> Run -> Configurations -> Default 勾选Run on exte ...

  6. 通用坐标投影转换器Uneversal Coord Transformer

    关键词:投影,重投影,坐标转换,坐标系,空间参考,北京54,西安80,中国2000,WGS84,UTM,墨卡托,网络墨卡托 软件名称:通用坐标投影转换器Uneversal Coord Transfor ...

  7. Javascript中call、apply之我见

    一.call和apply定义. 1.Call 语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 参数 thisObj 可选项.将被用作当前对象的对象. arg ...

  8. LinQ to sql 各种数据库查询方法

    1.多条件查询: 并且 && 或者 || var list = con.car.Where(r => r.code == "c014" || r.oil == ...

  9. yii添加模型基础类

  10. lcd 显示屏

    1.lcd 接口信号: VSYNC : 一帧新数据的开始信号 HSYNC :一行新数据的开始信号 VCLK   :像素的同步信号 VD[0:23]  :传递数据的信号线 2. LCD  的显示原理 ( ...