当前位置:科普知识站>IT科技>

java|bundle

IT科技 阅读(1.14W)

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