错误信息
昨天写项目的时候使用 @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:
这样问题就解决了!!!!
具体原因查看博客。