---
title: "OSGI学习笔记（六）"
author: "AbstractForever"
date: "2012-03-17T15:08:42Z"
tags: []
categories: ["编程语言"]
slug: "osgi"
draft: false
lastmod: "2012-03-18T12:41:26Z"
id: 1145407
url: "https://abstractforever.iteye.com/blog/1145407"
---

SpringDM初步使用（一）

官方地址[http://www.springsource.org/osgi](http://www.springsource.org/osgi)

上面提到了仓库位置：

&lt;repository&gt;  
  &lt;id&gt;spring-maven-milestone&lt;/id&gt;  
  &lt;name&gt;Springframework Maven Repository&lt;/name&gt;  
  &lt;url&gt;http://maven.springframework.org/milestone&lt;/url&gt;  
&lt;/repository&gt;

 

中文指南下载：http://forever-framework.googlecode.com/files/SpringDM\_ZH.rar

先谈哈这篇内容能给你什么，通过springDM简化osgi开发。

通过maven和eclipse的结合达到更方便的开发和测试。

谈哈这篇内容会使用到的东西：

apache-maven-3.0.3，spring-osgi-1.2.1，eclipse-jee-indigo-win32，nexus-oss-webapp-1.9.1.1-bundle.zip

下面主要介绍spring-osgi-1.2.1下面的4个例子：

   
![](http://dl.iteye.com/upload/attachment/0064/9555/79bc6ba9-4783-38bb-987c-144fabcafb61.png)

 

下面要做的就是把这4个例子正常跑起来。这几个项目需要maven的支持。

 如果直接导入这几个例子很有可能不能正常运行，大部分原因是jar不能够导入进来。所以我自己创建项目，建立pom信息。目录结构如下：

   
![](http://dl.iteye.com/upload/attachment/0064/9608/4bf09880-df25-3efa-93ca-442d31efd8c0.png)

 将simple-service-bundle和simple-service-integration-test下面的src复制到项目中。

 配置pom.xml信息,主要注意相应的jar是否引入到项目，我这里自己在nexus创建了一个springdm仓库提供所有的jar。

 simple-service-bundle引入的jar：

![](http://dl.iteye.com/upload/attachment/0065/0215/d1e74352-d79c-3f0e-a5cd-0692d4711715.png)  
 

 simple-service-integration-test引入的jar图：

![](http://dl.iteye.com/upload/attachment/0065/0217/9835a129-abbe-3c46-bb68-4e81ebeb2974.png)

整个项目很简单，普通的接口，实现类，单元测试，加入了spring进行bean的管理。

只是多了simpleservice-osgi.xml这么一个文件。为什么要分开放，方便做测试。

&lt;osgi:service id="simpleServiceOsgi" ref="simpleService"

interface="org.springframework.osgi.samples.simpleservice.MyService" /&gt;

告诉我们这个接口作为osgi里面的一个服务在运行。在simple-service-integration-test里面将看到在osgi环境里怎么获取该服务。

先将simple-service-bundle打包到本地仓库，  
 对测试类SimpleServiceBundleTest说明一哈：

```
public class SimpleServiceBundleTest extends AbstractConfigurableBundleCreatorTests {

	protected String[] getTestBundlesNames() {
		return new String[] {
			"org.springframework.osgi.samples, simple-service-bundle,1.2.1"
		};
	} 
	
	public Resource getTestingFrameworkBundlesConfiguration() {
		return new InputStreamResource(
			AbstractDependencyManagerTests.class.getResourceAsStream("/boot-bundles.properties"));
	}
	
	public void testOSGiStartedOk() {
		assertNotNull(bundleContext); 
	} 
	 
	public void testSimpleServiceExported() {
        ServiceReference ref = bundleContext.getServiceReference(MyService.class.getName());
        assertNotNull("Service Reference is null", ref);
        try {  
            MyService simpleService = (MyService) bundleContext.getService(ref);
            assertNotNull("Cannot find the service", simpleService);
            assertEquals("simple service at your service", simpleService.stringValue());
        } finally {
            bundleContext.ungetService(ref);
        }
	}
	
}
```

 

  如果要让测试类正常运行，必须先将simple-service-bundle安装在本地仓库，还有就是测试框架必须的jar，从测试运行的路径可以看出来：

startup()里面Resource\[] bundleResources = locateBundles();

会加载路径文件：/org/springframework/osgi/test/internal/boot-bundles.properties 

这个文件内容：

 #  
\# Properties file indicating the boot (or mandatory) bundles that are loaded  
\# by the testing framework.  
\#  
\# Normally, this file should not be edited since it is used by the testing infrastructure.  
\# Users that want to install bundles before starting a test, should use #bundles() method.  
\#

\#  
\# format: &lt;groupId,artifactId,version&gt;=+/-15  
\# - the optional value is used to install/remove bundles if running on JDK &gt;= 1.5  
\# - see Spring org.springframework.core.JdkVersion for jdk major version codes.

\# elements that have to be ignored should star with  
\# ignore

\# Note: inner placeholders are not supported.

\#  
\# common properties  
\#

\# versioning  
ignore.backport.version=3.1.0  
ignore.junit.version=3.8.2  
ignore.log4j.version=1.2.15-SNAPSHOT

ignore.spring.version=2.5.6.SEC01  
ignore.spring.osgi.version=1.2.1  
ignore.slf4j.version=1.5.0  
ignore.asm.version=2.2.3

\# groupIds  
ignore.spring.groupId=org.springframework  
ignore.spring.osgi.groupId=org.springframework.osgi  
ignore.slf4j.groupId=org.slf4j

\#  
\# actual libraries  
\#  
\# listed in dependency order to ease deployment

\# dependencies

\# junit  
org.junit,com.springsource.junit,${ignore.junit.version}=  
\# log4j  
${ignore.spring.osgi.groupId},log4j.osgi,${ignore.log4j.version}=  
\# slf4j (commons-logging API)  
#${ignore.slf4j.groupId},slf4j-api,${ignore.slf4j.version}=  
#${ignore.slf4j.groupId},slf4j-log4j12,${ignore.slf4j.version}=  
#${ignore.slf4j.groupId},jcl104-over-slf4j,${ignore.slf4j.version}=  
\# slf4j (BRITS)  
${ignore.slf4j.groupId},com.springsource.slf4j.api,${ignore.slf4j.version}=  
${ignore.slf4j.groupId},com.springsource.slf4j.log4j,${ignore.slf4j.version}=  
${ignore.slf4j.groupId},com.springsource.slf4j.org.apache.commons.logging,${ignore.slf4j.version}=  
\# aop alliance  
org.aopalliance,com.springsource.org.aopalliance,1.0.0=  
\# asm  
org.objectweb.asm,com.springsource.org.objectweb.asm,${ignore.asm.version}=  
\# backport concurrent  
edu.emory.mathcs.backport,com.springsource.edu.emory.mathcs.backport,${ignore.backport.version}=-15

\# spring libs  
${ignore.spring.groupId},org.springframework.beans,${ignore.spring.version}=  
${ignore.spring.groupId},org.springframework.core,${ignore.spring.version}=  
${ignore.spring.groupId},org.springframework.context,${ignore.spring.version}=  
${ignore.spring.groupId},org.springframework.aop,${ignore.spring.version}=  
${ignore.spring.groupId},org.springframework.test,${ignore.spring.version}=

\# spring osgi libs  
${ignore.spring.osgi.groupId},spring-osgi-io,${ignore.spring.osgi.version}=  
${ignore.spring.osgi.groupId},spring-osgi-core,${ignore.spring.osgi.version}=  
${ignore.spring.osgi.groupId},spring-osgi-annotation,${ignore.spring.osgi.version}=+15  
${ignore.spring.osgi.groupId},spring-osgi-extender,${ignore.spring.osgi.version}=  
${ignore.spring.osgi.groupId},spring-osgi-test,${ignore.spring.osgi.version}=

如果想修改框架默认加载的bundle，只需要重写getTestingFrameworkBundlesConfiguration方法：

这里我就改写了log4j.osgi-1.2.15-SNAPSHOT为log4j.osgi-1.2.15，你甚至可以将你自己的bundle写在里面，就不用在代码里面写了。

 完整的代码地址：[http://chenjun-java.googlecode.com/svn/spring-osgi/](http://chenjun-java.googlecode.com/svn/spring-osgi/)

 

 下面说哈怎么在eclipse的控制台运行该服务：

1.将spring-osgi-1.2.1下的lib和dist的jar放在eclipse-jee-indigo-SR2-win32\\dropins\\spring-osgi-1.2.1\\eclipse\\plugins目录下，然后重启eclipse。

2.新建一个osgi运行配置：

![](http://dl.iteye.com/upload/attachment/0065/0548/ec92fe5b-b9b0-39f3-8fae-f9e3d822f264.png)  
 

 勾上所需bundle然后run：会看到控制台出现如下界面，说明已经成功启动：

![](http://dl.iteye.com/upload/attachment/0065/0550/619bd6fa-4f5c-3c52-ac2e-eb7a098c4c0b.png)  
 可以尝试输入stop 10和start10试试看效果。