Search code examples
javaspringproperties-filespring-bean

Spring framework: load properties files, is the file read everytime a bean referencing it is created?


I am fairly new to spring and have not found time to take a short course also on it. I am learning as I go. I have a spring-app.xml which has the following import:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations">
        <list>
            <value>classpath:jdbc.properties</value>
        </list>
    </property>
</bean>

I define the classpath in my maven pom file and it works file. Now when i define a bean say

<bean id="DataLoader" class="com.abc.DataLoader" scope="prototype">
    <property name="dbDriver" value="${database.driver}"/>
    <property name="dbUrl" value="${database.url}"/>
    <property name="dbUserName" value="${database.username}"/>
    <property name="dbPassword" value="${database.password}"/>
</bean>

I need the bean to be prototype. Will spring read the properties file 'jdbc.properties' everytime this bean is created or just read it once and just inject the values? I am sure this is a pretty basic question but I didnot find an answer for this. This will affect the performance of my program greatly as I created this bean every min (almost).


Solution

  • Since PropertyPlaceholderConfigurer is singleton it would get initialized only once when context gets created and then further it will use its reference to resolve values