Picking up Tomcat's Context.xml parameters via SpEL

99封情书 提交于 2020-01-04 06:28:12

问题


Deploying war to Apache Tomcat 8 the following way.

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>

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

Then copying myApp.war to $CATALINA_HOME/webapps

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>

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>

However I am getting following error upon beans loading:

Field or property 'contextParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'

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

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.


回答1:


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" />

And it worked successfully.



来源:https://stackoverflow.com/questions/21613974/picking-up-tomcats-context-xml-parameters-via-spel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!