通过 SpEL 获取 Tomcat 的 Context.xml 参数 [英] Picking up Tomcat's Context.xml parameters via SpEL

查看:41
本文介绍了通过 SpEL 获取 Tomcat 的 Context.xml 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过以下方式将 war 部署到 Apache Tomcat 8.

Deploying war to Apache Tomcat 8 the following way.

myApp.xml 放在 $CATALINA_HOME/conf/[enginename]/[hostname]/下内容如下:

Placing myApp.xml under $CATALINA_HOME/conf/[enginename]/[hostname]/ with the following contents:

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

顺便说一句我不会将任何类型的 Context.xml 放入 war.

B.t.w. I do not place any kind of Context.xml into war.

然后将 myApp.war 复制到 $CATALINA_HOME/webapps

Then copying myApp.war to $CATALINA_HOME/webapps

这是我的 web.xml

This is my 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 中引用).

And this way I try loading properties-file in beans.xml (referenced in web.xml above).

<?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 时遇到以下错误:

However I am getting following error upon beans loading:

在org.springframework.beans.factory.config.BeanExpressionContext"类型的对象上找不到字段或属性contextParameters"

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

Could you help me understand the error and propose a fix so that I can access Context-defined parameters?

P.S. 我没有放在这里,但我在 Context 中也有一些 <Environment>-节点,并且它们可以在其他地方通过 JNDI 成功访问.

P.S. I have not put here, but I also have some <Environment>-nodes in Context, and they are successfully accessible via JNDI in other places.

推荐答案

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

So as we were not able to solve the root cause - why contextParameters bean is not available, the following workaround (using ol' syntax) took place:

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

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

它成功了.

这篇关于通过 SpEL 获取 Tomcat 的 Context.xml 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