您的位置:首页 > 运维架构

osgi系列之— .properties文件读取

2015-10-21 15:06 232 查看

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类,下一篇再做具体介绍。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: