I'm trying to run Spring on karaf.
When I am trying to run this:
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);
}
}
I've got the following error message:
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
I was checked the package that I deploy on Karaf and this class is there.
I had the same issue when I tried to use Spring 4.x into a bundle in Karaf 4, the most of the problems it's because the bundle is not importing all the spring packages, probably the plugin "maven-bundle-plugin" exports the necessary packages based on syntactic analysis and spring loads many classes in runtime by reflection. These are my steps:
Install Spring 4.x in Karaf 4.x
Make a list of the packages exported by the spring bundle's
Add the packages (with the version if you want) to the "Import-Package" in the config of the plugin maven-bundle-plugin in your pom. In my case, I imported all package from spring-core / spring-context / spring-beans In this way, your bundle will import all the packages, So I knowing that I am adding more packages than I needed in my poc, but it's a bit it is difficult to determine all the spring packages that your application will use in all scenarios when spring class are loaded in runtime by reflection.
<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>
With that, your bundle is able to use spring 4.x, but you have to create the application context manually, I used the parameter "init-method" in the definition of a bean in the blueprint definition, I do not like it, but it works, anyway, I'm looking for a more elegant way.
PD:Sorry for my english.