spring osgi helloworld

  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>
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值