在 Karaf 错误上部署 Spring

Deploy Spring on Karaf error

我正在尝试 运行 Spring 在 karaf 上。

当我尝试 运行 时:

public class SnifferActivator implements BundleActivator {

    @Override
    public void start(BundleContext bundleContext) throws Exception {
        System.out.println("Start");
       new Thread(new Sniffer()).start();
    }

    @Override
    public void stop(BundleContext bundleContext) throws Exception {

    }
}

  public class Sniffer implements Runnable{

            @Override
            public void run() {
            ApplicationContext context = new AnnotationConfigApplicationContext(SnifferConfig.class);
            }
    }

我收到以下错误消息:

karaf@root()> Exception in thread "Thread-305" java.lang.IllegalStateException:
Cannot load configuration class: com.emot.worker.configuration.SnifferConfig
        at org.springframework.context.annotation.ConfigurationClassPostProcesso
r.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:403)
        at 

我检查了我在 Karaf 上部署的包,这个 class 在那里。

当我尝试在 Karaf 4 中使用 Spring 4.x 到一个包中时,我遇到了同样的问题,大部分问题是因为包没有导入所有 spring 包,可能是插件 "maven-bundle-plugin" 根据语法分析导出必要的包,并且 spring 通过反射在运行时加载许多 classes。这些是我的步骤:

  1. 在 Karaf 4.x

    中安装 Spring 4.x
    • 功能:安装spring/4.3.12.RELEASE_1
  2. 列出 spring 包的

    导出的包
    • 捆绑:列表-t 0 | grep spring
    • bundle:bundle:headers --no-uses 55
  3. 将软件包(如果需要,可以添加版本)添加到 pom 中插件 maven-bundle-plugin 配置中的 "Import-Package"。在我的例子中,我从 spring-core / spring-context / spring-beans 导入了所有包这样,你的包将导入所有包,所以我知道我是在我的 poc 中添加了比我需要的更多的包,但是当 spring class 在运行时加载时,很难确定你的应用程序将在所有场景中使用的所有 spring 包通过反射.

