gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:00:08 24 4
gpt4 key购买 nike

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

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

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

顺便说一句我不会将任何类型的 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>

但是我在加载 bean 时遇到以下错误:

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

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

P.S. 我没有放在这里,但是我在Context中也有一些 -nodes,在其他地方可以通过JNDI成功访问它们。

最佳答案

由于我们无法解决根本原因 - 为什么 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" />

它成功地工作了。

关于spring - 通过 SpEL 获取 Tomcat 的 Context.xml 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21613974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com