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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27957364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:42:21  来源:igfitidea点击:

Spring MVC: @Value annotation to get int value defined in *.properties file

javaspringjspspring-mvcservlets

提问by zccoding

I have a config.properties file:

我有一个 config.properties 文件:

limit=10

My springmvc-servlet.xml:

我的 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?

有任何想法吗?

回答by Timothy Jeffcoat

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

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

更新:我测试并遇到了同样的问题。按照如何将属性值注入到注释配置的 spring mvc 3.0 控制器中的说明进行计算

回答by Ravi Jiyani

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 ?

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

If yes then check using xml based configuration as below :

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

<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.

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

回答by Satya

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

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

and

<context:property-placeholder location="config/config.properties" />