access common property file inside bundle with osgi

自作多情 提交于 2019-12-04 11:47:50

With OSGi, there isn't a universal application classpath. Although the properties are on the classpath of the bundle which contains them, they're not necessarily on the classpath of the bundles using them.

It's a little bit ugly, but in general exporting the 'package' containing the property folders will make them accessible. In this case it looks like that would be '.', which is very ugly, but you could put them in a 'properties' directory (say), and then export a properties package. Bundles which use those properties would also need to import the properties package.

Alternatively, using the containing bundle's classloader to look up the resource will work, although I can't comment on what the Spring config for that would look like.

The best way to read common properties across all bundles is to use compendium service which is provided by spring DM.

I find that there is rarely a case where the properties file should be 'common'. Can't you break the contents of the properties file up and place them in the bundles that actually require them. The properties should be grouped by context and placed in the bundle which requires that context.

Similar in theory to not using a constants class or interface, which is considered an anti-pattern.

Another alternative here which I finally used - is placing property files to filesystem and just referencing them from spring (or other code) as file:path/to/common.props.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!