javascript
OSGI和Spring动态模块–简单的Hello World
Spring動態(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)出了一項服務(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();} }因此,在這里,我們將簡單化。 讓我們刪除這個類,它對于Spring Dm不再有用。
我們將讓Spring Dm為我們導(dǎo)出捆綁包。 我們將為此捆綁包創(chuàng)建一個Spring上下文。 我們只需要在文件夾META-INF / spring中創(chuàng)建一個文件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將自動檢測到它,并創(chuàng)建一個應(yīng)用程序上下文。
現(xiàn)在,我們可以轉(zhuǎn)到消費者捆綁包。 在第一階段,我們創(chuàng)建了該消費者:
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();} }不再需要注射。 我們可以在這里保持計時器的啟動,但是再一次,我們可以使用框架的功能來啟動和停止計時器。 因此,讓我們刪除激活器并創(chuàng)建一個應(yīng)用程序上下文以創(chuà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屬性來開始和停止框架的時間,并使用構(gòu)造函數(shù)arg注入對服務(wù)的引用。 使用osgi:reference字段并使用接口作為服務(wù)的鍵來獲取對該服務(wù)的引用。
這就是我們與該捆綁包有關(guān)的全部。 比第一個版本簡單得多嗎? 除了簡化之外,您還可以看到源不依賴于OSGi或Spring Framework,這是純Java語言,這是一個很大的優(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>消費者:
<?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)建兩個捆綁包。 因此,讓我們在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的開發(fā)更加容易。 使用Spring Dm,您還可以啟動捆綁包。 它還使您可以制作Web捆綁包并輕松使用OSGi綱要的服務(wù)。
以下是這兩個項目的來源:
- OSGiDmHelloWorldProvider來源
- OSGiDmHelloWorldConsumer來源
這是兩個內(nèi)置的Jars:
- OSGiDmHelloWorldProvider-1.0.jar
- OSGiDmHelloWorldConsumer-1.0.jar
這是完整的文件夾,包括Felix和Spring Dm: osgi-hello-world.tar.gz
參考: OSGI和Spring動態(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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ddos攻击软件手机版(ddos攻击手机
- 下一篇: 工伤备案超时情况说明模板(工伤备案超时情