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

本文探讨了OSGI和Spring的区别,尤其是Spring动态模块如何简化基于OSGI的应用开发。通过一个简单的Hello World示例,展示了如何在Spring DM中创建和消费服务,强调了Spring DM如何使得服务部署和注入更方便,以及如何利用OSGI服务进行注册。此外,还提到了使用Maven构建OSGI捆绑包并在Felix中测试的过程。

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 {
    @Override
    public 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;
 
    @Override
    public void start(BundleContext bundleContext) throws Exception {
        registration = bundleContext.registerService(
                HelloWorldService.class.getName(),
                new HelloWorldServiceImpl(),
                null);
    }
 
    @Override
    public 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/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/osgi
            http://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();
    }
 
    @Override
    public 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;
 
    @Override
    public void start(BundleContext bundleContext) throws Exception {
        ServiceReference reference = bundleContext.getServiceReference(HelloWorldService.class.getName());
 
        consumer = new HelloWorldConsumer((HelloWorldService) bundleContext.getService(reference));
        consumer.startTimer();
    }
 
    @Override
    public 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/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/osgi
            http://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 Gogo

g! 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纲要的服务。

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

这是两个内置的Jars:

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

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

相关文章 :

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

osgi和spring区别

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值