@Value注解获取不到pom配置的值

时间:2025-02-13 07:22:42

 遇到问题:在UrlUtil类中通过@Value("${}")获取不到pom中配置的变量值,而获取的是变量名。文件如下:

解决办法:在


一、获取pom中配置的路径方法

@Service

public class UrlUtil {

@Value("${}")
private String goldUrl;

public String getUrl(String urlType, String systemid) {
Map<String,String> urlMaps = new HashMap<String,String>();
("gold_qm", goldUrl);

if (urlType!=null&&systemid!=null) {
return (urlType+"_"+systemid);
}
return null;
}

}


二、pom配置 变量,局部代码



三、使用@Value("${}")注入要在,文件中添加变量占位,不然报如下错误

Caused by: : Could not autowire field: private ; nested exception is : Error creating bean with name 'httpHelper': Injection of autowired dependencies failed; nested exception is : Could not autowire field: private ; nested exception is : Error creating bean with name 'urlUtil': Injection of autowired dependencies failed; nested exception is : Could not autowire field: private ; nested exception is : Could not resolve placeholder '' in string value "${}"
at $(:561)
at (:88)
at (:331)
... 22 more


文件配置如下:



四、解决问题所在

    之所以在获取不到@Value("${}")注入的值,只因为在中配置扫描路径时没有添加use-default-filters="false" 可能导致所有含注解的类重复加载,因此没获取到值。


添加上后能正常获取到@Value注入的值。

具体可参考 :详解这篇文章。