我有一个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
@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({)'
任何想法?
答案 0 :(得分:2)
尝试以下方法:
@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
上的说明进行计算答案 1 :(得分:0)
在您的代码中:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/config.properties</value>
</list>
</property>
</bean>
classpath是指war文件中的WEB-INF / classes路径。那么你在WEB-INF / classes中有配置文件夹吗?
如果是,则使用基于xml的配置进行检查,如下所示:
<bean id="dataSource" class="org.tempuri.DataBeanClass">
<property name="url" value="${url}"></property>
</bean>
如果您仍然收到错误,那么肯定会出现属性文件加载问题。
答案 2 :(得分:0)
试试这个:@Value("#{new Integer('${limit}')}")
和
<context:property-placeholder location="config/config.properties" />