Today in this tutorial we are going to discuss how to set property of a bean from external source file using Spring framework.
As we already know Spring is a very powerful framework to inject the dependency at get the value at run time in the form of beans. But what happen if we don't have value for those beans in our ApplicationContext.xml file and we need to fetch value from some external file or data source and assign those values to beans. We will see these all in a sample example program.
First we will create a java POJO calss "Apple.java" in this class we will create two String variables and their getters and setters. Also we will create a method whoareyou() with return type String.
Below is the code:
Third we will create a xml bean file "AppContextRashid.xml" in which we will to include namespace "xmlns:context="http://www.springframework.org/schema/context" to read the data from external source.
To read the external property or value from file we will provide the location in property placeholder. e.g. <context:property-placeholder location="xmlBeans/myApp.properties"/>
Then in Bean property tag we will assign values from file to POJO class variables. To get the value from properties file we will write "${name of the property}".
e.g. <property name="desc1" value="${def-desc}"></property>
In the above statement we are fetching value for property "def-desc" from .property file and assigning its value to "desc1" variable in Apple class.
As we already know Spring is a very powerful framework to inject the dependency at get the value at run time in the form of beans. But what happen if we don't have value for those beans in our ApplicationContext.xml file and we need to fetch value from some external file or data source and assign those values to beans. We will see these all in a sample example program.
Files used in this example:
- Apple.java
- myApp.properties
- AppContextRashid.xml
- ClassHer.java
First we will create a java POJO calss "Apple.java" in this class we will create two String variables and their getters and setters. Also we will create a method whoareyou() with return type String.
Below is the code:
Second we will create a property file which contains the value for name1 and desc1 variables. to do so we will create a file "myApp.properties" in a xmlBeans package in our source directory which have some values. below is the example file.package rashidtest;public class Apple {String name1, desc1="not yet set";public String getName1() {return name1;}public void setName1(String name1) {this.name1 = name1;}public String getDesc1() {return desc1;}public void setDesc1(String desc1) {this.desc1 = desc1;}public String whoareyou() {String result= name1 + desc1;if(desc1 == null) return name1;else if(name1 == null) return desc1;else return result;}}
def-desc=I am a fruit and my color is Red.def-name=Apple.
Third we will create a xml bean file "AppContextRashid.xml" in which we will to include namespace "xmlns:context="http://www.springframework.org/schema/context" to read the data from external source.
To read the external property or value from file we will provide the location in property placeholder. e.g. <context:property-placeholder location="xmlBeans/myApp.properties"/>
Then in Bean property tag we will assign values from file to POJO class variables. To get the value from properties file we will write "${name of the property}".
e.g. <property name="desc1" value="${def-desc}"></property>
In the above statement we are fetching value for property "def-desc" from .property file and assigning its value to "desc1" variable in Apple class.
Fourth and final we will create a implementer class which have main method, instance of ApplicationContext and inject the value from bean to class using setter injection. to achieve this we will create a java file "ClassHer.java". Below is the complete class.<?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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><context:property-placeholder location="xmlBeans/myApp.properties"/><bean id="apple" class="rashidtest.Apple"><property name="desc1" value="${def-desc}"></property><property name="name1" value="${def-name}"></property></bean></beans>
package rashidtest;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class ClassHer {public static void main(String[] args) {ApplicationContext appContext=new ClassPathXmlApplicationContext("rashidtest/AppContextRashid.xml");Apple apple=appContext.getBean("apple", Apple.class);System.out.println("Apple class have property: " +apple.whoareyou());}}