fastcodejava
fastcodejava

Reputation: 41087

Multiple properties file in spring

I load a properties file in spring :

 <context:property-placeholder location="classpath:foo.properties"/>

But if I try to load another file in a different context file I get error.

Upvotes: 2

Views: 4844

Answers (3)

user3358698
user3358698

Reputation: 21

Issue - How to use multiple properties file in spring config *

  1. create seperate prop-files in classPath eg. abc1.properties, abc2.propeties, abc2.propperties

  2. Create a propConfig.xml in class path class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">

    class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">

    classpath:/abc1.properties classpath:/abc2.properties classpath:/abc3.properties

  3. Create combined file for all other beanDefination.xml and propConfig.xml AllSpringConfig.xml 3.1 Make sure the below properties configure and locator beans are only defined in one
    fiile i.e. propConfig.xml 3.2 Make sure propConfig.xml is before any other config files like below

    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">

    <import resource="SpringPropConfig.xml"/>
            <import resource="abc1.xml"/> <!-- this may use props from abc1.prop --> 
            <import resource="abc2.xml"/> <!-- this may use props from abc2.prop --> 
            <import resource="abc3.xml"/> <!-- this may use props from abc3.prop --> 
    

  4. Use below to initialize the beans ApplicationContext context = new ClassPathXmlApplicationContext("AllSpringConfig.xml");

Note - make sure all mentioned files are in classpath or give proper paths where the file is located.

Upvotes: 2

tolitius
tolitius

Reputation: 22499

if you need to override properties you can do:

<context:property-override location="classpath:override.properties"/>

OR

if the error is due to not finding a certain property, you can set ignoreUnresolvablePlaceholders to true.


OR

if the error is about not found resource ( and you're ok with it ), you can set ignoreResourceNotFound to true.


OR

if there are errors in finding system properties:

The PropertyPlaceholderConfigurer not only looks for properties in the Properties file you specify. By default it also checks against the Java System properties if it cannot find a property in the specified properties files. You can customize this behavior by setting the systemPropertiesMode property of the configurer with one of the following three supported integer values:

never (0): Never check system properties

fallback (1): Check system properties if not resolvable in the specified properties files. This is the default.

override (2): Check system properties first, before trying the specified properties files. This allows system properties to override any other property source.

Upvotes: 4

Igor Nikolaev
Igor Nikolaev

Reputation: 4697

According to the sources you must provide comma separated list of propery files resources. This should work for you:

<context:property-placeholder location="classpath:foo1.properties,classpath:foo2.properties"/> 

Upvotes: 6

Related Questions