我有一个 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/