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