通过SpEL获取Tomcat的Context.xml参数

时间:2014-02-06 21:02:20

标签: spring tomcat war applicationcontext spring-el

以下列方式将 war 部署到Apache Tomcat 8。

在$ CATALINA_HOME / conf / [enginename] / [hostname] /下放置 myApp.xml 具有以下内容:

<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 中加载properties-file(在上面的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>

然而,我在bean加载时遇到以下错误:

在'org.springframework.beans.factory.config.BeanExpressionContext'

类型的对象上找不到字段或属性'contextParameters'

您能帮我理解错误并提出修复方法,以便我可以访问上下文定义的参数吗?

P.S。我没有放在这里,但我在Context中也有一些&lt; Environment&gt; -nodes,并且可以通过其他地方的JNDI成功访问它们。

1 个答案:

答案 0 :(得分:1)

因为我们无法解决根本原因 - 为什么contextParameters bean不可用,所以发生了以下解决方法(使用ol'语法):

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

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

它成功运作。

相关问题