3

为什么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吗?

4

2 回答 2

3

我对这个问题的解决方法是不使用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实现BundleContextAwareSpringCamelContextFactory扩展CamelContextFactory......一个或另一个,但我怀疑前者。如果骆驼专家想启发我,请做。

于 2013-09-17T14:37:58.947 回答
1

我使用了这些依赖项,它似乎有效

    <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>org.osgi.core</artifactId>
        <version>4.2.0</version>
        <type>jar</type>
    </dependency>

    <dependency>
        <groupId>org.springframework.osgi</groupId>
        <artifactId>spring-osgi-core</artifactId>
        <version>1.0</version>
    </dependency>

如果你愿意,试试吧!

于 2014-02-05T11:37:45.620 回答