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. C++中几种测试程序运行时间的方法<转>

    转的地址:https://www.cnblogs.com/silentteen/p/7532855.html 1.GetTickCount()函数 原理: GetTickCount()是获取系统启动后 ...

  2. UI5-学习篇-7-Postman测试SAP OData Services

    1.Postman简介 用户在开发或者调试网络程序或者是网页B/S模式的程序的时候是需要一些方法来跟踪网页请求的,用户可以使用一些网络的监视工具比如著名的Firebug等网页调试工具. postman ...

  3. Erlang 笔记

    集成开发环境:IntelliJ IDEA的Erlang插件 教程:www.erlang-cn.com/462.html,寻找erlang程序设计第2版pdf f():释放之前绑定过的所有变量. -ex ...

  4. docker之数据卷管理

    转自:https://www.cnblogs.com/jsonhc/p/7777811.html docker之数据卷的备份和还原 1.现在利用镜像创建一个nginx的服务容器,并挂载一个数据卷 [r ...

  5. Uni2D 入门 -- Atlas转载 http://blog.csdn.net/kakashi8841/article/details/17588095

    转载csdnTexture Atlas 我为什么应该使用Texture Atlas? 使用Atlas是一个普遍的好做法,而且它有很多好处.当有某些需要在屏幕渲染的时候,它背后带来的是draw call ...

  6. idea git 发起一个pull request 请求

  7. <assert.h>库学习

    assert的初步认识 assert宏指令是用来诊断程序是否有误的,函数原型如下 void assert(int expression) 那为什么我们要使用assert而不用printf呢?因为ass ...

  8. 使用Js控制ReactRouter路由

    [使用Js控制ReactRouter路由] 首先引入PropTypes: const PropTypes = require('prop-types'); 然后定义context的router属性: ...

  9. 为什么要用MarkDown?

    [为什么要用MarkDown?] 大部分作家用 Word 或 Pages 写作,过去的文档也大都以 .doc, .docx 格式或是 Pages 格式储存.还有人为了保证文稿发给谁都能正常打开,会用 ...

  10. linux重新安装python

    第一步:下载python2.7  wget https://www.Python.org/ftp/python/2.7.12/Python-2.7.12.tar.xz 第二步: 解压刚刚下载的压缩包 ...