Accessing Property File Using Spring Framework

On the previous post in this blog using Indonesian language, we tried to use property file to access database in order to avoid hardcode mechanism, and the accessing of the property file by using a class named ConfigProperties, now we will try to access property file without using those class but we will try to use Spring Framework to access property file.

As we knew, the existence of Spring Framework on Java technology is really-really help us the Java Developer, the modularity of this framework has made the enterprise java project development is easier to rebuild or modify.

We will not discuss much more about the Spring Framework here, we only will try to use the Spring Framework one of this framework feature to access property file. And of course the values of the property file to be used on our application chain later.

Let’s go directly to try this out. On Spring Framework, the usage of bean became a must, that’s why we will create an object as a representation of a bean, the scenario that we will make is every item that exist on property file will become a property of the object that that we have created. Let’s make the property file just like below:

application.properties file:

application.name=mojo
application.password=mojo123
application.url=http://localhost:8080/test_doang/test.jsp

We will put this file on config folder, a line with the src folder, just like below picture shown:

foldermacam2spring

With the item of the property file, we will create a simple object of Java pojo which will represent each item that exist on the property file above. The source code of this object is below:

PropertyHolder.java file:

package org.mojo.spring.entity;
/**
 * Created by IntelliJ IDEA.
 * User: Mojo
 * Date: Mar 9, 2009
 * Time: 7:04:46 PM
 * To change this template use File | Settings | File Templates.
 */
public class PropertyHolder {
    private String name;
    private String password;
    private String url;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String toString() {
        return "PropertyHolder{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", url='" + url + '\'' +
                '}';
    }
}

 

 

 

 

On above source code, we create a file named PropertyHolder.java. On this file we only create 3 property of the object, because there’s only 3 item on the property file. On the usage, each property of this object will hold values that exist on the property file.

In order to make each item on the property file can be inject into each object property, we have to define the object on Spring configuration file, so when the application run, this configuration will be called at first line and finally each item on the property file can be injected into each property of the PropertyHolder object that we have created before. Below is the definition of the object that we define as a bean on spring configuration file, this file will also put on same folder with the property file location which is on config folder.

applicationContext-app.xml file:

 

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>

    <bean id="propertyConfigurer"          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="config/application.properties"/>
   </bean>

       <!-- Local Data Holder -->
    <bean id="propertyHolder" class="org.mojo.spring.entity.PropertyHolder">
        <property name="name" value="${application.name}"/>
        <property name="password" value="${application.password}"/>
        <property name="url" value="${application.url}"/>
    </bean>
</beans>

Basically, when an application that using Spring Framework start running, then all the bean that already configured on Spring configuration file will also called one by one, even maybe one bean will depends on another bean. On the defined bean above, we can see that we have defined two beans there, the first bean have an id called “propertyConfigurer”, this bean have a function to give information to the Spring Framework the location of the property file. While the other bean given an id as “propertyHolder”, this bean is a map of the object that we have created before. Let’s make an attention to the defined class of the second bean, and then each property that defined on the block of this defined bean, this is how we map each item of the property file to each property on the object.

Actually, this is the main focus of our try-it-out today, here’s how we access each items on the property file using Spring Framework. And if when we want to use the value of the property file items, all we need is just called the PropertyHolder object only, a line before we set the PropertyHolder object for sure. The usage example is just like below:

package org.mojo.spring.application;

import org.mojo.spring.entity.PropertyHolder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * Created by IntelliJ IDEA.
 * User: Mojo
 * Date: Mar 9, 2009
 * Time: 7:06:35 PM
 * To change this template use File | Settings | File Templates.
 */
public class TestReadProperty {
    private PropertyHolder holder =null;

    public void setHolder(PropertyHolder holder) {
        this.holder = holder;
    }

    public static void main(String[] args) {
        ApplicationContext context    = new FileSystemXmlApplicationContext("config/applicationContext-app.xml");
        TestReadProperty app = new TestReadProperty();
        app.setHolder((PropertyHolder) context.getBean("propertyHolder"));

        System.out.println("Name : " + app.holder.getName());
        System.out.println("Password : " + app.holder.getPassword());
        System.out.println("Url : " + app.holder.getUrl());
        System.out.println("full : " + app.holder);
    }
}

On the source code of usage example above, we can describe it just like this:

At first PropertyHolder object defined as a private variable named as holder. Then there’s a method which have a function as a setter of the holder object. And then on the main method of this sample program, we have defined an ApplicationContext as an object, this context called the Spring Framework configuration file, and then we have to define this program as an object too, in order to make all the property of the object can be called on the main method.

The next step is, set the PropertyHolder object by calling setter method of the PropertyHolder that we have made before on this example program. When this method called, then automatically Spring Framework will fill up the property of PropertyHolder object with the value of each item that exists on the property file. And the last step is we tried to print-out the value of object property that owned by PropertyHolder class to console. And don’t forget to import all the necessary class and library that needed by this example application on the top line of this example program as we seen above.

That’s it, our try-it-out today, a little try-out that using one the feature of Spring Framework that has a lot and useful feature that we can use on our development project. And if you want to try-it too, don’t forget to download the library of Spring Framework and other libraries that become dependency libraries for the Spring Framework it self

Have a nice try ….!!!

 

 

I Hope this will helpfull

Menteng, March 10, 2009

 

Josescalia

One thought on “Accessing Property File Using Spring Framework

  1. Pingback: Accessing Property File Using Spring Framework | XML Developer

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s