• Software
  • Leadership
  • Agile
  • Events
  • Other Topics
    • Finance
    • Robotics & AI
    • System Administration
    • Books
    • Life Experiences
    • Environment
  • Write and Earn
  • About Us
    • About Us
    • Our Contributors
    • Contact Us
    • Article Submission Guidelines
    • Logo demystified
  • Follow @MeJaneve
    Janeve.Me
  • Categories

    Browse through following categories for articles written by professionals.
    • Agile
      4
    • Books
      5
    • Environment
      1
    • Events and Conferences
      7
    • Featured
      15
    • Finance
      1
    • Leadership
      5
    • Life Experiences
      8
    • Software
      30
    • System Administration
      2
  • Software
  • Leadership
  • Agile
  • Events
  • Other Topics
    • Finance
    • Robotics & AI
    • System Administration
    • Books
    • Life Experiences
    • Environment
  • Write and Earn
  • About Us
    • About Us
    • Our Contributors
    • Contact Us
    • Article Submission Guidelines
    • Logo demystified
Home » Software

Dynamic Loading using Java Reflection and Properties.

Janeve George Posted On August 9, 2012
0
10.1K Views


0
Shares
  • Share On Facebook
  • Tweet It

SoftwareProgramming

What is Java Reflection and Properties

In this article I will be explaining how you load classes dynamically using a properties file and Java Reflections. The Properties file are basically a collection of key-value pairs. It is the most commonly used mechanism for storing applications configuration data and settings. Reflection is a feature available in Java used by developers for examining and modifying the run-time behavior of applications running in the JVM.

MyBirds example

Let us start with a very simple problem statement: I should be able to load characters of a particular bird when I specify it’s name. For e.g: When i specify duck, the calling the sound() function should print “quack”;

This is a situation where you need to load a class dynamically based on some data provided by the client or external source. You also want the flexibility to configure classes in a simple properties file and these classes are having similar behavior.

To implement this we will need the following components:

  • mybirds.properties – The properties file where you can map keys to your class
  • MyBird.java – An interface which all the classes specified in the properties file will have to implement.
  • Duck.java, Eagle.java – The classes that implements the interface MyBird.
  • MyBirdFactory.java – The factory class that dynamically creates classes

Let us look at the code for each of these. Let us start with mybirds.properties.

#BIRD-TYPE=IMPLEMENTATION-CLASS
duck=com.foo.Duck
eagle=com.foo.Eagle

Now MyBird.java interface that declares the methods that the sub-classes should implement.

package com.foo;
 
/**
 * http://www.janeve.me
 */
public interface MyBird {
    public String sound();
}

Now let us see Duck.java and Eagle.java.

package com.foo;
 
/**
 * http://www.janeve.me
 */
public class Duck implements MyBird {
 
    public String sound() {
        return "Quack";
    }
}
package com.foo;
 
/**
 * http://www.janeve.me
 */
public class Eagle implements MyBird {
 
    public String sound() {
        return "Scream";
    }
 
}

MyBirdFactory.java is the class responsible for creating the desirable instance based on the birdType input passed to it.

package com.foo;
 
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Locale;
import java.util.ResourceBundle;
 
/**
 * http://www.janeve.me
 */
public class MyBirdFactory {
 
    private static final String MY_BIRDS_CONFIGURATION = "mybirds";
    private static Hashtable<String, String> myBirdsMappings = new Hashtable<String, String>();
 
    static {
        try {
            loadMyBirdsrMappings();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static MyBird getMyBird(String birdType) {
        String className = myBirdsMappings.get(birdType);
 
        MyBird bird = null;
 
        try {
            if( className!=null) {
                Class cls = Class.forName(className);
                bird = (MyBird)cls.newInstance();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return bird;
    }
 
    private static void loadMyBirdsrMappings() {
        ResourceBundle rb = ResourceBundle.getBundle(MY_BIRDS_CONFIGURATION, Locale.getDefault());
        for (Enumeration e = rb.getKeys(); e.hasMoreElements();) {
            String key = (String) e.nextElement();
            myBirdsMappings.put(key, rb.getString(key));
        }
    }
}

Make sure that the value of MY_BIRDS_CONFIGURATION is same as the name of the properties file.

We can write TestCode.java to test the code.

package com.foo;
 
/**
 * http://www.janeve.me
 */
public class TestCode {
 
    public static void main(String[] args) {
        if(args.length <=0)
            System.out.println("Please provide input. E.G: duck eagle duck ...");
        else {
            for(String name:args){
                MyBird bird = MyBirdFactory.getMyBird( name );
                if(bird == null){
                    System.out.println("Couldn't find your bird. Please make sure it's entered in mybirds.properties");
                } else {
                    System.out.println("The sound of the bird"  + name + " is " + bird.sound() );
                }
 
            }
        }
 
    }
 
}

Check out the output when you run the code.

C:\Janeve\MyBirds>java -classpath ./ com.foo.TestCode duck duck eagle duck eagle eagle
The sound of the bird duck is Quack
The sound of the bird duck is Quack
The sound of the bird eagle is Scream
The sound of the bird duck is Quack
The sound of the bird eagle is Scream
The sound of the bird eagle is Scream
 
C:\Janeve\MyBirds>

As you can see, the classes were loaded as per your input.You may download the complete source code.

Try adding your favourite bird in the properties file and let me know how it went.

Post Views: 10,117
0
Shares
  • Share On Facebook
  • Tweet It




Author

Janeve George

A Technology Leader, Software Engineer, and Agile Methodologies enthusiast. Currently, working as Lead Software Development with Zeta Suite. He has more than 1.6 decades of experience spanning different verticals predominated by hosting, cloud, and media delivery technologies.

Check your CIBIL TransUnion Score before applying for a loan.
Read Next

Check your CIBIL TransUnion Score before applying for a loan.

  • Follow @MeJaneve
    Janeve.Me
  • Categories

    Browse through following categories for articles written by professionals.
    • Agile
      4
    • Books
      5
    • Environment
      1
    • Events and Conferences
      7
    • Featured
      15
    • Finance
      1
    • Leadership
      5
    • Life Experiences
      8
    • Software
      30
    • System Administration
      2

  • Popular Posts

  • Recent Posts

    • Java Asynchronous Programming using CompletableFuture - Part 1
    • The Java Stream API
    • Functional Interfaces in Java
    • Online Webinar: Students & New Technology - 28th July 2020
  • Keep In Touch

    Follow us on social media to get latest articles on Programming, System Architecture, Agile Development Methodologies, Product and Project Management, Personal Development, BigData, Robotics, Upcoming Events and more...


Copyright © 2020 | Janeve.Me. All rights Reserved.
Press enter/return to begin your search