Spring MVC:@Value批注获取* .properties文件中定义的int值 [英] Spring MVC: @Value annotation to get int value defined in *.properties file

查看:986
本文介绍了Spring MVC:@Value批注获取* .properties文件中定义的int值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个config.properties文件:

I have a config.properties file:

limit=10

我的springmvc-servlet.xml:

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>

这是我的控制器类:

@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 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({)'

有什么想法吗?

推荐答案

尝试以下操作:

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

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

示例如下:

@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

这篇关于Spring MVC:@Value批注获取* .properties文件中定义的int值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