Spring boot 怎么做带有Spring引导和XML配置的Apache Camel

Spring boot 怎么做带有Spring引导和XML配置的Apache Camel,spring-boot,apache-camel,Spring Boot,Apache Camel,我有一个应用程序,其中我使用SpringBootMVC和camel。 下面是我正在运行的代码 @ComponentScan({"com.xyz.routes","com.xyz.web","com.xyz.security"}) @SpringBootApplication //@ImportResource("classpath:META-INF/spring/camelContext.xml") public class Application { public static vo

我有一个应用程序,其中我使用SpringBootMVC和camel。 下面是我正在运行的代码

@ComponentScan({"com.xyz.routes","com.xyz.web","com.xyz.security"})
@SpringBootApplication
//@ImportResource("classpath:META-INF/spring/camelContext.xml")
public class Application {

    public static void main(String[] args) { 
        ApplicationContext applicationContext  = SpringApplication.run(Application.class, args);
    }
如果我在上面使用@ImportResource注释(当前已注释),驼峰路线不会启动

例如,下面的示例没有注释

INFO | 27 Apr 2018 15:26:31,149 | [main] org.apache.camel.spring.SpringCamelContext - Total 1 routes, of which 1 are started. |  |  | (DefaultCamelContext.java:2834)
INFO | 27 Apr 2018 15:26:31,150 | [main] org.apache.camel.spring.SpringCamelContext - Apache Camel 2.17.0 (CamelContext: camel-1) started in 0.255 seconds |  |  | (DefaultCamelContext.java:2835)
INFO | 27 Apr 2018 15:30:56,755 | [main] org.apache.camel.spring.SpringCamelContext - Total 0 routes, of which 0 are started. |  |  | (DefaultCamelContext.java:2834)
INFO | 27 Apr 2018 15:30:56,755 | [main] org.apache.camel.spring.SpringCamelContext - Apache Camel 2.17.0 (CamelContext: mtmSender) started in 0.089 seconds |  |  | (DefaultCamelContext.java:2835)
下面两行日志是在我启用注释之后

INFO | 27 Apr 2018 15:26:31,149 | [main] org.apache.camel.spring.SpringCamelContext - Total 1 routes, of which 1 are started. |  |  | (DefaultCamelContext.java:2834)
INFO | 27 Apr 2018 15:26:31,150 | [main] org.apache.camel.spring.SpringCamelContext - Apache Camel 2.17.0 (CamelContext: camel-1) started in 0.255 seconds |  |  | (DefaultCamelContext.java:2835)
INFO | 27 Apr 2018 15:30:56,755 | [main] org.apache.camel.spring.SpringCamelContext - Total 0 routes, of which 0 are started. |  |  | (DefaultCamelContext.java:2834)
INFO | 27 Apr 2018 15:30:56,755 | [main] org.apache.camel.spring.SpringCamelContext - Apache Camel 2.17.0 (CamelContext: mtmSender) started in 0.089 seconds |  |  | (DefaultCamelContext.java:2835)
你知道为什么我启用注释时这不起作用吗?注-总路线为零

请注意,我需要XML配置,因为我计划稍后在那里定义bean和属性

下面是camelContext.xml文件

    <?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:camel="http://camel.apache.org/schema/spring"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
         ">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
        <property name="locations">
            <list>
                <value>${application-default.properties}</value>
                <value>${application.properties}</value>
            </list>
        </property>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_NEVER" />
    </bean>
    <bean id="sampleBean" class="com.xyz.model.SampleBean" />

    <camelContext xmlns="http://camel.apache.org/schema/spring"  id="mtmSender">
        <properties>
            <property key="CamelLogDebugStreams" value="true"/>
        </properties>
    </camelContext>

</beans>

${application default.properties}
${application.properties}

在与OP讨论后,我们发现问题在于将ApacheCamel依赖项与SpringBoot2.0.x依赖项混合在一起

由于SpringBoot2.0.x中的一些突破性API更改,ApacheCamel还不支持SpringBoot2.0

这一点也在本文中提到

此版本仅支持Spring Boot 1.5.x。对Spring Boot 2.0.x的支持将在骆驼版本2.22中发布,该版本计划于2018年初夏发布

目前的解决方案是将SpringBoot1.5.x与ApacheCamel一起使用

POM与实际兼容的依赖项:

<dependencyManagement>
  <dependencies>
    <!-- Spring Boot BOM -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-dependencies</artifactId>
      <version>1.5.12.RELEASE</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
    <!-- Camel BOM -->
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-spring-boot-dependencies</artifactId>
      <version>2.21.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>

  <!-- Spring Boot -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>

  <!-- Camel -->
  <dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-spring-boot-starter</artifactId>
  </dependency>
  <dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-stream-starter</artifactId>
  </dependency>

  <!-- Test -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-test-spring</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <version>1.5.12.RELEASE</version>
      <executions>
        <execution>
          <goals>
            <goal>repackage</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

org.springframework.boot
spring启动依赖项
1.5.12.1发布
聚甲醛
进口
org.apache.camel
camel-spring引导依赖项
2.21.0
聚甲醛
进口
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧靴起动器下拖
org.springframework.boot
弹簧靴起动器执行器
org.apache.camel
驼形弹簧靴起动器
org.apache.camel
骆驼流起动器
org.springframework.boot
弹簧起动试验
测试
org.apache.camel
驼峰试验弹簧
测试
org.springframework.boot
springbootmaven插件
1.5.12.1发布
重新包装

能否尝试将
com.xyz.routes
添加到camelContext.xml的
部分?我假设您的意思是com.xyz.routes是正确的?我试过了,但没用。为了尝试,我还单独尝试了com.xyz.routes,但也没有成功。我尝试用您的代码复制它,并为我工作。日志中有错误吗?这是我的工作样本,谢谢你的例子。我注意到您的路由是xml格式的,而不是DSL格式的。我还尝试将路由定义放在xml中,这很有效。但是DSL中的那个不起作用。看起来组件扫描没有检测到它。在我的路线中,我在类前面有@Component注释,我正在扩展FatJarRouter类。谢谢!你的例子帮助了我。我使用SpringBootVersion2.0.1(作为父版本)并使用它派生所有JAR。我还实现了FatJarRouter。有一次,我将我的spring boot版本更改为1.5.10.RELEASE,并使用您导入的方式导入了camel。使用org.apache.camel-spring引导依赖项2.21.0POM导入,它运行良好。