0

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

vaibhav
  • 3,929
  • 8
  • 45
  • 81

5 Answers5

1

Use = instead of : as separator

baseUri=http://localhost:8080/
Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69
  • Your file is not loaded (maybe not in classpath) – Luca Basso Ricci Sep 12 '13 at 08:45
  • i can see this file in WEB-INF/classes – vaibhav Sep 12 '13 at 08:47
  • try without `classpath:` prefix as described in http://stackoverflow.com/questions/4779572/could-not-resolve-spring-property-placeholder?rq=1 – Luca Basso Ricci Sep 12 '13 at 08:52
  • removed classpath: org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/mediamonitoring.properties] – vaibhav Sep 12 '13 at 08:56
  • Try move `` in same context of your `@Controller` as in http://stackoverflow.com/questions/5275724/spring-3-0-5-doesnt-evaluate-value-annotation-from-properties?rq=1 – Luca Basso Ricci Sep 12 '13 at 09:00
0

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

secario
  • 456
  • 4
  • 14
  • it is in the src/main/resources folder where the localization and related files are kept, also the file is getting recognized as when i renamed the file in context:property-placeholder location="classpath:mediamonitoring.properties" to some weired name it was throwing error file not found – vaibhav Sep 12 '13 at 08:17
0

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

Community
  • 1
  • 1
Ondrej Bozek
  • 10,987
  • 7
  • 54
  • 70
0

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']}")
dyrkin
  • 544
  • 4
  • 15
0

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.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224