0

I have a web application in which I'm using spring MVC. I have defined few constants in sample.properties file. spring-servlet is the servlet that gets initialized for all urls in my app. spring-servlet:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <context:property-placeholder location="classpath:sample.properties"/> </beans> 

In one of my java classes I access the value of one of the properties like this:

package com.sample; import com.google.gson.JsonArray; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Validator { @Value("${credit}") String credits; private static final Logger LOG = Logger.getLogger(Validator.class); public void checkRating() { LOG.debug(credits); } } 

sample.properties:

credit=[{"duration":15,"credit":10},{"duration":30,"credit":20}] 

When Validator is called from a controller, it just logs it as ${credit} in my log file. I dont get its value. Similarly, if I put an integer / floating point number in the properties file, I get an error saying it cannot be cast into integer / float. Am I missing any configuration?

6
  • Please confirm it again that properties file is placed under classpath directly under bin or build folder. Commented May 24, 2014 at 9:08
  • This is a gradle based project. I have placed it under src/main/resources. It is there under build directory also (resources/main/sample.properties). Commented May 24, 2014 at 9:25
  • I'm not sure if this is the problem or what version of Spring you're running. But try updating the XSD to the proper versions. It now uses 3.0 which might not be the correct version to use. Commented May 24, 2014 at 9:43
  • I changed it to 3.2 now since I'm using 3.2.4.RELEASE. But still no luck :( Commented May 24, 2014 at 9:48
  • I fixed it by placing the context:property-placeholder in applicationContext.xml instead of spring-servlet. Commented May 24, 2014 at 10:11

1 Answer 1

1

The reason for this is because propertyplaceholder configurer is a BeanFactoryPostProcessor and it processess only the beans defined in the context in which the propertyplaceholder is configured. In a web application typically there is a heirarchy of context as explained here. Hence you need to declare this at appropriate place.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.