OSGI和Spring动态模块–简单的Hello World
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”)的 Simple 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动态模块–简单的Hello World相关推荐
- osgi和spring区别_OSGI和Spring动态模块–简单的Hello World
osgi和spring区别 在此姿势中,我们将采用使用OSGi进行的第一个实现,并使用Spring Dynamic Modules改进应用程序. Spring动态模块(Spring Dm)使基于OSG ...
- java osgi 文件_OSGi 系列(一)之什么是 OSGi :Java 语言的动态模块系统
OSGi 系列(一)之什么是 OSGi :Java 语言的动态模块系统 OSGi 的核心:模块化.动态.基于 OSGi 就可以模块化的开发 java 应用,模块化的部署 java 应用,还可以动态管理 ...
- 基于OSGi的Virgo Server最简单Spring web实例
一:开发工具下载与环境搭建 1.下载并安装JDK6u30版本,下载地址如下: http://www.oracle.com/technetwork/java/javasebusiness/downloa ...
- java osgi web开发_基于 OSGi 和 Spring 开发 Web 应用
开发一个简单的OSGi Web应用实例 一个简单的Web应用 我们写一个简单的 Web 应用 compute.html :计算两个数字的和或乘积.如下图所示: 图1.一个简单例子 一个简单例子.bmp ...
- #Spring代理的简单例子#
Spring AOP详解(转载) 一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地 ...
- 【Spring】模块
[Spring]模块 文章目录 [Spring]模块 一.系统架构 1.Core Container 2.AOP/Aspects/Instrument 3.Data Access/Integerati ...
- 基于 SOFAArk 和 SOFADashboard 实现动态模块管控 | Meetup#2 回顾
作者:卫恒(宋国磊) SOFATracer 以及 SOFADashboard 开源负责人. 本文根据 5月26日 SOFA Meetup#2上海站 <使用 SOFAStack 快速构建微服务&g ...
- 一文读懂Spring动态配置多数据源---源码详细分析
Spring动态多数据源源码分析及解读 一.为什么要研究Spring动态多数据源 代云小说网 https://www.3187.info 期初,最开始的原因是:想将答题服务中发送主观题答题数据给批 ...
- springboot导入项目依赖报错_使用Spring Boot很简单,go!!!
Spring Boot依赖 使用Spring Boot很简单,先添加基础依赖包,有以下两种方式 1. 继承spring-boot-starter-parent项目 org.springframe ...
最新文章
- Android输入输出机制之来龙去脉之前生后世
- JsonMappingException:找不到类型[simple type,class]的合适构造函数:无法从JSON对象实例化
- 【C 语言】二级指针作为输入 ( 指针数组 | 复杂指针解读 )
- 编程三角形面积公式_三角形面积公式110式(英文版)
- mysql show full_mysql: show full processlist 详解
- React Hook “useState“ is called in function xx which is neither a React function component or
- excel中的不同类型图表叠加
- Eclipse控制台输出信息的控制
- Windows 下安装 Redis 1
- 论文笔记_S2D.75_2021-CoRL_TANDEM_基于深度多视图立体视觉的实时跟踪和稠密建图
- Jsp链接传值中文乱码问题解决
- 大量监控视频如何存储?
- UltraEdit 注册机使用激活方法:
- 面试官又问我Select * 为什么效率低下?
- 公募基金资格:社保、养老金、企业年金,三者有什么区别?
- 信道、频段带宽等术语简介
- 计算机视觉、图像等领域一些著名牛人和实验室(附网址)
- 2020-2022年最全各省、市矢量和栅格数据(土地利用、植被覆盖、土壤类型、人口密度、NPP、气象水文数据、路网、省市县乡镇区划边界、poi数据、河流水系、建筑轮廓)
- 小技巧(1):Ububtu18.04中.z01 .z02 .z03此类拆分后压缩文件解压缩办法(以及常用解分卷压缩方法)
- java企查查爬_爬取企查查热搜
热门文章
- spring-boot--整合thymeleaf模板
- qt弹簧教程_弹簧启动执行器教程
- 子类重写父类变量_为什么在子类中不重写超类的实例变量
- smpp客户端_SMPP Java示例(客户端)
- javafx css_JavaFX缺少的功能调查:CSS
- 性能测试流程_流性能
- java 拼图_我最喜欢的Java拼图2 + 1 = 4
- jbpm 和 drools_Drools和jBPM KIE A​​pps平台
- java jpa 规范_Java:在JPA中使用规范模式
- Java程序员应在2018年学习的3种JVM语言