2010-01-13 49 views
13

第一:我使用Spring 3.0我如何注入属性值转换成注释配置Spring MVC的3.0控制器

配置我的控制器类时,我有一个问题。控制器使用Web服务,我想使用.properties文件定义端点地址。

@Controller 
public class SupportController { 

    @Value("#{url.webservice}") 
    private String wsEndpoint; 

    ... 

在我的应用程序上下文XML的文件中,我定义的:

<context:property-placeholder location="/WEB-INF/*.properties" /> 

我一直在阅读文档,尝试不同的方法(如添加前缀systemProperties),但我不断收到一条错误消息,告诉我它不存在。

字段或属性 'URL' 不能 上型 'org.springframework.beans.factory.config.BeanExpressionContext'


确定的对象中找到。我已经知道了。现在

,控制器:

@Value("#{settings['url.webservice']") 

然后在上下文配置我有这样的 “帮手豆”:

<util:properties id="settings" 
location="/WEB-INF/supportweb.properties"></util:properties> 
+0

Duplicate:http://stackoverflow.com/questions/1741968/using-spring3-value-to-access-propertyplaceholderconfigurer-values – skaffman 2010-01-13 11:08:11

回答

11

这应该工作,太:

@Value("${url.webservice}") 
private String wsEndpoint; 
2

你应该检查

<context:property-placeholder location="/WEB-INF/*.properties" /> 

在webmvc-config.xml中定义,您创建@Controllers

2

我有此配置的情况下,它工作正常:

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

我iniejct物业这样

@Value("${root.log.level}") 
private String prop; 

该字段已正确初始化为“DEBUG”值。