4

I have a config.properties file:

limit=10

My springmvc-servlet.xml:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:config/config.properties</value>
        </list>
    </property>
</bean>

This is my controller class:

@Controller
@RequestMapping(...)
public class Test extends BaseController
{
    @Value("${limit}") Integer limit;   // This doesn't work
    @Value("#{T(java.lang.Integer).parseInt(${limit})}") Integer limit;   // This also doesn't work

    @RequestMapping(...)
    @ResponseBody
    public String session()
    {
        return String.valueOf(limit);
    }
}

error message is:

Error creating bean with name 'test': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: java.lang.Integer **.Test.limit; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelParseException: EL1043E:(pos 31): Unexpected token. Expected 'rparen())' but was 'lcurly({)'

any ideas?

Chip Zhang
  • 641
  • 2
  • 11
  • 26
  • Use an `int` instead of an `Integer`. I would also suggest using `` instead of the `PropertyPlaceholderConfigurer`. Saves you some XML and in newer spring versions it will give you the more powerful `PropertySourcesPlaceholderConfigurer`. – M. Deinum Jan 15 '15 at 06:56
  • @zccode: `@Value("${limit}") Integer limit` should work. What is the error message in this case? Which spring version do you use? – Jens Jan 15 '15 at 07:20

3 Answers3

2

Try the following:

@Value("#{config.limit}") Integer limit;

Documentation at http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/new-in-3.html

Example given there is:

@Repository 
public class RewardsTestDatabase {
    @Value("#{systemProperties.databaseName}")
    public void setDatabaseName(String dbName) { … }

    @Value("#{strategyBean.databaseKeyGenerator}")
    public void setKeyGenerator(KeyGenerator kg) { … }
}

UPDATE: I tested and had same problem. Figured it out by following instructions at How can I inject a property value into an annotation configured spring mvc 3.0 controller

br.julien
  • 3,420
  • 2
  • 23
  • 44
Timothy Jeffcoat
  • 775
  • 2
  • 9
  • 26
0

In your code :

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:config/config.properties</value>
        </list>
    </property>
</bean>

classpath refers to WEB-INF/classes path inside war file. So do you have config folder inside WEB-INF/classes ?

If yes then check using xml based configuration as below :

<bean id="dataSource" class="org.tempuri.DataBeanClass">
    <property name="url" value="${url}"></property>
</bean>

If still you are getting an error then there is sure issue with property file loading.

Ravi Jiyani
  • 919
  • 1
  • 10
  • 26
0

try this : @Value("#{new Integer('${limit}')}")

and

<context:property-placeholder location="config/config.properties" />
Satya
  • 9
  • 1
  • 3
  • no, but that's not the point. the point is, that if you didn't check, can't explain why, or simply think that this might be the answer, don't post it as answer! – No Idea For Name Jan 15 '15 at 07:40