@Value注解获取不到pom配置的值
来源:互联网 发布:python grep功能 编辑:程序博客网 时间:2024/05/22 00:33
遇到问题:在UrlUtil类中通过@Value("${gold.url}")获取不到pom中配置的变量值,而获取的是变量名。文件如下:
解决办法:在
一、获取pom中配置的路径方法
@Service
public class UrlUtil {@Value("${gold.url}")
private String goldUrl;
public String getUrl(String urlType, String systemid) {
Map<String,String> urlMaps = new HashMap<String,String>();
urlMaps.put("gold_qm", goldUrl);
if (urlType!=null&&systemid!=null) {
return urlMaps.get(urlType+"_"+systemid);
}
return null;
}
}
二、pom配置gold.url 变量,局部代码
三、使用@Value("${gold.url}")注入要在,database.properties文件中添加变量占位,不然报如下错误
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.qmc.sportbetting.util.HttpHelper com.qmc.sportbetting.controller.CupRecordController.httpHelper; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'httpHelper': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.qmc.sportbetting.util.UrlUtil com.qmc.sportbetting.util.HttpHelper.urlUtil; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'urlUtil': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.qmc.sportbetting.util.UrlUtil.goldUrl; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'gold.url' in string value "${gold.url}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 22 more
database.properties文件配置如下:
四、解决问题所在
之所以在获取不到@Value("${gold.url}")注入的值,只因为在mvc-config.xml中配置扫描路径时没有添加use-default-filters="false" 可能导致所有含注解的类重复加载,因此没获取到值。
添加上后能正常获取到@Value注入的值。
具体可参考 :<context:component-scan>详解这篇文章。
- @Value注解获取不到pom配置的值
- spring结合disconf时@Value注解获取不到值
- spring的value获取不到值
- springboot Configuration 获取不到@value配置问题
- springMVC @Value 注解取不到值
- @Value注解取不到值问题
- 获取不到方法的注解
- Spring的@Value获取不到值的问题
- Spring @Value注入属性获取不到值
- SpringMVC通过注解@Value获取properties配置
- SpringMVC通过注解@Value获取properties配置
- Spring通过注解@Value获取properties配置
- 关于spring value注解取不到值问题。
- @Value spring 注解 获取*.properties 的属性值
- Spring之@Value 注解获取properties值(Spring 3的@value简化配置文件的读取)
- 关于VS05里checkboxlist用JS获取不到value值的解决方法
- 关于VS05里checkboxlist用JS获取不到value值的解决方法[转]
- [转载]关于VS05里checkboxlist用JS获取不到value值的解决方法
- CLLocationManager定位不准确,在国内存在偏移
- Android的productFlavor使用
- 均衡算法LMS 概述
- ButterKnife使用出现Nullpointerexception
- Python学习笔记(5)- 列表的遍历
- @Value注解获取不到pom配置的值
- 一个硅谷码农的自我觉醒:为什么我做起了科技记者?
- HDFS中NameNode和Secondary NameNode
- 页面设置预览实现
- mybatis传递Map和List集合示例
- Excel中数据截取
- 互联网架构为什么要做服务化?
- 腾讯提出共享AI,抢先布局游戏与多媒体AI,首秀同声传译
- 一个理解同步异步阻塞非阻塞非常好的解释