---
title: "OSGI"
author: "harbey"
date: "2012-02-11T22:25:52Z"
tags: []
categories: ["编程语言"]
slug: "osgi"
draft: false
lastmod: "2012-02-12T17:34:08Z"
id: 1402975
url: "https://harbey.iteye.com/blog/1402975"
---

1、场景：
搭建Spring-DM环境的时候，在导入com.springsource.org.aopalliance、org.srpingframework.aop、org.springframework.beans、org.springframework.context、org.springframework.context.support、org.springframework.core这几个bundle并且
在run-configurations对话框中选中target platform下org.apache.commons.logging(1.0.4.v201005080501)bundle。

在启动OSGI Framework完框架后，用ss命令查看bundle运行状态时，会显示上一步导入的bundle为install状态，而不是active状态。
使用指令启动bundle，start <bundleid>(代表bundle的id值),发现报告如下错误

Import-Package: org.apache.commons.logging; version="[1.1.1,2.0.0)"

原因：OSGI框架对bundle的version的版本也有过滤约束，[1.1.1,2.0.0)表示logging的版本号要大于等于1.1.1版本并且小于2.0.0版本。而我们上面导入的target platform的版本不在这个范围之内，所以报错没启动，状态为install。

解决：1、可以改每个bundle对于logging的依赖版本。
2、参见http://flym.iteye.com/blog/677873的描述。导入如下bundle即可：com.springsource.slf4j.org.apache.commons.logging-1.5.6.jar  
com.springsource.slf4j.api-1.5.6.jar  
com.springsource.slf4j.log4j-1.5.6.jar  
com.springsource.org.apache.log4j-1.2.15.jar 

2、修改Target Platform的内容：
场景：在使用Spring-DM来实现osgi应用的时候，网上很多的做法都是将Spring-DM的lib和dist里的bundle导入到workspace，这样exploer内容就很多，找项目很麻烦。如是想找一种方法，把bundle都加入到Target Platform中，这样，在配置OSGI FRAMEWORK运行的时候，就在Target Framework中选即可。

解决：window -- preferences -- Plug-in Development -- Target Platform , 选中 “Running Platform(Active)”,点击“Edit...”,在“locations“书签下，点击“Add...”,在此加入Spring DM2的dist和lib目录即可。 

3、入门可参考 http://nopainnogain.iteye.com/category/106515