![](/img/trans.png)
[英]How can I inject a property value into a Spring Bean which was configured using annotations?
[英]How can I inject a property value into an annotation configured spring mvc 3.0 controller
第一:我正在使用Spring 3.0
配置控制器類時出現問題。 控制器使用Web服務,我想使用.properties文件定義端點地址。
@Controller
public class SupportController {
@Value("#{url.webservice}")
private String wsEndpoint;
...
在我的應用程序上下文xml文件中,我定義了以下內容:
<context:property-placeholder location="/WEB-INF/*.properties" />
我一直在閱讀文檔,嘗試使用不同的方法(例如添加前綴systemProperties。 ),但是我不斷收到一條錯誤消息,告訴我它不存在。
在類型為“ org.springframework.beans.factory.config.BeanExpressionContext”的對象上找不到字段或屬性“ url”
好。 我知道了。
現在,在控制器中:
@Value("#{settings['url.webservice']")
然后在上下文配置中,我有這個“幫助器bean”:
<util:properties id="settings"
location="/WEB-INF/supportweb.properties"></util:properties>
這也應該起作用:
@Value("${url.webservice}")
private String wsEndpoint;
我有此配置,它工作正常:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application.properties</value>
</list>
</property>
</bean>
我以這種方式創造財產
@Value("${root.log.level}")
private String prop;
該字段已正確初始化為“ DEBUG”值。
您應該檢查
<context:property-placeholder location="/WEB-INF/*.properties" />
在webmvc-config.xml中定義,您可以在其中創建@Controllers的實例
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.