Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use an optional properties file in a Spring configuration file?

Tags:

java

spring

I am using an XML configuration file that is loaded into my Java application using ApplicationContext.

The XML configuration file resolves its properties by reading from several property files using PropertyPlaceholderConfigurer.

I want to make each property file optional. I thought that this is done by setting ignoreUnresolsvablePlaceholders to true, however I am getting the following exception when I run the application (db-default.properties exists but db.properties does not):

Exception in thread "main" org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [db.properties] cannot be opened because it does not exist

This is what my XML configuration looks like:

<?xml version="1.0" encoding="UTF-8"?>

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

    <bean id="placeholder-configurer-1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="1"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
    </bean>   

    <bean id="placeholder-configurer-2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="2"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="location" value="classpath:/db-default.properties"/>
    </bean>   

    <bean id="placeholder-configurer-3" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="3"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="locations">
            <list>
                <value>classpath:/db.properties</value>
            </list>
        </property>
    </bean>

    <bean id="MyDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

</beans>

What do I need to do to make db.properties an optional property file?

like image 832
Jin Kim Avatar asked Mar 08 '12 21:03

Jin Kim


People also ask

How do you externalize a constants from a Spring configuration file into a properties file?

You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration. Property values can be injected directly into your beans using the @Value annotation, accessed via Spring's Environment abstraction or bound to structured objects.

How do I apply application properties in spring boot?

Step 1 − After creating an executable JAR file, run it by using the command java –jar <JARFILE>. Step 2 − Use the command given in the screenshot given below to change the port number for Spring Boot application by using command line properties.


1 Answers

<property name="ignoreResourceNotFound" value="true"/>
  • spring blog article
  • api docs
like image 64
andrew cooke Avatar answered Oct 13 '22 22:10

andrew cooke