使用@Value无法注入

错误信息
昨天写项目的时候使用 @Value 注解时。发现使用 @Value 项目保存,在配置文件中无法找到对应的键值对。错误信息是

org.springframework.beans.factory.BeanCreationException: Error
creating bean with name ‘blogServiceIpml’: Injection of
autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.pushlightly.service.ipml.BlogServiceIpml.ABCD; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder ‘BLOG_PAGE_COUNT’ in string value “${BLOG_PAGE_COUNT}”

配置信息
在代码中,使用依赖注入,注入的值为:
这里写图片描述

配置文件:
这里写图片描述

spring 配置文件 1:
这里写图片描述

spring 配置文件 2:
这里写图片描述

错因排除:
1.查看是不是注解类的所在的包导错了:(org.springframework.beans.factory.annotation.Value);

2.查看日志文件:

查看配置文件是否被初始化:

[org.springframework.core.io.support.PathMatchingResourcePatternResolver]-[DEBUG]
Resolved location pattern [classpath:resources/*.properties] to
resources [file
[E:\javadaima1\blog\blog-service\target\classes\resources\db.properties],
file
[E:\javadaima1\blog\blog-service\target\classes\resources\resource.properties]]

从日志信息可以看出 db.properties 文件和 resource.properties 均已经被初始化。

2.查看 @Value 注解是否被扫描到:
在日志中,随便查找一个被 @Value 注解的变量名。

这里写图片描述

发现所在包的注解已经被扫描到。

现在尴尬了,所有的配置都正确,为什么无法进行注入呢???

解决方案:

在多个配置文件中这能使用一次 也就是说只能在一个 spring 配置文件中导入所有的配置文件。

spring 正确配置:
spring 配置文件 1:

这里写图片描述

spring 配置文件 2:

这里写图片描述

这样问题就解决了!!!!

具体原因查看博客

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值