溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java如何使用屬性文件和Reflections動(dòng)態(tài)加載類

發(fā)布時(shí)間:2020-06-02 21:25:16 來(lái)源:億速云 閱讀:255 作者:Leah 欄目:編程語(yǔ)言

這篇文章給大家分享的是Java使用屬性文件和Reflections動(dòng)態(tài)加載類的方法。小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí)。如下資料是關(guān)于Reflections動(dòng)態(tài)加載類實(shí)現(xiàn)內(nèi)容。

MyBirds示例

讓我們從一個(gè)非常簡(jiǎn)單的問(wèn)題陳述開(kāi)始:指定特定鳥(niǎo)的名字后,我應(yīng)該能夠加載它的字符。 例如:當(dāng)我指定鴨子時(shí),調(diào)用sound()函數(shù)應(yīng)顯示“ quack”;

在這種情況下,你需要根據(jù)客戶端或外部源提供的某些數(shù)據(jù)動(dòng)態(tài)加載類。 你還希望靈活地在簡(jiǎn)單的屬性文件中配置類,并且這些類具有類似的行為。


為了實(shí)現(xiàn)這一點(diǎn),我們將需要以下組件:

·         mybirds.properties ——可在其中將鍵映射到類的屬性文件。

·         MyBird.java ——屬性文件中指定的所有類都必須實(shí)現(xiàn)的接口。

·         Duck.java, Eagle.java ——實(shí)現(xiàn)接口MyBird的類。

·         MyBirdFactory.java ——?jiǎng)討B(tài)創(chuàng)建類的工廠類


讓我們看一下這些代碼。讓我們從mybirds.properties開(kāi)始。

1

2

3

#BIRD-TYPE=IMPLEMENTATION-CLASS

duck=com.foo.Duck

eagle=com.foo.Eagle


現(xiàn)在,MyBird.java接口聲明了子類應(yīng)實(shí)現(xiàn)的方法。

1

2

3

4

5

6

7

8

package com.foo;

 

/**

 * http://www.janeve.me

 */

public interface MyBird {

    public String sound();

}


現(xiàn)在讓我們看看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是負(fù)責(zé)根據(jù)傳遞給它的birdType輸入創(chuàng)建所需實(shí)例的類。.

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的值與屬性文件的名稱相同。


我們可以編寫(xiě)TestCode.java來(lái)測(cè)試代碼。

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

                }

 

            }

        }

 

    }

 

}


運(yùn)行代碼時(shí)簽出輸出。

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>

如你所見(jiàn),這些類是根據(jù)你的輸入加載的。

上文描述的就是Java使用屬性文件和Reflections動(dòng)態(tài)加載類的方法,具體使用情況還需要大家自己動(dòng)手實(shí)驗(yàn)使用過(guò)才能領(lǐng)會(huì)。如果想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道!


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI