osgi和spring区别_OSGI和Spring动态模块–简单的Hello World

osgi和spring区别

在此姿势中,我们将采用使用OSGi进行的第一个实现,并使用Spring Dynamic Modules改进应用程序。

Spring动态模块(Spring Dm)使基于OSGi的应用程序的开发更加容易。 这样,服务的部署就容易得多。 您可以像其他任何Spring Bean一样注入服务。

因此,让我们从Spring dm开始。

首先,您需要下载Spring Dm Distribution 。 在本文中,我使用了具有依赖关系的发行版,而我将仅使用以下库:

com.springsource.net.sf.cglib-2.1.3.jar
com.springsource.org.aopalliance-1.0.0.jar
log4j.osgi-1.2.15-SNAPSHOT.jar
com.springsource.slf4j.api-1.5.0.jar
com.springsource.slf4j.log4j-1.5.0.jar
com.springsource.slf4j.org.apache.commons.logging-1.5.0.jar
org.springframework.aop-2.5.6.SEC01.jar
org.springframework.beans-2.5.6.SEC01.jar
org.springframework.context-2.5.6.SEC01.jar
org.springframework.core-2.5.6.SEC01.jar
spring-osgi-core-1.2.1.jar
spring-osgi-extender-1.2.1.jar
spring-osgi-io-1.2.1.jar

当然,您可以将Spring 2.5.6库替换为Spring 3.0库。 但是对于本文而言,Spring 2.5.6就足够了。

因此,从服务捆绑开始。 回想一下,该捆绑软件导出了一项服务:

package com.bw.osgi.provider.able;public interface HelloWorldService {void hello();
}
package com.bw.osgi.provider.impl;import com.bw.osgi.provider.able.HelloWorldService;public class HelloWorldServiceImpl implements HelloWorldService {@Overridepublic void hello(){System.out.println("Hello World !");}
}

这里没有要做的任何更改。 现在,我们可以看到激活器:

package com.bw.osgi.provider;import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;import com.bw.osgi.provider.able.HelloWorldService;
import com.bw.osgi.provider.impl.HelloWorldServiceImpl;public class ProviderActivator implements BundleActivator {private ServiceRegistration registration;@Overridepublic void start(BundleContext bundleContext) throws Exception {registration = bundleContext.registerService(HelloWorldService.class.getName(),new HelloWorldServiceImpl(),null);}@Overridepublic void stop(BundleContext bundleContext) throws Exception {registration.unregister();}
}

因此,在这里,我们将简单化。 让我们删除这个类,它对于Spring Dm不再有用。

我们将让Spring Dm为我们导出捆绑包。 我们将为此捆绑包创建一个Spring上下文。 我们只需要在文件夹META-INF / spring中创建一个文件provider-context.xml即可。 这是XML文件中的简单上下文,但是我们使用新的名称空间注册服务“ http://www.springframework.org/schema/osgi ”。 因此,让我们开始:

<?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"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/osgihttp://www.springframework.org/schema/osgi/spring-osgi.xsd"><bean id="helloWorldService" class="com.bw.osgi.provider.impl.HelloWorldServiceImpl"/><osgi:service ref="helloWorldService" interface="com.bw.osgi.provider.able.HelloWorldService"/>
</beans>

OSGi特有的唯一内容是osgi:service声明。 此行表明我们使用接口HelloWorldService作为服务名称将helloWorldService注册为OSGi服务。

如果将上下文文件放在META-INF / spring文件夹中 ,Spring Extender将自动检测到它,并创建一个应用程序上下文。

现在,我们可以转到消费者捆绑包。 在第一阶段,我们创建了该消费者:

package com.bw.osgi.consumer;import javax.swing.Timer;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import com.bw.osgi.provider.able.HelloWorldService;public class HelloWorldConsumer implements ActionListener {private final HelloWorldService service;private final Timer timer;public HelloWorldConsumer(HelloWorldService service) {super();this.service = service;timer = new Timer(1000, this);}public void startTimer(){timer.start();}public void stopTimer() {timer.stop();}@Overridepublic void actionPerformed(ActionEvent e) {service.hello();}
}

目前,这里没有任何更改。 可以使用@Resource注释代替构造函数的注入,但这在Spring 2.5.6和Spring Dm中不起作用(但在Spring 3.0中很好用)。

现在激活器:

