简体   繁体   中英

Providing optionally properties from external file in Spring

I have a spring beans file, queue-beans.xml , with the following relevant segments:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>classpath:queue.properties</value>
            <value>file:///${confPath}/queue.properties</value>
        </list>
    </property>
</bean>

<rabbit:connection-factory id="rabbitConnectionFactory" 
    port="${rabbitmq.port}" 
    host="${rabbitmq.host}" 
    virtual-host="${rabbitmq.vhost}" 
    username="${rabbitmq.username}" 
    password="${rabbitmq.password}" />

My goal is to be able to override the properties. When I want to override the properties I run this:

c:\my-app>mvn exec:java -Dexec.mainClass="com.mycompany.myapp.module1.SomeClass" -DconfPath="my-app-conf/vagrant" -pl module1

I'm experiencing 2 problems: (1) when I don't provide -DconfPath="my-app-conf/vagrant" then I get an error that Could not resolve placeholder 'confPath' , and (2) when I do provide this parameter I get Could not resolve placeholder 'rabbitmq.host' in string value "${rabbitmq.host}" .

Question: How can I tell Spring to ignore ${confPath} if I don't provide it and why when I do provide it it doesn't find the properties?

You should provide another properties file which contains all defaults. Like this you dont need to ignore non existing properties. You can still superseed with the approach you take. It's just a matter of ordering the propertyplaceholderconfigurer correctly. You declare 2 configurer and define an order on each.

I dont have a concrete example right here, but if you need it i can dig it up.

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