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. Lazarus下改变DBGrid记录的颜色,与Delphi不同了。

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

  2. 1.ECS(CentOS7)主机名修改命令

    命令:hostnamectl 使用--help参数查看hostnamectl里面每个参数对应的含义: [root@localhost ~]# hostnamectl --help Query or c ...

  3. How to Pronounce the Word OR

    How to Pronounce the Word OR Share Tweet Share Tagged With: OR Reduction Study the OR reduction.  Th ...

  4. k8s的内置DNS增加父系DNS方法

    我们都知道K8S有内置DNS,是在搭建K8S时候以容器方式起来的,那么有时候我们需要解析内部DNS地址该怎么办呢,我们可以搭建个内部DNS 但是怎么让K8S通过内部DNS解析呢? 可以尝试如下方法 在 ...

  5. Myeclipse2017 安装反编译插件和SVN插件

    亲测有效 2018年1月22日10:36:33 https://www.cnblogs.com/liuyk-code/p/7519886.html

  6. 【原】linux学习路径

    1.  <<The Linux Command Line A Complete Introduction>> 2.  <<Advanced Programming ...

  7. AngularJS理论知识

    两个核心概念 三个架构 MVC 一切应用程序都是数据的增删改查 那么总要有东西装数据吧 Model就是干这个事(数据表现和操作) View(展现数据) Controller(逻辑) 那么M- V- C ...

  8. centos 卸载和安装软件

    rpm -qa 列出所有已安装软件包 rpm -e packagename  删除软件包 rpm -e --nodeps packagename  强制删除软件和依赖包 rpm -q 包名     查 ...

  9. beebase

    1.简单介绍 BeeBase是一个在线生物信息学数据库,显示与Apis mellifera.欧洲蜜蜂以及一些病原体和其他物种有关的数据.它是与蜜蜂基因组测序联盟合作开发的.BeeBase是蜜蜂研究社区 ...

  10. C专家编程

    [C专家编程] 1.如果写了这样一条语句: if(3=i).那么编程器会发出“attempted assignment to literal(试图向常数赋值)”的错误信息. 所以将常量放置在==前央, ...