gpt4 book ai didi

spring - PropertyPlaceholderConfigurer 只是不能在 tomcat 中工作,请帮忙

转载 作者:行者123 更新时间:2023-11-28 22:45:42 25 4
gpt4 key购买 nike

在我部署在tomcat环境下的webapp中,我有一个spring配置文件,其中包含placeHolder,比如${myurl}。为了替换占位符,我在包含 PropertyPlaceholderConfigurater bean 的 WEB-INF 目录中创建了 applicationContext.xml,并将其位置设置为也在 WEB-INF 目录中的属性文件。

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="placeHolderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="WEB-INF/my.properties"/>
</bean>
<import resource="classpath*:META-INF/springFile1.xml"/>
</beans>

然后在 web.xml 中,我指定上下文:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml
</param-value>
</context-param>

在 WEB-INF/my.properties 中myurl=http://www.google.com

这是springFile1.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="placeholderPrefix" value="${" />
<property name="placeholderSuffix" value="}" />
</bean>
<bean id="mycache" class="com.abc.MyURL"
init-method="start" destroy-method="stop">

<constructor-arg value="${myurl}" />
</bean>
</beans>

我一直在获取未解析的值 ${myurl}

我尝试将属性文件放在类路径、绝对路径和 WEB-INF 中,结果都一样。

有什么建议吗?谢谢。

最佳答案

不要在 web.xml 中列出 xml 文件,而是尝试将它们导入 applicationContext.xml:

<import resource="classpath*:springFile1.xml"/>

更新:您似乎正在子 xml 中重新定义占位符配置器,从而覆盖了父级的配置器,后者指定了目标属性文件。从子 xml 中删除配置器声明。

关于spring - PropertyPlaceholderConfigurer 只是不能在 tomcat 中工作,请帮忙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5021460/

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