SpringBoot使用@Value从yml文件取值为空--注入静态变量
SpringBoot使用@Value从yml文件取值为空--注入静态变量
1.application.yml中配置内容如下:
- pcacmgr:
- publicCertFilePath: E:\\pcacmgr\\CerFiles\\xh_public.cer
- encPublicCertFilePath: E:\\pcacmgr\\CerFiles\\hjzf_encPublic.cer
- encPfxFilePath: E:\\pcacmgr\\CerFiles\\hjzf_encPfx.pfx
- encPfxFilePwd: 11111111
2.通过@Value获取值:
- @Configuration
- public class PcacIntegrationUtil {
- @Value("${pcacmgr.publicCertFilePath}")
- private static String publicCertFilePath;
- @Value("${pcacmgr.encPfxFilePath}")
- private static String encPfxFilePath;
- @Value("${pcacmgr.encPfxFilePwd}")
- private static String encPfxFilePwd;
- @Value("${pcacmgr.encPublicCertFilePath}")
- private static String encPublicCertFilePath;
- public static String signData(String sourceData) {
- System.out.println(publicCertFilePath);
- }
- }
3.启动项目调用过程中发现获取值为null。
4.发现是static导致,以下为解决方案:
- @Configuration
- public class PcacIntegrationUtil {
- private static Logger logger = LoggerFactory.getLogger(PcacIntegrationUtil.class);
- private static String publicCertFilePath;
- public static String getPublicCertFilePath() {
- return publicCertFilePath;
- }
- @Value("${pcacmgr.publicCertFilePath}")
- public void setPublicCertFilePath(String publicCertFilePath) {
- PcacIntegrationUtil.publicCertFilePath = publicCertFilePath;
- }
- public static String signData(String sourceData) {
- System.out.println(publicCertFilePath);
- }
- }
问题解决,打印结果与yml文件配置的内容相符。
心得:使用注解的方式,不过注解写在非static的方法上(Spring的注解不支持静态的变量和方法)。
SpringBoot使用@Value从yml文件取值为空--注入静态变量的更多相关文章
- setlocale(LC_ALL, ""); 取值为空字符串" "(注意,不是NULL),则locale与本地环境所使用的编码方式相同(在本地化时,应该很有用);
在C运行库提供的多字节字符-宽字符转换函数:mbstowcs()/wcstombs()中,需要用到全局变量locale( locale encoding ),以指定多字节字符的编码类型 1. 功能: ...
- springboot的yaml基础语法与取值,配置类,配置文件加载优先级
1.基本语法k:(空格)v:表示一对键值对(一个空格必须有):以空格的缩进来控制层级关系:只要是左对齐的一列数据,都是同一个层级的属性和值也是大小写敏感: server: port: 8081 pat ...
- SpringBoot加载自定义yml文件
自定义配置文件(跟SpringBoot的application.yml同一目录下): nlu-parse-rule: title: "NLU响应结果解析规则" desc: &quo ...
- springboot:读取application.yml文件
现在开发主要使用微服务框架springboot,在springboot中经常遇到读取application.yml文件的情形. 一.概述 开发过程中经常遇到要读取application.yml文件中的 ...
- Springboot读取自定义的yml文件中的List对象
Yml文件(novellist.xml)如下: novellist: list: - name: 笑傲江湖 type: 武侠 master: 令狐冲 a ...
- ASP.NET页面使用JQuery EasyUI生成Dialog后台取值为空
原因: JQuery EasyUI生成Dialog后原来的文档结构发生了变化,原本在form里的内容被移动form外面,提交到后台后就没有办法取值了. 解决办法: 在生成Dialog后将它append ...
- springboot加载application.yml文件null
话不多说,直接上代码 本人项目为maven项目 以下是项目结构 pom.xml文件 <?xml version="1.0" encoding="UTF-8" ...
- springboot配置多个yml文件
新接触了springboot项目,yml一大堆,启动不知道用的哪个,各种百度后: <profiles> <profile> <id>dev</id> & ...
- 从xml文件取值
假设有个 test.xml,包含以下字段: <config> <property name="login_protocol" value="http&q ...
随机推荐
- Java基础之用记事本编辑java代码运行,并且打成jar包后运行
使用记事本写java代码 1.在d盘新建一个记事本,名字叫做zhanzhuang.java,会询问不可用,是否继续,点击是 2.在里面编辑就如下内容,注意文件的名字要和 class 后面的名字相对应 ...
- R-CNN 学习记录
CNN是一个运用卷积神经网络进行图片分类的开山之作.RCNN是第一个把图片分类和目标检测连接起来的作品. RCNN主要解决的问题是: 1.怎样用深度神经网络进行目标定位:2.怎样用小批量的标注数据来训 ...
- RACSignal常见用法
RACSignal 两种用法, 1:异步操作,一般创建signal的时候写逻辑,然后通过subscribeNext拿到异步执行的结果 2:监听的属性的变化,及时给出回应,一般赋值的时候用RACObse ...
- unity WegGL 调用js
test.jslib文件,必须放到Assets/Plugins下,这里是:Assets/Plugins/WebGL mergeInto(LibraryManager.library, { Hello: ...
- C#使用MonoPInvokeCallback,让C直接回调C#函数
Test.mm char* TestMakeCString(NSString *str) { const char* string = [str UTF8String]; if (string == ...
- Haskell语言学习笔记(83)Pipes
安装 pipes $ cabal install pipes Installed pipes-4.3.9 Prelude> import Pipes Prelude Pipes> impo ...
- 知识点6: v-for列表循环
效果图: 只手写了第一个dom: 剩下4个dom由v-for生成. html <div class="item" id="item"> <di ...
- android Button、TabLayout英文自动改小写为大写的问题
如果是Button自动大写问题,直接设置Button的 textAllCaps="false" 即可: 如果是TabLayout出现全大写问题,先在style.xml加入属性: & ...
- LISTVIEW显示JPEG缩略图
http://www.ctsys.cn/files/SHOW_FILES.ASPX?ID=22 许多的JPEG图片浏览器(如由我设计的<JPEG浏览缩放器>),都可以将JPEG缩略图放置到 ...
- 【转】Classful IPv4 addressing definition
Classful addressing definition Class Leadingbits Size of networknumber bit field Size of restbit fie ...