Dynamic Loading using Java Reflection and Properties

[复制链接]
查看11 | 回复5 | 2011-5-7 01:45:08 | 显示全部楼层 |阅读模式
What is Java Reflection and PropertiesIn 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 [url=]applications[/url] configuration data and settings. Reflection is a feature available in Java used by developers for [url=]examining[/url] and modifying the run-time behavior of applications running in the JVM.MyBirds exampleLet 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 [url=]calling[/url] the sound() function should [url=]print[/url] "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 classMyBird.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 [url=]code[/url] for each of these. Let us start with mybirds.properties.[backcolor=white !important][size=1em][backcolor=rgb(212, 208, 200) !important][color=#a0a0a0 !important][size=1em]?
[backcolor=white !important][size=1em]1
[backcolor=white !important][size=1em]2
[backcolor=white !important][size=1em]3

回复

使用道具 举报

千问 | 2011-5-7 01:45:08 | 显示全部楼层
package com.foo;

/**
* http://www.janeve.me
*/
public interface MyBird {
public String sound();
}
回复

使用道具 举报

千问 | 2011-5-7 01:45:08 | 显示全部楼层
本帖最后由 carniege 于 2012-8-18 21:53 编辑
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";
}
}
回复

使用道具 举报

千问 | 2011-5-7 01:45:08 | 显示全部楼层
package com.foo;

/**
* http://www.janeve.me
*/
public class Eagle implements MyBird {

public String sound() {
return "Scream";
}

}
回复

使用道具 举报

千问 | 2011-5-7 01:45:08 | 显示全部楼层
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 myBirdsMappings = new Hashtable();

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));
}
}
}
回复

使用道具 举报

千问 | 2011-5-7 01:45:08 | 显示全部楼层
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 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 favorite bird in the properties file and let me know how it went.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行