package com.bw.osgi.consumer;import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;import com.bw.osgi.provider.able.HelloWorldService;public class HelloWorldActivator implements BundleActivator {private HelloWorldConsumer consumer;@Overridepublic void start(BundleContext bundleContext) throws Exception {ServiceReference reference = bundleContext.getServiceReference(HelloWorldService.class.getName());consumer = new HelloWorldConsumer((HelloWorldService) bundleContext.getService(reference));consumer.startTimer();}@Overridepublic void stop(BundleContext bundleContext) throws Exception {consumer.stopTimer();}
}

不再需要注射。 我们可以在此处保持计时器的启动,但是再一次,我们可以使用框架的功能来启动和停止计时器。 因此,让我们删除激活器并创建一个应用程序上下文以创建使用者并自动启动它,并将其放入META-INF / spring文件夹中

<?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"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/osgihttp://www.springframework.org/schema/osgi/spring-osgi.xsd"><bean id="consumer" class="com.bw.osgi.consumer.HelloWorldConsumer" init-method="startTimer" destroy-method="stopTimer"lazy-init="false" ><constructor-arg ref="eventService"/></bean><osgi:reference id="eventService" interface="com.bw.osgi.provider.able.HelloWorldService"/>
</beans>

我们使用了init-method和destroy-method属性来开始和停止框架的时间,并使用构造函数arg注入对服务的引用。 使用osgi:reference字段并使用接口作为服务的键来获取对该服务的引用。

这就是我们与该捆绑包有关的全部。 比第一个版本简单得多吗? 除了简化之外,您还可以看到源不依赖于OSGi或Spring Framework,这是纯Java语言,这是一个很大的优势。

Maven POM与第一阶段的相同,只是我们可以减少对osgi的依赖。

提供者:

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion><groupId>OSGiDmHelloWorldProvider</groupId><artifactId>OSGiDmHelloWorldProvider</artifactId><version>1.0</version><packaging>bundle</packaging><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.0.2</version><configuration><source>1.6</source><target>1.6</target></configuration></plugin><plugin><groupId>org.apache.felix</groupId><artifactId>maven-bundle-plugin</artifactId><extensions>true</extensions><configuration><instructions><Bundle-SymbolicName>OSGiDmHelloWorldProvider</Bundle-SymbolicName><Export-Package>com.bw.osgi.provider.able</Export-Package><Bundle-Vendor>Baptiste Wicht</Bundle-Vendor></instructions></configuration></plugin></plugins></build> 
</project>

消费者:

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion><groupId>OSGiDmHelloWorldConsumer</groupId><artifactId>OSGiDmHelloWorldConsumer</artifactId><version>1.0</version><packaging>bundle</packaging><dependencies><dependency><groupId>OSGiDmHelloWorldProvider</groupId><artifactId>OSGiDmHelloWorldProvider</artifactId><version>1.0</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.0.2</version><configuration><source>1.6</source><target>1.6</target></configuration></plugin><plugin><groupId>org.apache.felix</groupId><artifactId>maven-bundle-plugin</artifactId><extensions>true</extensions><configuration><instructions><Bundle-SymbolicName>OSGiDmHelloWorldConsumer</Bundle-SymbolicName><Bundle-Vendor>Baptiste Wicht</Bundle-Vendor></instructions></configuration></plugin></plugins></build>
</project>

我们可以使用maven install构建两个捆绑包。 因此,让我们在Felix中测试我们的东西:

wichtounet@Linux-Desktop:~/Desktop/osgi/felix$ java -jar bin/felix.jar
_______________
Welcome to Apache Felix Gogog! install file:../com.springsource.slf4j.org.apache.commons.logging-1.5.0.jar
Bundle ID: 5
g! install file:../com.springsource.slf4j.log4j-1.5.0.jar
Bundle ID: 6
g! install file:../com.springsource.slf4j.api-1.5.0.jar
Bundle ID: 7
g! install file:../log4j.osgi-1.2.15-SNAPSHOT.jar
Bundle ID: 8
g! install file:../com.springsource.net.sf.cglib-2.1.3.jar
Bundle ID: 9
g! install file:../com.springsource.org.aopalliance-1.0.0.jar
Bundle ID: 10
g! install file:../org.springframework.core-2.5.6.SEC01.jar
Bundle ID: 11
g! install file:../org.springframework.context-2.5.6.SEC01.jar
Bundle ID: 12
g! install file:../org.springframework.beans-2.5.6.SEC01.jar
Bundle ID: 13
g! install file:../org.springframework.aop-2.5.6.SEC01.jar
Bundle ID: 14
g! install file:../spring-osgi-extender-1.2.1.jar
Bundle ID: 15
g! install file:../spring-osgi-core-1.2.1.jar
Bundle ID: 16
g! install file:../spring-osgi-io-1.2.1.jar
Bundle ID: 17
g! start 5 7 8 9 10 11 12 13 14 15 16 17
log4j:WARN No appenders could be found for logger (org.springframework.osgi.extender.internal.activator.ContextLoaderListener).
log4j:WARN Please initialize the log4j system properly.
g! install file:../OSGiDmHelloWorldProvider-1.0.jar
Bundle ID: 18
g! install file:../OSGiDmHelloWorldConsumer-1.0.jar
Bundle ID: 19
g! start 18
g! start 19
g! Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
stop 19
g!

