1、错误场景:

springboot项目中在.properties文件(.yml)文件中配置了属性值,在Bean中使用@Value注解引入该属性,Bean的构造器中使用该属性进行初始化,此时有可能会出现属性值为null,造成初始化程序的错误

2、错误原因:

因为Bean的构造器调用是在@Value属性赋值之前进行的,所以造成了属性还没有赋值,就被调用的情况。

3、解决方案:

将构造器中需要使用的@Value属性作为构造器的参数,确保构造器中使用该属性之前,属性已经得到初始化

理论先行,代码跟上(^_^)

(1).yml配置文件中配置系统参数值 file.upload-dir

file:
upload-dir: /Users/lc/temp/

(2)FileStorageService 的构造器需要使用使用 file.upload-dir 属性

@Service
public class FileStorageService {
  /* @Value("${file.upload-dir}")
    private String uploadDir; */
public FileStorageService(@Value("${file.upload-dir}") String uploadDir) throws ServiceException {
this.fileStorageLocation = Paths.get(uploadDir).toAbsolutePath().normalize();
try {
Files.createDirectories(this.fileStorageLocation);
} catch (Exception e) {
throw new Exception(e);
}
}
}

(3)now,问题解决了。

解决springboot项目中@Value注解参数值为null的问题的更多相关文章

  1. idea解决springboot项目中log4j漏洞升级问题

    最近阿里云团队发现log4j漏洞,危险级别:严重,相关资讯 https://m.sohu.com/coo/hsdt/506958086_355140 https://www.sohu.com/a/50 ...

  2. SpringBoot项目中自定义注解的使用

    1.定义注解接口 @Documented @Retention(RUNTIME) @Target(METHOD) public @interface MyLog {    String value() ...

  3. 解决SpringBoot项目中Thymeleaf模板的中文乱码问题

    1.使用IDEA创建SpringBoot项目 package com.example.demo; import org.springframework.boot.SpringApplication; ...

  4. SpringBoot项目中处理返回json的null值

    在后端数据接口项目开发中,经常遇到返回的数据中有null值,导致前端需要进行判断处理,否则容易出现undefined的情况,如何便捷的将null值转换为空字符串? 以SpringBoot项目为例,SS ...

  5. Spring-Boot项目中配置redis注解缓存

    Spring-Boot项目中配置redis注解缓存 在pom中添加redis缓存支持依赖 <dependency> <groupId>org.springframework.b ...

  6. SpringBoot项目中遇到的BUG

    1.启动项目的时候报错 1.Error starting ApplicationContext. To display the auto-configuration report re-run you ...

  7. SpringBoot12 QueryDSL01之QueryDSL介绍、springBoot项目中集成QueryDSL

    1 QueryDSL介绍 1.1 背景 QueryDSL的诞生解决了HQL查询类型安全方面的缺陷:HQL查询的扩展需要用字符串拼接的方式进行,这往往会导致代码的阅读困难:通过字符串对域类型和属性的不安 ...

  8. SpringBoot项目中,表单的验证操作

    在创建Springboot项目中,我们使用了表单验证操作,这一操作将极大地简化我们编程的开发 1.接收数据,以及验证 @PostMapping("/save") public Mo ...

  9. springboot项目中接口入参的简单校验

    .katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...

随机推荐

  1. 【译】第三篇 Integration Services:增量加载-Adding Rows

    本篇文章是Integration Services系列的第三篇,详细内容请参考原文. 增量加载是什么增量加载仅加载与先前加载差异的.差异包括:->新增的行->更新的行->删除的行通过 ...

  2. Electron 开发环境下总是 crash

    全局安装一个 electron devtool 关掉 崩溃时选择重新打开

  3. 2016.5.15——leetcode:Number of 1 Bits ,

    leetcode:Number of 1 Bits 代码均测试通过! 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n ...

  4. Linux SSH Backdoor分析排查

    1.SSH后门分类 SSH后门方式有以下几种 软链接 SSH Server wrapper SSH Keylogger 2.软链接 利用方法 [root@helen]# ln -sf /usr/sbi ...

  5. Linux USB驱动框架分析 【转】

    转自:http://blog.chinaunix.net/uid-11848011-id-96188.html 初次接触与OS相关的设备驱动编写,感觉还挺有意思的,为了不至于忘掉看过的东西,笔记跟总结 ...

  6. linux快速复制大量小文件方法 nc+tar【转】

    1,在需要对大量小文件进行移动或复制时,用cp.mv都会显得很没有效率,可以用tar先压缩再解压缩的方式.  2,在网络环境中传输时,可以再结合nc命令,通过管道和tcp端口进行传输.  nc和tar ...

  7. npm 下载node-zookeeper包

    环境:centos7(lunix) 1.安装nvm curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.6/install. ...

  8. Scrapy项目之User timeout caused connection failure(异常记录)

    Windows 10家庭中文版,Python 3.6.4,Scrapy 1.5.0, 提示:此文存在问题,真正测试, 请勿阅读, 07-14 14:26更新: 经过两个多小时的测试,发现此问题的原因是 ...

  9. ActiveMQ之VirtualTopic是什么?

    一句话总结: VirtualTopic是为了解决持久化模式下多消费端同时接收同一条消息的问题.   想象这样一个场景:   生产端产生了一笔订单,作为消息MessageOrder发了出去. 这笔订单既 ...

  10. handlermethodargumentresolver

    http://www.cnblogs.com/fangjian0423/p/springMVC-request-param-analysis.html http://www.cnblogs.com/f ...