java - Spring MVC : @Value annotation to get int value defined in *. 属性文件

标签 java spring jsp spring-mvc servlets

我有一个 config.properties 文件:

limit=10

我的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 类:

@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) { … }
}

更新:我测试过并遇到了同样的问题。按照 How can I inject a property value into an annotation configured spring mvc 3.0 controller 上的说明解决了这个问题

关于java - Spring MVC : @Value annotation to get int value defined in *. 属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27957364/

相关文章:

spring - Spring MVC 应用程序测试中的 ContextConfiguration

java - 无法在类中 Autowiring bean

java - Spring ,Java : Get email for critical errors configuration.

java - Netbeans 中使用 JSTL 的代码提示

java - 这个 Java 模式叫什么?

每 10 次循环的 Java 打印结果

jsp - 我想使用jsp将文件上传到mysql数据库

java - 我无法从 JSP 页面调用带有 varargs 的方法

java - 如何使用 JPA 实体构建和填充数据库?

java - 循环数组只有两个对象分配给Map?