无法将spring组件自动装配到camel组件中

时间:2017-06-13 11:48:43

标签: java spring apache-camel

我猜测整合我的春天和骆驼的背景是错误的。

我正在对Camel组件进行单元测试,我试图注入一个由spring组件扫描生成的bean(MainframeEncoderProvider)。我可以看到正在构造bean(打破一个init块),我可以将它自动装入ToFalinkProducerTest,但它没有进入驼峰组件。该组件通过META-INF自动发现实例化,如果相关(http://camel.apache.org/how-do-i-add-a-component.html

测试类设置:

@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:spring-config/testContext.xml"})
public class ToFalinkProducerTest extends CamelTestSupport{

[some tests...]

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        public void configure() {
            //@formatter:off

            from("direct:start")
                    .to("tofalink:x?encoderName=test")
                    .to("mock:result");
            //@formatter:on
        }
    };
}

的TestContext:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd

        http://camel.apache.org/schema/spring
        http://camel.apache.org/schema/spring/camel-spring.xsd
        ">

    <!-- lightweight testcontext -->
    <context:annotation-config/>
    <context:component-scan base-package="net.mo"/>

    <camel:camelContext>
            <camel:contextScan/>
    </camel:camelContext>


</beans>

成分:

/**
 * Represents the component that manages {@link ToFalinkEndpoint}.
 */
public class ToFalinkComponent extends DefaultComponent {

    @Autowired
    private MainframeEncoderProvider provider;

    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
        Endpoint endpoint = new ToFalinkEndpoint(uri, this);
        setProperties(endpoint, parameters);
        return endpoint;
    }

    protected MainframeEncoderProvider getProvider() {
        return this.provider;
    }
}

1 个答案:

答案 0 :(得分:0)

在经过大量的努力之后管理解决这个问题。用CamelSpringTestSupport替换了CamelTestSupport扩展,并删除了@RunWith和@ContextConfiguration。使用我的xml spring上下文文件实现createApplicationContext(),以保留组件扫描。

让自动发现机制到位。

现在我只需要看看它是否在运行时工作:/