简体   繁体   中英

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. For this the changes i made: in mvc-dispatcher-servlet.xml:

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

Component class:

@Component

public class 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. 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. check where did u put your 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

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. It will only operate on beans in the same application-context, not on beans in a parent or child context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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