温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java如何使用属性文件和Reflections动态加载类

发布时间:2020-06-02 21:25:16 来源:亿速云 阅读:240 作者:Leah 栏目:编程语言

这篇文章给大家分享的是Java使用属性文件和Reflections动态加载类的方法。小编觉得挺实用的,因此分享给大家学习。如下资料是关于Reflections动态加载类实现内容。

MyBirds示例

让我们从一个非常简单的问题陈述开始:指定特定鸟的名字后,我应该能够加载它的字符。 例如:当我指定鸭子时,调用sound()函数应显示“ quack”;

在这种情况下,你需要根据客户端或外部源提供的某些数据动态加载类。 你还希望灵活地在简单的属性文件中配置类,并且这些类具有类似的行为。


为了实现这一点,我们将需要以下组件:

·         mybirds.properties ——可在其中将键映射到类的属性文件。

·         MyBird.java ——属性文件中指定的所有类都必须实现的接口。

·         Duck.java, Eagle.java ——实现接口MyBird的类。

·         MyBirdFactory.java ——动态创建类的工厂类


让我们看一下这些代码。让我们从mybirds.properties开始。

1

2

3

#BIRD-TYPE=IMPLEMENTATION-CLASS

duck=com.foo.Duck

eagle=com.foo.Eagle


现在,MyBird.java接口声明了子类应实现的方法。

1

2

3

4

5

6

7

8

package com.foo;

 

/**

 * http://www.janeve.me

 */

public interface MyBird {

    public String sound();

}


现在让我们看看Duck.javaEagle.java

1

2

3

4

5

6

7

8

9

10

11

package com.foo;

 

/**

 * http://www.janeve.me

 */

public class Duck implements MyBird {

 

    public String sound() {

        return "Quack";

    }

}

 

1

2

3

4

5

6

7

8

9

10

11

12

package com.foo;

 

/**

 * http://www.janeve.me

 */

public class Eagle implements MyBird {

 

    public String sound() {

        return "Scream";

    }

 

}


MyBirdFactory.java是负责根据传递给它的birdType输入创建所需实例的类。.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

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

        }

    }

}


确保MY_BIRDS_CONFIGURATION的值与属性文件的名称相同。


我们可以编写TestCode.java来测试代码。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

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

                }

 

            }

        }

 

    }

 

}


运行代码时签出输出。

1

2

3

4

5

6

7

8

9

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>

如你所见,这些类是根据你的输入加载的。

上文描述的就是Java使用属性文件和Reflections动态加载类的方法,具体使用情况还需要大家自己动手实验使用过才能领会。如果想了解更多相关内容,欢迎关注亿速云行业资讯频道!


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI