osgi系列之— .properties文件讀取

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類,下一篇再做具體介紹。

發佈了112 篇原創文章 · 獲贊 14 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章