Read Properties Spring Application Context
There are many ways you could read value from properties file while using Spring framework. If you want to read properties from file during Spring context load please use below simple technique. To make something working I will create small maven project to show how to read properties from file:
- Create maven project name: LoadPropertyFromContext as full structure shown below:
This is just for reference you could download full project from bottom download link.
- Create properties file where you want to save all your project properties and in my case its javahonk-solace.properties where I wanted to save all my Solace properties:
#properties SOLACE_HOST=tcp://javahonk.com:55555 SOLACE_VPN=D_JAVA_VPM SOLACE_CONNECTION_FACTORY=D_JAVA_VPM_CF SOLACE_OUTPUT_QUEUE_NAME=JAVAHONK_TEST SOLACE_USERNAME_PUB=JAVAHONK SOLACE_PASSWORD_PUB=PASS
- spring-context.xml:
<?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" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <context:annotation-config /> <context:component-scan base-package="com.javahonk.test" /> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:javahonk-solace.properties</value> </list> </property> <property name="ignoreUnresolvablePlaceholders" value="true"/> </bean> <bean id="propertyHolder" class="com.migration.test.PropertyHolder"></bean> </beans>
- Create PropertyHolder.java class to hold whatever properties are needed to use during Application context load:
package com.javahonk.test; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class PropertyHolder { @Value("${SOLACE_HOST}") private String SOLACE_HOST; @Value("${SOLACE_VPN}") private String SOLACE_VPN; @Value("${SOLACE_CONNECTION_FACTORY}") private String SOLACE_CONNECTION_FACTORY; @Value("${SOLACE_OUTPUT_QUEUE_NAME}") private String SOLACE_OUTPUT_QUEUE_NAME; @Value("${SOLACE_USERNAME_PUB}") private String SOLACE_USERNAME_PUB; @Value("${SOLACE_PASSWORD_PUB}") private String SOLACE_PASSWORD_PUB; public String getSOLACE_HOST() { return SOLACE_HOST; } public void setSOLACE_HOST(String sOLACE_HOST) { SOLACE_HOST = sOLACE_HOST; } public String getSOLACE_VPN() { return SOLACE_VPN; } public void setSOLACE_VPN(String sOLACE_VPN) { SOLACE_VPN = sOLACE_VPN; } public String getSOLACE_CONNECTION_FACTORY() { return SOLACE_CONNECTION_FACTORY; } public void setSOLACE_CONNECTION_FACTORY(String sOLACE_CONNECTION_FACTORY) { SOLACE_CONNECTION_FACTORY = sOLACE_CONNECTION_FACTORY; } public String getSOLACE_OUTPUT_QUEUE_NAME() { return SOLACE_OUTPUT_QUEUE_NAME; } public void setSOLACE_OUTPUT_QUEUE_NAME(String sOLACE_OUTPUT_QUEUE_NAME) { SOLACE_OUTPUT_QUEUE_NAME = sOLACE_OUTPUT_QUEUE_NAME; } public String getSOLACE_USERNAME_PUB() { return SOLACE_USERNAME_PUB; } public void setSOLACE_USERNAME_PUB(String sOLACE_USERNAME_PUB) { SOLACE_USERNAME_PUB = sOLACE_USERNAME_PUB; } public String getSOLACE_PASSWORD_PUB() { return SOLACE_PASSWORD_PUB; } public void setSOLACE_PASSWORD_PUB(String sOLACE_PASSWORD_PUB) { SOLACE_PASSWORD_PUB = sOLACE_PASSWORD_PUB; } }
- Now create JavahonkReadPropertiesFromFile.java file which will load context and read properties:
package com.javahonk.test; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class JavahonkReadPropertiesFromFile { private static final Logger logger = LogManager.getLogger(JavahonkReadPropertiesFromFile.class.getName()); public static void main(String[] args) { logger.info("Solace Output Adapter Starting..."); ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml"); PropertyHolder propertyHolder = context.getBean(PropertyHolder.class); System.out.println(propertyHolder.getSOLACE_HOST()); System.out.println(propertyHolder.getSOLACE_CONNECTION_FACTORY()); System.out.println(propertyHolder.getSOLACE_OUTPUT_QUEUE_NAME()); ((AbstractApplicationContext) context).close(); } }
- Download complete project LoadPropertyFromContext