我在 Web 应用程序的 Spring 上下文中配置了 PropertyPlaceholderConfigurer,该上下文又导入了 JAR 中需要配置某些属性的其他一些上下文。但由于某种原因,PropertyPlaceholderConfigurer 值对他们不可用,并且我在启动时收到错误:
java.net.URISyntaxException: Illegalcharacter in path at index 1: ${dax.svc1.endpoint}
这是我的应用程序上下文的样子:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" 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.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" name="mhpVariables">
<property name="locations">
<list>
<value>classpath:appconfig.properties</value>
</list>
</property>
</bean>
<import resource="classpath:com.test.svc1/childContext.xml"/>
<import resource="classpath:com.test.svc2/child2Context.xml"/>
</beans>
子上下文是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
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">
<!-- connection info -->
<bean class="com.test.java.framework.dataaccess.ServiceConnectionInfo" id="ConnectionInfo">
<property name="defaultUri" value="${dax.svc1.endpoint}"/>
<property name="maxTotalConnections" value="500"/>
<property name="maxConnectionsPerHost" value="50"/>
<property name="readTimeout" value="3000"/>
<property name="ConnectionTimeout" value="1000"/>
</bean>
</beans>
我验证了属性文件位于类路径上并且具有属性 dax.svc1.endpoint
。我在这里缺少什么?
最佳答案
您必须在每个导入中放置一个占位符 bean;这是我可以让它工作的唯一方法,因为我的设置与您所描述的类似。我还从 bean 中删除了 id,以防止容器中出现任何 id 冲突。
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="WEB-INF/myconfig.properties" />
</bean>
关于java - 导入上下文中的 bean 的 PropertyPlaceholderConfigurer 值未得到解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11110110/