2010-01-13 19 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」值。

相關問題