開心生活站

位置:首頁 > IT科技 > 

java,bundle

IT科技1.11W

<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>

java bundle是什麼,讓我們一起了解一下?

Bundle是OSGi中的一個外掛,OSGi框架規範是OSGi規範的核心部分,提供了一個通用的、安全可管理的Java框架,通過這個框架,可以支援 Bundle 服務應用的部署和擴充套件。

Bundle 之間可以通過 Import Package 和 Require-Bundle 來共享 Java 類,在 OSGi 服務平臺中,使用者通過開發 Bundle 來提供需要的功能,這些 Bundle 可以動態載入和解除安裝,或者根據需要遠端下載和升級。

實戰操作,以如何在JAVA中使用ResourceBundle為例:

1、新建4個屬性檔案

my_en_US.properties:cancelKey=cancel。

my_zh_CN.properties:cancelKey=u53D6u6D88(取消)。

my_zh.properties:cancelKey=u53D6u6D88zh(取消zh)。

my.properties:cancelKey=u53D6u6D88default(取消default)。

java bundle

2、獲取bundle

ResourceBundle bundle = ResourceBundle.getBundle("res", new Locale("zh", "CN"));

其中new Locale(“zh”, “CN”)提供本地化資訊,上面這行程式碼,程式會首先在classpath下尋找my_zh_CN.properties檔案,若my_zh_CN.properties檔案不存在,則取找my_zh.properties,如還是不存在,繼續尋找my.properties,若都找不到就丟擲異常。

3、具體程式碼

import javax.annotation.Resource;import java.util.Locale;import java.util.ResourceBundle;/** * @author OovEver * 2018/1/14 22:12 */public class Main {    public static void main(String args[]) {        ResourceBundle bundle = ResourceBundle.getBundle("my", new Locale("zh", "CN"));        String cancel = bundle.getString("cancelKey");        System.out.println(cancel);        bundle = ResourceBundle.getBundle("my", Locale.US);        cancel = bundle.getString("cancelKey");        System.out.println(cancel);        bundle = ResourceBundle.getBundle("my", Locale.getDefault());        cancel = bundle.getString("cancelKey");        System.out.println(cancel);        bundle = ResourceBundle.getBundle("my", Locale.GERMAN);        cancel = bundle.getString("cancelKey");        System.out.println(cancel);        bundle = ResourceBundle.getBundle("my");        for (String key : bundle.keySet()) {            System.out.println(bundle.getString(key));        }    }}

標籤:bundle java