网上找了很多例子,看了很多资料发现都不能完全重现一个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负载,数据挖掘,机器学习等方向