简体   繁体   English

从部署时从属性文件gettng读取的Spring 3.0 @Value

[英]Spring 3.0 @Value read from property file gettng exception on deployment

I am trying to build a spring 3.0 application version 3.1.0.RELEASE , where i want to read from a property file and using the @Value annotation read from it in my Component class. 我正在尝试构建一个Spring 3.0应用程序版本3.1.0.RELEASE,我想从其中读取属性文件,并在Component类中使用从中读取的@Value注释。 For this the changes i made: in mvc-dispatcher-servlet.xml: 为此,我进行了以下更改:在mvc-dispatcher-servlet.xml中:

 <context:property-placeholder location="classpath:mediamonitoring.properties"/> 

Component class: 组件类:

@Component

public class SomeHelper { 公共类SomeHelper {

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

public String getBaseUri() {
    return baseUri;
}

public void setBaseUri(String baseUri) {
    this.baseUri = baseUri;
}
}

Property: 属性:

baseUri:http://localhost:8080/

and i have wired this helper class to a @service class using the @Autowired annotation. 并且我已经使用@Autowired注释将此帮助程序类连接到@service类。 When i build and deploy the application i get the following error: 当我构建和部署应用程序时,出现以下错误:

java.lang.IllegalArgumentException: Could not resolve placeholder 'baseUri'
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)

Is there anything which i am missing because i just followed the standard proceedure. 有什么我想念的东西吗,因为我只是遵循标准程序。

Appreciate any help in advance. 提前感谢您的帮助。

-Vaibhav -瓦贝哈夫

使用=代替:作为分隔符

baseUri=http://localhost:8080/

can't comment, need more rep, so using the asnwer option. 无法发表评论,需要更多代表,因此请使用asnwer选项。 check where did u put your mediamonitoring.properties. 检查您在哪里放置了mediamonitoring.properties。 I mean, check if it's in your classpath 我的意思是,检查它是否在您的类路径中

You should escape special characters : and = with \\ in value like this: 您应将特殊字符:=转义为\\ ,如下所示:

baseUri:http\://localhost\:8080/

Otherwise parser can't decide where you value ends and where new key starts. 否则解析器无法确定您的值在哪里结束以及新键在哪里开始。
See also Java properties file specs 另请参阅Java属性文件规范

Replace : with = and use # instead of $ =替换:并使用#代替$

#{baseUri}

You can also try to use: 您也可以尝试使用:

<util:properties id="props"
    location="classpath:/yourproperties.properties" />

And than: 然后:

@Value("#{props['yourProperty']}")

Assuming you are following the normal practices of having a ContextLoaderListener and a DispatcherServlet make sure that the <context:property-placeholder location="classpath:mediamonitoring.properties"/> is in the correct application-context. 假设您遵循使用ContextLoaderListenerDispatcherServlet的常规做法,请确保<context:property-placeholder location="classpath:mediamonitoring.properties"/>处于正确的应用程序上下文中。 It will only operate on beans in the same application-context, not on beans in a parent or child context. 它将仅在相同应用程序上下文中的Bean上运行,而不在父或子上下文中的Bean上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM