osgi系列之— .properties文件读取

来源:互联网 发布:工厂仿真软件 编辑:程序博客网 时间:2024/05/29 15:09

bundle A 加载osgi context中所有bundle的

application文件夹下.properties的键值对方法


  每个bundle独有一个classLoader,在运行环境中,所以考虑将所有bundle的properties中的属性键值对,放置在全局Util类中,供程序使用。

具体方法:

private static String CONFIG_PATH = "configuration";private static String FilePattern = "*.properties";public void init() throws Exception {Bundle[] bds = bundle.getBundleContext().getBundles();for (Bundle b : bds) {Enumeration<URL> e = b.findEntries(CONFIG_PATH, FilePattern, true);if (e != null) {while (e.hasMoreElements()) {URL url = e.nextElement();if (url != null) {InputStream in = url.openStream();try {properties.load(in);} finally {in.close();}}}}}}

在启动osgi framework时,Activator的start()方法中,调用上述方法,实现加载所有的properties。
Activator.context = bundleContext;Bundle bundle   = context.getBundle();String bundleName = bundle.getSymbolicName();FrameworkPropertyHolder propertyHolder = new FrameworkPropertyHolder();propertyHolder.setBundle(bundle);propertyHolder.init();



osgi继承spring后,启动spring容器时,在配置文件中,需要解析通配符${xxx.name}.

需要实现spring的PropertyPlaceholderConfigurer类,下一篇再做具体介绍。

0 0
原创粉丝点击