分层:
modual:
主要作用于包级管理与共享代码
lifecycle:
主要作用于执行期间的模块管理与訪问osgi底层框架
service:
主要作用于多模块之间的相互通信
demo:
hello-provider/pom.xml
xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.demo</groupId> <artifactId>pom</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>hello-provider</artifactId> <packaging>bundle</packaging> <name>hello-provider</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.core</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <configuration> <instructions> <Export-Package>org.hello.provider</Export-Package> <Import-Package>org.osgi.framework</Import-Package> <Bundle-Activator>org.hello.provider.impl.UserServiceActivator</Bundle-Activator> <Private-Package>org.hello.*</Private-Package> </instructions> </configuration> </plugin> </plugins> </build> </project>
hello-provider/BundleActivator:package org.hello.provider.impl;import org.hello.provider.IUserService;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;public class UserServiceActivator implements BundleActivator { public void start(BundleContext context) throws Exception { System.out.println("registerService......"); context.registerService(IUserService.class.getName(), new UserService(), null); } public void stop(BundleContext context) throws Exception { }}hello-client/pom.xml:
hello-client/BundleActivator:4.0.0 com.demo pom 0.0.1-SNAPSHOT hello-client bundle hello-client http://maven.apache.org UTF-8 junit junit org.osgi org.osgi.core com.demo hello-provider org.apache.felix maven-bundle-plugin true org.hello.provider, org.osgi.framework org.hello.client.Client com.demo.hello.* package org.hello.client;import org.hello.provider.IUserService;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import org.osgi.framework.ServiceReference;public class Client implements BundleActivator { public void start(BundleContext context) throws Exception { ServiceReference reference=context.getServiceReference(IUserService.class.getName()); System.out.println(((IUserService)context.getService(reference)).add()); } public void stop(BundleContext context) throws Exception { }}
将bundle安装到本地仓库且部署到karaf(參考前一篇)
启动bundle
通过下面命令查看bundle的id
list
通过下面命令,启动bundle
bundle:start 78參考演示样例