论坛首页 Java企业应用论坛

spring osgi helloworld

浏览 5131 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-05-03  
  1. 1.准备环境:

 

下载felix,

运行felix.jar:org.apache.felix.main.Main函数

如何加入spring osgi
-> start file:///home/jiangyy/workspace/spring-osgi-1.0.2/lib/slf4j-api-1.4.3.jar
-> start file:///home/jiangyy/workspace/spring-osgi-1.0.2/lib/jcl104-over-slf4j-1.4.3.jar
-> start file:///home/jiangyy/workspace/spring-osgi-1.0.2/lib/spring-core-2.5.1.jar
-> start file:///home/jiangyy/workspace/spring-osgi-1.0.2/lib/spring-beans-2.5.1.jar
-> start file:///home/jiangyy/workspace/spring-osgi-1.0.2/lib/spring-context-2.5.1.jar
-> start file:///home/jiangyy/workspace/spring-osgi-1.0.2/lib/spring-aop-2.
-> start file:///home/jiangyy/workspace/spring-osgi-1.0.2/lib/aopalliance.osgi-1.0-SNAPSHOT.jar5.1.jar

-> start file:///home/jiangyy/workspace/spring-osgi-1.0.2/dist/spring-osgi-core-1.0.2.jar
-> start file:///home/jiangyy/workspace/spring-osgi-1.0.2/dist/spring-osgi-extender-1.0.2.jar
-> start file:///home/jiangyy/workspace/spring-osgi-1.0.2/dist/spring-osgi-io-1.0.2.jar



test (the spring samples service):以下测试见2,3步骤
-> start file:///home/jiangyy/workspace/spring-osgi-1.0.2/src/samples/simple-service/simple-service-bundle/target/simple-service-bundle-1.0.2.jar
-> start file:///home/jiangyy/workspace/spring-osgi-1.0.2/src/samples/simple-service/simple-service-bundle/target/simple-service-bundle-1.0.2.test.jar
 

 

2.定义服务:

下载spring-osgi包,找到sample/simpleservice,到src相对应目录下,运行:

mvn clean install,打包后将生成:

simple-service-bundle-1.0.2.jar

这个可以注册了

 

3.在其他jar 服务中注入服务

如何在其他jar中注入MyService实现:

写了个类,相当于spring bean,然后把刚才打好的包拷贝,改成以下的class文件,

package org.springframework.osgi.samples.simpleservicetest;

import org.springframework.osgi.samples.simpleservice.MyService;

public class MyTest {
     public void setMyService(MyService service) {
    	 System.out.println("invokeing");
    	 System.out.println(service.stringValue());
    	 System.out.println("end");
     }
}

MANIFEST.MF中引入 需要的包 。在META-INF中添加spring /bean.xml,并定义该测试 类:

xml 代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xmlns:osgi="http://www.springframework.org/schema/osgi"

  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

                      http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">



<osgi:reference id="simpleServiceOsgi" interface="org.springframework.osgi.samples.simpleservice.MyService"/> 
<bean id="testEntityRegister" class="org.springframework.osgi.samples.simpleservicetest.MyTest">
   <property name="myService" ref="simpleServiceOsgi" />
</bean>


</beans>
  修改.MF文件,然后在Felix中start:


4.附录

spring sampleservice的MANIFEST.MF文件:

Bundle-Version: 1.0
Bundle-SymbolicName: org.springframework.osgi.samples.simpleservice
Bundle-Name: Simple-Service-Sample
Bundle-Vendor: Spring Framework
Export-Package: org.springframework.osgi.samples.simpleservice
Bundle-ManifestVersion: 2

META-INF/springsample.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

  <!-- regular spring configuration file defining simple service
       bean. We've kept the osgi definitions in a separate 
       configuration file so that this file can easily be used
       for testing outside of an OSGi environment -->

  <bean name="simpleService" class="org.springframework.osgi.samples.simpleservice.impl.MyServiceImpl" />

  <osgi:service id="simpleServiceOsgi" ref="simpleService"
    interface="org.springframework.osgi.samples.simpleservice.MyService" />

</beans>
 

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics