本教程使用相关工具:
目标:教程演示使用Maven构建,采用Spring DM开发一个最基本的OSGI组件,并将组件运行于ServiceMix容器中。
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.micai.testgroupId>
<artifactId>osgi-hello-liyongartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>bundlepackaging>
<name>osgi-hello-liyongname>
<url>http://maven.apache.orgurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<spring.version>4.3.12.RELEASEspring.version>
<felix.plugin.version>3.2.0felix.plugin.version>
<osgi.version>4.2.0osgi.version>
<spring.osgi.version>1.2.1spring.osgi.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
<scope>compilescope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
<scope>compilescope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring.version}version>
<scope>compilescope>
dependency>
<dependency>
<groupId>org.springframework.osgigroupId>
<artifactId>spring-osgi-coreartifactId>
<version>${spring.osgi.version}version>
<exclusions>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>org.springframework.coreartifactId>
exclusion>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>org.springframework.aopartifactId>
exclusion>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>org.springframework.beansartifactId>
exclusion>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>org.springframework.contextartifactId>
exclusion>
<exclusion>
<groupId>org.aopalliancegroupId>
<artifactId>com.springsource.org.aopallianceartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.osgigroupId>
<artifactId>spring-osgi-webartifactId>
<version>${spring.osgi.version}version>
<scope>providedscope>
<exclusions>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>org.springframework.coreartifactId>
exclusion>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>org.springframework.aopartifactId>
exclusion>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>org.springframework.beansartifactId>
exclusion>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>org.springframework.contextartifactId>
exclusion>
<exclusion>
<groupId>javax.servletgroupId>
<artifactId>com.springsource.javax.servletartifactId>
exclusion>
<exclusion>
<groupId>org.aopalliancegroupId>
<artifactId>com.springsource.org.aopallianceartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.osgigroupId>
<artifactId>spring-osgi-extenderartifactId>
<version>${spring.osgi.version}version>
<scope>providedscope>
<exclusions>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>org.springframework.coreartifactId>
exclusion>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>org.springframework.aopartifactId>
exclusion>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>org.springframework.beansartifactId>
exclusion>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>org.springframework.contextartifactId>
exclusion>
exclusions>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<target>1.6target>
<source>1.6source>
configuration>
plugin>
<plugin>
<groupId>org.apache.felixgroupId>
<artifactId>maven-bundle-pluginartifactId>
<version>${felix.plugin.version}version>
<configuration>
<instructions>
<Import-Package>
*
Import-Package>
<Karaf-Commands>*Karaf-Commands>
instructions>
configuration>
<extensions>trueextensions>
plugin>
plugins>
build>
project>
依赖引入关键点:
<plugin>
<groupId>org.apache.felixgroupId>
<artifactId>maven-bundle-pluginartifactId>
<version>${felix.plugin.version}version>
<configuration>
<instructions>
<Import-Package>
*
Import-Package>
<Karaf-Commands>*Karaf-Commands>
instructions>
configuration>
<extensions>trueextensions>
plugin>
使用这个插件后,我们就不用再配置OSGI相关的描述文件了,所有OSGI的属性该插件已经在打包的时候为我们添加完成,我们只需要开发自己的业务就行了。
<packaging>bundlepackaging>
这点是与我们正常的的项目不同地方,正常项目我们都是配置pom、jar、war等
说明:如果在你的代码中报错:Project build error: Unknown packaging: bundle,那么仅需要在plugin中添加
即可
true
package com.micai.test.osgi.hello;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.stereotype.Service;
@Service
public class HelloWorldService {
@PostConstruct
public void init() {
System.out.println("Bean初始化");
}
@PreDestroy
public void destroy() {
System.out.println("Bean销毁");
}
}
配置文件名称没有特殊要求,但是放置的位置必须在指定位置
必须放置于classpath下面的: META-INF/spring目录中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd">
<context:component-scan base-package="com.micai.test.osgi.hello" />
beans>
该出配置没有什么特殊的配置,仅仅配置了包扫描功能。我们的简单组件就开发完成了。
直接通过maven命令
mvn install
进行打包安装到本地,这里因为我使用 mvn build打包报错,用install反而好使了,暂时没时间纠结为什么。。。
将打好的jar包上传到自己准备好的ServiceMix容器环境的发布目录:
发布目录:在ServiceMix的安装目录中的 deploy文件夹中。
karaf@root>Bean初始化
karaf@root>bundle:list | grep "osgi-hello-liyong"
265 | Active | 80 | 0.0.1.SNAPSHOT | osgi-hello-liyong
karaf@root>bundle:stop 265
Bean销毁
karaf@root>bundle:start 265
karaf@root>Bean初始化
当上传成功后,组件就自动重启了,即spring容器也初始化了,由于我们直接使用打印命令打印内容到控制台了,那么在我们的ServiceMix的控制台上面就可以看到Bean初始化时打印的消息。
当我们使用相关命令停止该bundle时,spring容器被销毁了,bean也就销毁了,所以执行了我们bean配置中的销毁方法。