如您所见,它运行完美!

总之,Spring Dm确实使使用OSGi的开发更加容易。 使用Spring Dm,您还可以启动捆绑包。 它还使您能够制作Web捆绑包并轻松使用OSGi纲要的服务。

以下是这两个项目的来源:

  • OSGiDmHelloWorldProvider来源
  • OSGiDmHelloWorldConsumer来源

这是两个内置的Jars:

  • OSGiDmHelloWorldProvider-1.0.jar
  • OSGiDmHelloWorldConsumer-1.0.jar

这是完整的文件夹,包括Felix和Spring Dm: osgi-hello-world.tar.gz

参考: OSGI和Spring动态模块–我们的JCG合作伙伴 Baptiste Wicht的 @ @Blog(“ Baptiste Wicht”)提供的 简单Hello World 。

相关文章 :
  • OSGi –具有服务的简单Hello World
  • OSGi将Maven与Equinox结合使用
  • 真正的模块化Web应用程序:为什么没有开发标准?
  • Java模块化方法–模块,模块,模块

翻译自: https://www.javacodegeeks.com/2011/11/osgi-and-spring-dynamic-modules-simple.html

osgi和spring区别

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/358976.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql binary blob区别_SQL中binary 和 varbinary的区别 blob

binary 和 varbinary固定长度 (binary) 的或可变长度 (varbinary) 的 binary 数据类型。binary [ ( n ) ]固定长度的 n 个字节二进制数据。N 必须从 1 到 8,000。存储空间大小为 n4 字节。varbinary [ ( n ) ]n 个字节变长二进制数据。n 必须从 1 到 8,000。存储空间大小为实际…

线上测试bug工具

根据每个公司性质的不同&#xff0c;规模的不同&#xff0c;所用到的bug管理工具也可能不同。你们用的bug管理工具是什么呢&#xff1f; 1. JIRA&#xff08;付费&#xff09; IRA的生产者把JIRA定义为Professional Issue Tracker&#xff0c;即它是一个专业的问题跟踪管理的…

关于bolg

云笔记挺好用的&#xff0c;博客不更了。转载于:https://www.cnblogs.com/usedrosee/p/4434618.html

Java Micro Framework:您无法忽略的新趋势

什么是Java微框架&#xff0c;为什么要使用它们&#xff1f; 每种语言都有权衡。 对于Java&#xff0c;要成为一种安全&#xff0c;经过严格测试&#xff0c;向后兼容的语言&#xff0c;就要在敏捷性和精简性方面做出一些牺牲。 毫无疑问&#xff0c;它有一些冗长和冗长的内容…

springboot mysql时区设置_java/springboot/mysql时区问题解决方案

解决步骤&#xff1a;先理清楚逻辑数据从mysql查出-->进入docker(没容器化跳过)-->进入相应的应用程序(比如java或者框架springboot之类的)-->程序处理完输出返回给前端或者页面-->展示第一步:先查mysql所以先进入mysql控制台&#xff0c;select now(),时区没问题则…

无法获取签名信息,请上传有效包(110506)

此篇文章将要介绍安卓App提交应用商店时遇到的两个小问题的相关介绍&#xff0c;具体代码请看下文 陆陆续续做了一个半月左右的「喵呜天气」终于在今天下午成功提交到应用商店&#xff08;腾讯应用宝&#xff09;。期间遇到两个小问题&#xff0c;记录如下&#xff1a; 1、上…

工作一个月有感

一年前的自己肯定想不到现在的工作内容&#xff0c;那时候的自己还抱着 effective c&#xff0c;刷着 leetcode&#xff0c;准备实习生面试呢。我一直以为自己毕业后要做 c 服务器端开发&#xff0c;而现在已与当初的想法越走越远了&#xff0c;我把书架上 c 的书收了起来&…

mysql 数据如何存储,MySQL如何存储数据

I looked around google but didnt find any good answers. Does it store the data in one big file? What methods does it use to make data access quicker them just reading and writing to a regular file?解决方案Does it store the data in one big file?Some DBMS…

