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

这篇文章给大家分享的是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.java和Eagle.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);

 

        MyBirdbird = 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() {

        ResourceBundlerb = 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){

                MyBirdbird = 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 duckis Quack

The   sound of the bird duckis Quack

The   sound of the bird eagleis Scream

The   sound of the bird duckis Quack

The   sound of the bird eagleis Scream

The   sound of the bird eagleis Scream

 

C:\Janeve\MyBirds>

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

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

文章题目:Java如何使用属性文件和Reflections动态加载类
网页路径:https://www.cdcxhl.com/article30/jhdeso.html

成都网站建设公司_创新互联,为您提供品牌网站制作网站建设全网营销推广建站公司网站营销

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站建设网站维护公司