2014-02-06 19 views
2

以下列方式將war部署到Apache Tomcat 8。通過SpEL提取Tomcat的Context.xml參數

配售myApp.xml下$ CATALINA_HOME/CONF/[引擎]/[主機名]/ ,其內容如下:

<Context> 
    <Parameter name="myApp_configs" value="file:/the/path/to/configs/folder" 
     type="java.lang.String" override="false"/> 
</Context> 

B.t.w. 我沒有將任何一種Context.xml放入war

然後複製myApp.war到$ CATALINA_HOME/webapps中

這是我的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE web-app PUBLIC 
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
     "http://java.sun.com/dtd/web-app_2_3.dtd"> 
<web-app> 
    <display-name>service</display-name> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>WEB-INF/beans.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class> 
      org.springframework.web.context.ContextLoaderListener 
     </listener-class> 
    </listener> 
    <servlet> 
     <servlet-name>CXFServlet</servlet-name> 
     <display-name>CXF Servlet</display-name> 
     <servlet-class> 
      org.apache.cxf.transport.servlet.CXFServlet 
     </servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>CXFServlet</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

而且這樣我嘗試加載的屬性文件中beans.xml中(參考在上面的web.xml中)。

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs" 
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> 

    <!-- Imported resources for cxf --> 
    <import resource="classpath:META-INF/cxf/cxf.xml" /> 
    <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" /> 
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 

    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 
    <!--context:property-placeholder 
     location="#{contextParameters['myApp_configs']}/myApp.properties"/--> 
    <bean id="configurer" 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="location" 
       value="#{contextParameters['myApp_configs']}/myApp.properties"/> 
    </bean> 
...other lines follow here... 
</beans> 

但是我在豆類加載得到以下錯誤:

字段或屬性「contextParameters」不能在類型「org.springframework.beans.factory.config.BeanExpressionContext」

的對象找到

你能幫我理解錯誤並提出一個修復方案,以便我可以訪問上下文定義的參數嗎?

P.S.我沒有放在這裏,但我也有一些<環境>在上下文中的節點,並且他們可以通過JNDI在其他地方成功訪問。

回答

1

因此,當我們沒能解決的根本原因 - 爲什麼contextParameters豆不可用,以下解決方法(使用醇」語法)發生了:

<bean id="myConfigsLocation" 
      class="org.springframework.web.context.support.ServletContextParameterFactoryBean"> 
     <property name="initParamName" value="myApp_configs" /> 
    </bean> 

    <context:property-placeholder 
     location="#{myConfigsLocation}/myApp.properties" /> 

併成功的工作。