Menu

How to set property of a bean from external source file using Spring framework

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.

Files used in this example:


  1. Apple.java
  2. myApp.properties
  3. AppContextRashid.xml
  4. 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:
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;
}
}

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.

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.

<?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.xsd
http://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>

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.

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());
}
}

2 comments:

  1. Gclub มือถือ How much will you get from us?

    บาคาร่า It is another option that all the names will have access to the game. Frankenstein's monster and other gambling games. It is free and meets the desires of all Internet users. With flight hours and expertise in service for over 12 years, it is well known in the field of online gambling throughout Southeast Asia.
    About Games Frankenstein's monster is a revolutionary internet-based slot game. It was created by a scientist named Franken and you are the winner of the great payoff. And you'll also enjoy spinning your slots with stunning 3D graphics. What are you waiting for? Come into the slot machine Franken Slots. Monster is now available. Let's see how much money you get from us.
    And unless the game Frankenstein's monster, we also have the highest number of gambling games, with more than 500 registrations for every team member to choose according to each player's satisfaction. Do not wait, we have a dedicated team of 24 hours of service. Gclub มือถือ

    ReplyDelete