<Import-Package>org.springframework.asm;version=4.3.12.RELEASE, org.springframework.cglib;version=4.3.12.RELEASE, org.springframework.cglib.beans;version=4.3.12.RELEASE, org.springframework.cglib.core;version=4.3.12.RELEASE, org.springframework.cglib.core.internal;version=4.3.12.RELEASE,
  org.springframework.cglib.proxy;version=4.3.12.RELEASE, org.springframework.cglib.reflect;version=4.3.12.RELEASE, org.springframework.cglib.transform;version=4.3.12.RELEASE, org.springframework.cglib.transform.impl;version=4.3.12.RELEASE, org.springframework.cglib.util;version=4.3.12.RELEASE,
  org.springframework.core;version=4.3.12.RELEASE, org.springframework.core.annotation;version=4.3.12.RELEASE, org.springframework.core.convert;version=4.3.12.RELEASE, org.springframework.core.convert.converter;version=4.3.12.RELEASE, org.springframework.core.convert.support;version=4.3.12.RELEASE,
  org.springframework.core.env;version=4.3.12.RELEASE, org.springframework.core.io;version=4.3.12.RELEASE, org.springframework.core.io.support;version=4.3.12.RELEASE, org.springframework.core.serializer;version=4.3.12.RELEASE, org.springframework.core.serializer.support;version=4.3.12.RELEASE,
  org.springframework.core.style;version=4.3.12.RELEASE, org.springframework.core.task;version=4.3.12.RELEASE, org.springframework.core.task.support;version=4.3.12.RELEASE, org.springframework.core.type;version=4.3.12.RELEASE, org.springframework.core.type.classreading;version=4.3.12.RELEASE,
  org.springframework.core.type.filter;version=4.3.12.RELEASE, org.springframework.lang;version=4.3.12.RELEASE, org.springframework.objenesis;version=4.3.12.RELEASE, org.springframework.objenesis.instantiator;version=4.3.12.RELEASE, org.springframework.objenesis.instantiator.android;version=4.3.12.RELEASE,
  org.springframework.objenesis.instantiator.annotations;version=4.3.12.RELEASE, org.springframework.objenesis.instantiator.basic;version=4.3.12.RELEASE, org.springframework.objenesis.instantiator.gcj;version=4.3.12.RELEASE, org.springframework.objenesis.instantiator.perc;version=4.3.12.RELEASE,
  org.springframework.objenesis.instantiator.sun;version=4.3.12.RELEASE, org.springframework.objenesis.instantiator.util;version=4.3.12.RELEASE, org.springframework.objenesis.strategy;version=4.3.12.RELEASE, org.springframework.util;version=4.3.12.RELEASE,
  org.springframework.util.backoff;version=4.3.12.RELEASE, org.springframework.util.comparator;version=4.3.12.RELEASE, org.springframework.util.concurrent;version=4.3.12.RELEASE, org.springframework.util.xml;version=4.3.12.RELEASE, org.springframework.beans;version=4.3.12.RELEASE,
  org.springframework.beans.annotation;version=4.3.12.RELEASE, org.springframework.beans.factory;version=4.3.12.RELEASE, org.springframework.beans.factory.access;version=4.3.12.RELEASE, org.springframework.beans.factory.access.el;version=4.3.12.RELEASE,
  org.springframework.beans.factory.annotation;version=4.3.12.RELEASE, org.springframework.beans.factory.config;version=4.3.12.RELEASE, org.springframework.beans.factory.groovy;version=4.3.12.RELEASE, org.springframework.beans.factory.parsing;version=4.3.12.RELEASE,
  org.springframework.beans.factory.serviceloader;version=4.3.12.RELEASE, org.springframework.beans.factory.support;version=4.3.12.RELEASE, org.springframework.beans.factory.wiring;version=4.3.12.RELEASE, org.springframework.beans.factory.xml;version=4.3.12.RELEASE,
  org.springframework.beans.propertyeditors;version=4.3.12.RELEASE, org.springframework.beans.support;version=4.3.12.RELEASE, org.springframework.cache;version=4.3.12.RELEASE, org.springframework.cache.annotation;version=4.3.12.RELEASE, org.springframework.cache.concurrent;version=4.3.12.RELEASE,
  org.springframework.cache.config;version=4.3.12.RELEASE, org.springframework.cache.interceptor;version=4.3.12.RELEASE, org.springframework.cache.support;version=4.3.12.RELEASE, org.springframework.context;version=4.3.12.RELEASE, org.springframework.context.access;version=4.3.12.RELEASE,
  org.springframework.context.annotation;version=4.3.12.RELEASE, org.springframework.context.config;version=4.3.12.RELEASE, org.springframework.context.event;version=4.3.12.RELEASE, org.springframework.context.expression;version=4.3.12.RELEASE, org.springframework.context.i18n;version=4.3.12.RELEASE,
  org.springframework.context.support;version=4.3.12.RELEASE, org.springframework.context.weaving;version=4.3.12.RELEASE, org.springframework.ejb.access;version=4.3.12.RELEASE, org.springframework.ejb.config;version=4.3.12.RELEASE, org.springframework.ejb.interceptor;version=4.3.12.RELEASE,
  org.springframework.format;version=4.3.12.RELEASE, org.springframework.format.annotation;version=4.3.12.RELEASE, org.springframework.format.datetime;version=4.3.12.RELEASE, org.springframework.format.datetime.joda;version=4.3.12.RELEASE, org.springframework.format.datetime.standard;version=4.3.12.RELEASE,
  org.springframework.format.number;version=4.3.12.RELEASE, org.springframework.format.number.money;version=4.3.12.RELEASE, org.springframework.format.support;version=4.3.12.RELEASE, org.springframework.instrument.classloading;version=4.3.12.RELEASE,
  org.springframework.instrument.classloading.glassfish;version=4.3.12.RELEASE, org.springframework.instrument.classloading.jboss;version=4.3.12.RELEASE, org.springframework.instrument.classloading.tomcat;version=4.3.12.RELEASE, org.springframework.instrument.classloading.weblogic;version=4.3.12.RELEASE,
  org.springframework.instrument.classloading.websphere;version=4.3.12.RELEASE, org.springframework.jmx;version=4.3.12.RELEASE, org.springframework.jmx.access;version=4.3.12.RELEASE, org.springframework.jmx.export;version=4.3.12.RELEASE, org.springframework.jmx.export.annotation;version=4.3.12.RELEASE,
  org.springframework.jmx.export.assembler;version=4.3.12.RELEASE, org.springframework.jmx.export.metadata;version=4.3.12.RELEASE, org.springframework.jmx.export.naming;version=4.3.12.RELEASE, org.springframework.jmx.export.notification;version=4.3.12.RELEASE,
  org.springframework.jmx.support;version=4.3.12.RELEASE, org.springframework.jndi;version=4.3.12.RELEASE, org.springframework.jndi.support;version=4.3.12.RELEASE, org.springframework.remoting;version=4.3.12.RELEASE, org.springframework.remoting.rmi;version=4.3.12.RELEASE,
  org.springframework.remoting.soap;version=4.3.12.RELEASE, org.springframework.remoting.support;version=4.3.12.RELEASE, org.springframework.scheduling;version=4.3.12.RELEASE, org.springframework.scheduling.annotation;version=4.3.12.RELEASE, org.springframework.scheduling.concurrent;version=4.3.12.RELEASE,
  org.springframework.scheduling.config;version=4.3.12.RELEASE, org.springframework.scheduling.support;version=4.3.12.RELEASE, org.springframework.scripting;version=4.3.12.RELEASE, org.springframework.scripting.bsh;version=4.3.12.RELEASE, org.springframework.scripting.config;version=4.3.12.RELEASE,
  org.springframework.scripting.groovy;version=4.3.12.RELEASE, org.springframework.scripting.jruby;version=4.3.12.RELEASE, org.springframework.scripting.support;version=4.3.12.RELEASE, org.springframework.stereotype;version=4.3.12.RELEASE, org.springframework.ui;version=4.3.12.RELEASE,
  org.springframework.ui.context;version=4.3.12.RELEASE, org.springframework.ui.context.support;version=4.3.12.RELEASE, org.springframework.validation;version=4.3.12.RELEASE, org.springframework.validation.annotation;version=4.3.12.RELEASE, org.springframework.validation.beanvalidation;version=4.3.12.RELEASE,
  org.springframework.validation.support;version=4.3.12.RELEASE, org.springframework.cache.caffeine;version=4.3.12.RELEASE, org.springframework.cache.ehcache;version=4.3.12.RELEASE, org.springframework.cache.guava;version=4.3.12.RELEASE, org.springframework.cache.jcache;version=4.3.12.RELEASE,
  org.springframework.cache.jcache.config;version=4.3.12.RELEASE, org.springframework.cache.jcache.interceptor;version=4.3.12.RELEASE, org.springframework.cache.transaction;version=4.3.12.RELEASE, org.springframework.mail;version=4.3.12.RELEASE, org.springframework.mail.javamail;version=4.3.12.RELEASE,
  org.springframework.scheduling.commonj;version=4.3.12.RELEASE, org.springframework.scheduling.quartz;version=4.3.12.RELEASE, org.springframework.ui.freemarker;version=4.3.12.RELEASE, org.springframework.ui.jasperreports;version=4.3.12.RELEASE, org.springframework.ui.velocity;version=4.3.12.RELEASE,
  org.springframework.expression;version=4.3.12.RELEASE, org.springframework.expression.common;version=4.3.12.RELEASE, org.springframework.expression.spel;version=4.3.12.RELEASE, org.springframework.expression.spel.ast;version=4.3.12.RELEASE, org.springframework.expression.spel.generated;version=4.3.12.RELEASE,
  org.springframework.expression.spel.standard;version=4.3.12.RELEASE, org.springframework.expression.spel.support;version=4.3.12.RELEASE, org.springframework.aop;version=4.3.12.RELEASE, org.springframework.aop.aspectj;version=4.3.12.RELEASE, org.springframework.aop.aspectj.annotation;version=4.3.12.RELEASE,
  org.springframework.aop.aspectj.autoproxy;version=4.3.12.RELEASE, org.springframework.aop.config;version=4.3.12.RELEASE, org.springframework.aop.framework;version=4.3.12.RELEASE, org.springframework.aop.framework.adapter;version=4.3.12.RELEASE, org.springframework.aop.framework.autoproxy;version=4.3.12.RELEASE,
  org.springframework.aop.framework.autoproxy.target;version=4.3.12.RELEASE, org.springframework.aop.interceptor;version=4.3.12.RELEASE, org.springframework.aop.scope;version=4.3.12.RELEASE, org.springframework.aop.support;version=4.3.12.RELEASE, org.springframework.aop.support.annotation;version=4.3.12.RELEASE,
  org.springframework.aop.target;version=4.3.12.RELEASE, org.springframework.aop.target.dynamic;version=4.3.12.RELEASE</Import-Package>

有了它,你的bundle就可以使用spring 4.x,但是你必须手动创建应用程序上下文,我在bean的定义中使用了参数"init-method"在蓝图定义中,我不喜欢它,但它有效,无论如何,我正在寻找更优雅的方式。

PD:对不起我的英语。