日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

OSGI和Spring动态模块–简单的Hello World

發(fā)布時(shí)間:2023/12/3 javascript 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OSGI和Spring动态模块–简单的Hello World 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在此姿勢中,我們將采用使用OSGi進(jìn)行的第一個(gè)實(shí)現(xiàn),并使用Spring Dynamic Modules改進(jìn)應(yīng)用程序。

Spring動(dòng)態(tài)模塊(Spring Dm)使基于OSGi的應(yīng)用程序的開發(fā)更加容易。 這樣,服務(wù)的部署就容易得多。 您可以像其他任何Spring bean一樣注入服務(wù)。

因此,讓我們從Spring dm開始。

首先,您需要下載Spring Dm Distribution 。 在本文中,我使用了具有依賴關(guān)系的發(fā)行版,而我將僅使用以下庫:

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

當(dāng)然,您可以將Spring 2.5.6庫替換為Spring 3.0庫。 但是對于本文而言,Spring 2.5.6就足夠了。

因此,從服務(wù)捆綁開始。 回想一下,該捆綁軟件導(dǎo)出了一項(xiàng)服務(wù):

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 !");} }

這里沒有要做的任何更改。 現(xiàn)在,我們可以看到激活器:

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();} }

因此,在這里,我們將簡單化。 讓我們刪除這個(gè)類,它對于Spring Dm不再有用。

我們將讓Spring Dm為我們導(dǎo)出捆綁包。 我們將為此捆綁包創(chuàng)建一個(gè)Spring上下文。 我們只需要在文件夾META-INF / spring中創(chuàng)建一個(gè)文件provider-context.xml即可。 這是XML文件中的簡單上下文,但是我們使用新的名稱空間注冊服務(wù)“ 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特有的唯一內(nèi)容是osgi:service聲明。 此行表明我們使用接口HelloWorldService作為服務(wù)名稱將helloWorldService注冊為OSGi服務(wù)。

如果將上下文文件放在META-INF / spring文件夾中 ,Spring Extender將自動(dòng)檢測到它,并創(chuàng)建一個(gè)應(yīng)用程序上下文。

現(xiàn)在,我們可以轉(zhuǎn)到消費(fèi)者捆綁包。 在第一階段,我們創(chuàng)建了該消費(fèi)者:

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注釋代替構(gòu)造函數(shù)的注入,但這在Spring 2.5.6和Spring Dm中不起作用(但在Spring 3.0中很好用)。

現(xiàn)在激活器:

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();} }

不再需要注射。 我們可以在這里保持計(jì)時(shí)器的啟動(dòng),但是再一次,我們可以使用框架的功能來啟動(dòng)和停止計(jì)時(shí)器。 因此,讓我們刪除激活器并創(chuàng)建一個(gè)應(yīng)用程序上下文以創(chuàng)建使用者并自動(dòng)啟動(dòng)它,并將其放入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屬性來開始和停止框架的時(shí)間,并使用構(gòu)造函數(shù)arg注入對服務(wù)的引用。 使用osgi:reference字段并使用接口作為服務(wù)的鍵來獲取對該服務(wù)的引用。

這就是我們與該捆綁包有關(guān)的全部。 比第一個(gè)版本簡單得多嗎? 除了簡化之外,您還可以看到源不依賴于OSGi或Spring Framework,這是純Java語言,這是一個(gè)很大的優(yōu)勢。

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>

消費(fèi)者:

<?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構(gòu)建兩個(gè)捆綁包。 因此,讓我們在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!

如您所見,它運(yùn)行完美!

總之,Spring Dm確實(shí)使使用OSGi的開發(fā)更加容易。 使用Spring Dm,您還可以啟動(dòng)捆綁包。 它還使您可以制作Web捆綁包并輕松使用OSGi綱要的服務(wù)。

以下是這兩個(gè)項(xiàng)目的來源:

  • OSGiDmHelloWorldProvider來源
  • OSGiDmHelloWorldConsumer來源

這是兩個(gè)內(nèi)置的Jars:

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

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

參考: OSGI和Spring動(dòng)態(tài)模塊–來自我們的JCG合作伙伴 Baptiste Wicht的 @ @Blog(“ Baptiste Wicht”)的 Simple Hello World 。

相關(guān)文章 :
  • OSGi –帶有服務(wù)的簡單Hello World
  • OSGi將Maven與Equinox結(jié)合使用
  • 真正的模塊化Web應(yīng)用程序:為什么沒有開發(fā)標(biāo)準(zhǔn)?
  • Java模塊化方法–模塊,模塊,模塊

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

總結(jié)

以上是生活随笔為你收集整理的OSGI和Spring动态模块–简单的Hello World的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。