简体   繁体   中英

PropertyPlaceholderConfigurer values are not getting resolved for beans in imported context

I have PropertyPlaceholderConfigurer configured in my web application's Spring context which in turn imports few other contexts which are in JARs that expect certain properties to be configured. But for some reason the PropertyPlaceholderConfigurer values ware not available to them and I get error on start up:

java.net.URISyntaxException: Illegalcharacter in path at index 1: ${dax.svc1.endpoint}

Here is what my application context looks like:

<?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>

Child context is like this :

<?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>

I verified the property file is on the classpath and has the property dax.svc1.endpoint . What am I missing here?

我将假设您拥有所有xml指令...检查属性文件的编码(也是您的XML)

You have to put a placeholder bean inside each of the imports; that's the only way I could get it to work as I have a similar setup to what you describe. I also removed the id from the bean to prevent any id conflicts in the container.

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="WEB-INF/myconfig.properties" />
</bean> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM