编程语言
首页 > 编程语言> > 以Spring Java配置方式实例化Camel时对BundleContextAware的运行时依赖性

以Spring Java配置方式实例化Camel时对BundleContextAware的运行时依赖性

作者:互联网

为什么需要BundleContextAware?

2013-09-17 13:54:09 ERROR ContextLoader:319 - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'camelContext' defined in class path resource [com/myorg/asr/camel/configuration/CamelConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.apache.camel.CamelContext org.apache.camel.spring.javaconfig.CamelConfiguration.camelContext() throws java.lang.Exception] threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/osgi/context/BundleContextAware
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:581)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1025)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:921)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:656)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1635)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.apache.camel.CamelContext org.apache.camel.spring.javaconfig.CamelConfiguration.camelContext() throws java.lang.Exception] threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/osgi/context/BundleContextAware
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:181)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:570)
    ... 28 more
Caused by: java.lang.NoClassDefFoundError: org/springframework/osgi/context/BundleContextAware
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2895)
    at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1173)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1681)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2895)
    at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1173)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1681)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at com.myorg.asr.camel.configuration.CamelConfig.createCamelContext(CamelConfig.java:25)
    at org.apache.camel.spring.javaconfig.CamelConfiguration.camelContext(CamelConfiguration.java:138)
    at com.myorg.asr.camel.configuration.CamelConfig$$EnhancerByCGLIB$$7498e1c7.CGLIB$camelContext$13(<generated>)
    at com.myorg.asr.camel.configuration.CamelConfig$$EnhancerByCGLIB$$7498e1c7$$FastClassByCGLIB$$cfbc542f.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:286)
    at com.myorg.asr.camel.configuration.CamelConfig$$EnhancerByCGLIB$$7498e1c7.camelContext(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:160)
    ... 29 more
Caused by: java.lang.ClassNotFoundException: org.springframework.osgi.context.BundleContextAware
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    ... 57 more

配置类

package com.myorg.asr.camel.configuration;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.log.LogComponent;
import org.apache.camel.osgi.SpringCamelContextFactory;
import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * A simple example router.
 *
 * @version
 */
@Configuration
public class CamelConfig extends SingleRouteCamelConfiguration implements InitializingBean {

    /**
     * Returns the CamelContext which support OSGi
     */
    @Override
    protected CamelContext createCamelContext() throws Exception {
        SpringCamelContextFactory factory = new SpringCamelContextFactory();
        factory.setApplicationContext(getApplicationContext());
        return factory.createContext();
    }

    @Override
    protected void setupCamelContext(CamelContext camelContext) throws Exception {
        // add the log component
        camelContext.addComponent("log", new LogComponent());

        // setup the ActiveMQ component
        //ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        //connectionFactory.setBrokerURL("vm://localhost.spring.javaconfig?marshal=false&broker.persistent=false&broker.useJmx=false");

        // and register it into the CamelContext
        //JmsComponent answer = new JmsComponent();
        //answer.setConnectionFactory(connectionFactory);
        //camelContext.addComponent("jms", answer);
    }


    public static class SomeBean {

        public void someMethod(String body) {
            System.out.println("Received: " + body);
        }

    }

    @Bean
    @Override
    public RouteBuilder route() {
        return new RouteBuilder() {
            public void configure() {
                // you can configure the route rule with Java DSL here

                // populate the message queue with some messages
                from("file:src/data?noop=true").
                        to("jms:test.MyQueue");

                from("jms:test.MyQueue").
                        to("file://target/test?noop=true");

                // set up a listener on the file component
                from("file://target/test?noop=true").
                        bean(new SomeBean());
            }
        };
    }

    public void afterPropertiesSet() throws Exception {
        // just to make SpringDM happy do nothing here
    }

}

编辑

经过一番(轻微)的挖掘,似乎SpringCamelContextFactory扩展了一个实现BundleContextAware的类.我可以包括以下依赖项:

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-spring-osgi</artifactId>
  <version>2.3.0</version>
  <scope>runtime</scope>
</dependency>

但是最新版本-版本2.3.0-依赖于某些Spring 2.5.6库,这些库似乎太旧并且缺少某些类.这意味着我在启动时会得到以下信息:

Caused by: java.lang.NoClassDefFoundError: org/springframework/core/env/EnvironmentCapable

也许我可以使用其他一些类来代替SpringCamelContextFactory吗?

解决方法:

我对这个问题的解决方法是不使用SpringCamelContextFactory而是自己实例化一个SpringCamelContext:

/**
 * Returns the CamelContext
 */
@Override
protected CamelContext createCamelContext() throws Exception {
    // TODO Broken because of dependency on BundleContextAware
    //SpringCamelContextFactory factory = new SpringCamelContextFactory();
    //factory.setApplicationContext(getApplicationContext());
    //return factory.createContext();

    return new SpringCamelContext(getApplicationContext());
}

CamelContextFactory实现BundleContextAware或SpringCamelContextFactory扩展CamelContextFactory可能是一个错误,但是我怀疑是前者.如果骆驼专家想启发我,请这样做.

标签:maven,apache-camel,spring
来源: https://codeday.me/bug/20191030/1967693.html