如何在Equinox OSGi Container中部署Spring Boot应用程序。获取BundleException:加载bundle activator

时间:2016-12-15 18:46:17

标签: spring-boot osgi maven-3 osgi-bundle equinox

My Activator课程:

package com.package.actprovider;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceRegistration;
import com.package.Application;
import org.osgi.framework.ServiceEvent;

/**
* This class implements a simple bundle that utilizes the OSGi
* framework's event mechanism to listen for service events. Upontting 
* receiving a service event, it prints out the event's details.
**/

public class Activator implements BundleActivator , ServiceListener{
private ServiceRegistration registration;
//private Application application;

/**
 * Implements BundleActivator.start(). Prints
 * a message and adds itself to the bundle context as a service
 * listener.
 * @param context the framework context for the bundle.
**/

@Override
public void start(BundleContext context)
{
    System.out.println("Starting to listen for service events.++++");
    context.addServiceListener(this);

}

/**
 * Implements BundleActivator.stop(). Prints
 * a message and removes itself from the bundle context as a
 * service listener.
 * @param context the framework context for the bundle.
**/
@Override
public void stop(BundleContext context)
{
    context.removeServiceListener(this);
    System.out.println("Stopped listening for service events.");
}

/**
 * Implements ServiceListener.serviceChanged().
 * Prints the details of any service event from the framework.
 * @param event the fired service event.
**/
public void serviceChanged(ServiceEvent event)
{
    String[] objectClass = (String[])
        event.getServiceReference().getProperty("objectClass");

    if (event.getType() == ServiceEvent.REGISTERED)
    {
        System.out.println(
            "Ex1: Service of type " + objectClass[0] + " registered.");
    }
    else if (event.getType() == ServiceEvent.UNREGISTERING)
    {
        System.out.println(
            "Ex1: Service of type " + objectClass[0] + " unregistered.");
    }
    else if (event.getType() == ServiceEvent.MODIFIED)
    {
        System.out.println(
            "Ex1: Service of type " + objectClass[0] + " modified.");
    }
}

}

equinox容器读取的MANIFEST.MF文件:

Manifest-Version: 1.0
Bundle-Description: A bundle that displays messages at startup and whe
n service events occur
Bundle-Name: Service listener example
Bundle-Version: 1.0.0
Bundle-Activator: com.package.actprovider.Activator
Bundle-Vendor: Apache Felix
Import-Package: org.osgi.framework
Created-By: 1.8.0_101 (Oracle Corporation)

这里是Bundle-Activator:com.package.actprovider.Activator,但是在/ BOOT-INF / classes / package中创建的activator.class文件com.package.actprovider.Activator

我的pom.xml如下

http://maven.apache.org/xsd/maven-4.0.0.xsd">     4.0.0

<groupId>com.package</groupId>
<artifactId>myfoo</artifactId>
<version>0.1.0</version>
<packaging>bundle</packaging>



<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
    <relativePath />
</parent>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
            <execution>
                <phase>none</phase>
            </execution>
        </executions>
        </plugin>
        <!-- tag::plugin[] -->
        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>0.4.9</version>
            <configuration>
                <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
                <dockerDirectory>src/main/docker</dockerDirectory>
                <resources>
                    <resource>
                        <targetPath>/</targetPath>
                        <directory>${project.build.directory}</directory>
                        <include>${project.build.finalName}.jar</include>
                    </resource>
                </resources>
            </configuration>
        </plugin>


        <!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> 
            <configuration> <archive> <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile> 
            </archive> </configuration> </plugin> -->
        <plugin>
\
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>

            <configuration>
                <archive>
                    <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
                </archive>
            </configuration>
        </plugin>

    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
    </dependency>
    <dependency>
        <groupId>sdp-api-java</groupId>
        <artifactId>sdp-api-java</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.framework</artifactId>
        <version>2.0.4</version>
    </dependency>
</dependencies>

请建议如何在osgi容器中添加spring-boot bundle

1 个答案:

答案 0 :(得分:0)

Spring boot和OSGi是互斥的。你可以在一定程度上使用弹簧,但即使这样也很糟糕。

因此,最佳做法是基于声明性服务或蓝图构建OSGi应用程序。你也可以看看Apache Karaf对JPA,servlets等技术的支持......

这比春季启动要多一点努力,但你应该能够解决同样的问题。