Add properties file spring

To add properties file spring please follow below steps:

  • Create resources folder inside your project as source folder
  • Create sample properties file sample.properties inside resources folder and add some content in it sample below:
invalid-key=The CAPTCHA Service public key is incorrect.
invalid-parameters=The CAPTCHA Service was invoked with incorrect parameters.
unknown-error=The CAPTCHA Service returned an unknown error.
incorrect-captcha-sol=The letters in the graphic were entered incorrectly. Please try again.
NotBlank.captcha.response=The letters in the graphic were entered incorrectly. Please try again.

 

  • In your spring context XML file add below bean tag:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>                
                <value>classpath:sample.properties</value>
            </list>
        </property>
</bean>

 

  • Eclipse automatically add source folder to classpath so resources folder and its files will be available at run-time. (Otherwise you could create any  folder and add to the classpath)
  • To use this properties value in bean see example below:
<bean id="captchaValidator" class="com.util.CaptchaValidator" init-method="init">
        <property name="invalid-key" value="${invalid-key}"/>
        <property name="invalid-parameters" value="$invalid-parameters}"/>
        <property name="unknown-error" value="${unknown-error}"/>
    </bean>

 

  • That’s it.

Add properties file spring

Leave a Reply

Your email address will not be published. Required fields are marked *