[OSGi] OSGi + Spring + Web Demo [1]

本文结合《OSGi原理与最佳实践》及公司同事的《OSGi开发》资源,详细阐述了如何使用OSGi框架创建并发布Web服务的全过程。通过创建`timeserviceBundle`,实现了输出时间的功能,并配置了Spring框架进行服务的发布。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网上找了很多例子,看了很多资料发现都不能完全重现一个OSGi的Web Demo
本文结合了《OSGi原理与最佳实践》+公司同事的《OSGi开发》+自己的理解。

环境WindowXP + eclipse3.4 + Spring OSGi1.2.1
1. 创建timeservice Bundle,提供输出时间的功能
1.1 eclipse内创建Maven Project,选quickstart project:timeservice,package org.osgichina.demo.timeservice
1.2 创建好后,remove maven的jar包,删掉project中的App和AppTest,在项目上右键->PDE Tools->Convert
1.3

package org.osgichina.demo.timeservice;

public interface TimeService {
String getCurrentTime();
}

 

public class TimeServiceImpl implements TimeService {

public String getCurrentTime() {
return ( new Date()).toString();
}

}


1.4 Spring配置
配置放在META-INF/spring目录下,框架启动时会按照这个路径去寻找配置文件
timeservice.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" >

<!-- timeService bean -->
< bean name ="timeService" class ="org.osgichina.demo.timeservice.impl.TimeServiceImpl" />

</ beans >


timeservice-osgi.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"
>

<!-- 将timeService 这个bean发布为OSGi服务的定义 -->
< osgi:service id ="osgiTimeService" ref ="timeService" interface ="org.osgichina.demo.timeservice.TimeService" >
</ osgi:service >

</ beans >


这个Bundle到此已经完成了,下一节会讲解helloworldweb Bundle,涉及Import Package,OSGi的启动,Spring+Web的配置等

专注于Java,数据库性能,Web Server负载,数据挖掘,机器学习等方向

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值