openshift_红帽Openshift:入门–云中的Java EE6

openshift现在有一段时间&#xff0c;我正在研究“云”。 研究它的功能&#xff0c;它可以做什么&#xff0c;为什么我们应该切换到“云”&#xff0c;进行交谈&#xff0c;与Realmaolmen的云专家maartenballiauw等人交谈。 我已经在Google App Engine&#xff08;用于Java&…

【APICloud系列|10】最新苹果APP上架App Store流程(超详细)

2018最新整理iOS app上架app详细教程 上架iOS需要一个付费688的开发者账号,还没有的话申请一个或者借用。 申请苹果开发者账号教程 上架App Store之前是先安装到苹果手机测试调试好,app能正常运行再上架

.Net面试经验,从北京到杭州

首先简单说下&#xff0c;本人小本&#xff0c;目前大四软件工程专业&#xff0c;大三阴差阳错地选了.Net方向&#xff0c;也是从大三开始接触.Net。自认为在学生中.net基础还可以&#xff0c;嘿嘿&#xff0c;吹一下。 大四第一学期学校安排去北京培训&#xff0c;培训了两个月…

proxifier访问https错误_教你实现IE访问https网站不出错方法

不同的系统出现的问题是不一样的&#xff0c;有小伙伴在电脑账打开我们的网站的时候发现不能正常的打开我们的电脑出现&#xff0c;是否只查看安全传送的网页内容的错误的提示&#xff0c;那我们遇到这个问题应该怎么处理访问https网站呢&#xff0c;今天小编就来跟大家分享一下…

Gradle入门:集成测试

因为Java项目的标准项目布局仅定义了一个测试目录&#xff08; src / test &#xff09;&#xff0c;所以我们没有将集成测试添加到Gradle构建中的标准方法。 如果要使用标准项目布局&#xff0c;则可以使用以下选项之一将集成测试添加到Gradle构建中&#xff1a; 我们可以将…

【APICloud系列|11】使用APPuploader申请ios开发证书及ios发布证书教程

开发证书用于app测试。申请ios开发证书 发布证书用于上架。ios发布证书 我开发的APP使用APICloud,简单走一下编译的流程&#xff0c;然后直接上架到APP store.完整的开发&#xff0c;window电脑&#xff0c;安卓手机&#xff0c;苹果手机&#xff0c;mac电脑还是很有必要的。…

orchard mysql_如何在Orchard CMS 1.3.10中使用MySQL数据库?

您正在讨论的错误是因为DatabaseOptions属性是一个布尔值。您需要更改该属性以接受字符串值。安装控制器中有一些地方需要更改该属性的使用方式...但是&#xff0c;最重要的部分是实现DataServicesProvider。我将我添加到核心&#xff0c;但我认为你可以把它作为一个功能放在设…

UIButton-初识IOS

今天&#xff0c;我学到了所有app经常用到的UIButton控件&#xff0c;废话不多说&#xff0c;这些都是我学习的时候总结的一些&#xff0c;希望可以帮到以后的初学者&#xff0c;IOS初学不应该直接拖拽&#xff0c;感觉不易于理解&#xff0c;所以我总结的基本上全是纯代码编辑…

【APICloud系列|12】ios真机调试时如何添加新设备的udid?

很多开发者在真机调试测试ios应用时,会看到需要添加udid,搞不清这个是什么东西应该怎么获取。 udid就是手机的一个串号,相当于手机的身份证,具有唯一性。 下面介绍如何获取udid并添加到开发者中心后台。 有两种方式可以获取udid ios app真机调试到上架App Store完整…

python rtf转txt_将DOC、RTF格式文件批量转为TXT格式文件

Windows 下将目录下所有文件下的文件 拷贝到一个目录中&#xff1a;搜索"."安装python 及 win32com模块 pip install pypiwin32创建LDA_RTF_TXT.pyfrom glob import globimport shutilimport osimport warningswarnings.filterwarnings(actionignore,categoryUserWar…

记录常用工具

查看没有用过的资源 Improving Your Code with lint 转载于:https://www.cnblogs.com/a0000/p/4443712.html

UA Web挑战会议:针对初创公司的SpringIO

在本周&#xff0c;我在“后端”部分的UA Web挑战会议上发表了讲话。 我在演讲中选择了一个奇怪的话题-“面向初创企业的SpringIO”。 结果是什么&#xff1f; 我将在下面总结。 在过去三年中&#xff0c;我开发了各种个人Web项目。 我再也没有称它们为“初创企业”&#xff0…