2011-07-14 40 views
0

有沒有特別的方法來做到這一點? 我得到的是:如何配置mvc spring bean控制器的屬性?

  1. config.properties與param.key =值
  2. web.xml中與ContextLoaderListener的讀取配置
  3. 頁-servlet.xml中定義的servlet豆。

我想要的是使用param.key在pages-servlet.xml中配置其中一個bean。 我在xml中使用<property name="myField" value="${param.key}"/>,但我看到該字段配置爲${param.key}而不是「值」。

配置bean的正確方法是什麼?

好吧,我通過將定義配置bean的應用程序上下文文件導入到pages-servlet.xml中來解決它。它的工作原理,但似乎很不對勁。

+1

Spring Framework有相當詳細的文檔,您是否閱讀過用戶指南? http://springframework.org/documentation – cjstehno

+0

是的,但找不到答案。如果你能指出我的正確部分,而不是給出一般鏈接,我將不勝感激。 – Dima

回答

3

屬性佔位符是你想要的。

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 
    <context:property-placeholder location="classpath:/config.properties" /> 
    <bean id="mybean" class="..."> 
     <property name="xxx" value="${prop.value}" /> 
    </bean> 
</beans> 
+0

yup(+1)以下是鏈接:http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-placeholderconfigurer –

+0

這正是我在用什麼。由於某些原因,在Web項目的某個庫中定義的問題與其他所有Bean都無法在Servlet上下文中看到,儘管是ContextLoaderListener。它在將配置bean導入servlet上下文文件後起作用。 – Dima