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

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的注解不支持静态的变量和方法)。

原文地址:https://www.cnblogs.com/lykbk/p/sadfsafef2345234324234.html

时间: 2024-08-04 10:41:06

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

springboot配置多个yml文件

springboot配置多个yml文件 参考 springboot配置多个yml文件 maven(三)最详细的profile的使用 实战 为生产和开发分别配置一个profile.每个都有多个文件. profile 每个profile都应该有唯一的id, 可以同时激活多个profile,每个profile提供一些配置信息. <project> <profiles> <!-- profile的id应该唯一,但是当重复时,使用的是文件后面的配置.--> <profile

springboot学习(2)配置文件取值

今天介绍三种配置文件手动取值的方式: springboot配置文件信息保存在application.properties中,默认可以spring.开头的进行spring进行一些常用参数的配置,但是很多时候我们需要手动配置一些配置,这就需要我们自己手动取值了, application.propertis配置文件默认存在classpath/classpaht/config中.我们也可以通过配置在启动服务器时通过启动程序时通过手动配置参数置顶该路径,这里就暂时不做演示,今天主要学习的是如何取得配置文件

使用Struts框架的Java Web项目中的两个Session的区别和常见的Session取值为空的错误

我的百度知道回答,感觉这个知识很有意义,专门贴出来. 不过还有待验证的地方,就是 ActionContext.getContext().getSession("name", "Tom"); //如果在JSP中通过 EL 或者 Struts2 标签怎么显示? 以下是回答正文. 很多初学Java Web的同学因为使用 Strut2框架而分不清两个 Session的区别. 1.   JSP/Servlety有个原生的 session 对象,这是9大内置对象中的一个,书上写

springboot加载application.yml文件null

话不多说,直接上代码 本人项目为maven项目 以下是项目结构 pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=&

从xml文件取值

假设有个 test.xml,包含以下字段: 1 <config> 2 <property name="login_protocol" value="http" /> 3 <property name="access_control" value="10"/> 4 <property name="receipt_route_kbn" value="smart

MVC绕过登陆界面验证时HttpContext.Current.User.Identity.Name取值为空问题解决方法

Global.asax界面添加如下方法: void FormsAuthentication_Authenticate(object sender, FormsAuthenticationEventArgs e) { string UserName = this.Context.Request.QueryString["UserName"]; if (!string.IsNullOrEmpty(UserName)) { FormsAuthentication.SetAuthCookie(

loadrunner动态从mysql取值

loadrunner动态从mysql取值 loadrunner动态从mysql取值 [需要下载跟数据库服务器一致的dll,32位或64位] loadrunner中有参数化从数据库中取值,但是只是静态的,对于一些要实时取值的数据就game over了,比如取短信验证码,因为MySQL中有一个libmysql.dll,里面提供了可以操作数据库的方法,所以我们可以调用这个dll连接数据库实时从数据库中取值 1 Action() 2 { 3 4 int status; //定义状态变量,0表示成功,非0

pig对null的处理(实际,对空文本处理为两种取值null或‘’)

pig对文本null的处理很特殊.会处理成两种null,还会处理成''这种空值. 比如,读name,age,sex日志信息.name取值处理,如果记录为",,,"这样,会将name取值为null,如果记录为",19,男"则name会处理为''.同样是空值,pig读取后的取值却不一样.所以一定要小心. pig读取日志信息,遇到取值为空的字段会处理为两种,一种取值为'',另一种为null. 具体例子:读取日志中倒数第4个字段(全部为空,两个逗号之间无值",,&

SpringBoot @Autowired中注入静态方法或者静态变量

https://www.cnblogs.com/huanghongbo/p/10890025.html 注:用static去定义一个注入的方法或者配置文件值变量,编译时不会有任何异常,运行时会报空指针. Spring官方不推荐此种方法. 原理: https://www.cnblogs.com/chenfeng1122/p/6270217.html 解决办法 利用spring的set注入方法,通过非静态的setter方法注入静态变量 ,我们可以改成这样就静态变量可以获取到你配置的信息了. 把get