Java Spring MVC:@Value注释以获取在*.properties文件中定义的int值

Java Spring MVC:@Value注释以获取在*.properties文件中定义的int值,java,spring,jsp,spring-mvc,servlets,Java,Spring,Jsp,Spring Mvc,Servlets,我有一个config.properties文件: limit=10 My springmvc-servlet.xml: <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:con

我有一个config.properties文件:

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>
错误消息是:

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({)'
有什么想法吗?

在您的代码中:

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

classpath:config/config.properties
classpath是指war文件中的WEB-INF/classes路径。那么您在WEB-INF/classes中有配置文件夹吗

如果是,则使用基于xml的配置进行检查,如下所示:

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

如果仍然出现错误,则肯定存在属性文件加载问题

试试这个:
@Value(“#{new Integer('${limit}')}”)


尝试以下操作:

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

举例如下:

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

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

更新:我做了测试,发现了同样的问题。按照

中的说明进行计算,使用
int
而不是
整数
。我还建议使用
而不是
属性PlaceHolderConfigure
。为您保存一些XML,在较新的spring版本中,它将为您提供更强大的
属性资源占位符配置器
@zccode:
@Value(${limit}”)整数限制
应该可以工作。在这种情况下,错误消息是什么?你使用哪种spring版本?不,但这不是重点。关键是,如果你没有检查,无法解释原因,或者只是认为这可能是答案,不要将其作为答案发布!
@Value("#{config.limit}") Integer limit;
@Repository 
public class RewardsTestDatabase {
    @Value("#{systemProperties.databaseName}")
    public void setDatabaseName(String dbName) { … }

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