骆驼春天javaconfig

时间:2016-06-09 21:44:50

标签: java spring apache-camel maven-plugin

我无法配置camel以使用maven camel运行java配置:运行插件。

这是插件xml:

            <plugin>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-maven-plugin</artifactId>
                <version>2.15.1.redhat-621084</version>
                <configuration>
                    <mainClass>com.deltashade.config.MyRouteConfiguration</mainClass>
                </configuration>
            </plugin>

这是类代码

import org.apache.camel.spring.javaconfig.Main;
import org.apache.camel.spring.javaconfig.CamelConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.deltashade.hellocamel")
public class MyRouteConfiguration extends CamelConfiguration {
    public static void main(String[] args) throws Exception {
        Main main = new Main();
        main.setConfigClass(MyRouteConfiguration.class);
        main.run();
    }
}

当我使用set config类时,我收到以下错误: The method setConfigClass(Class<MyRouteConfiguration>) is undefined for the type Main

1 个答案:

答案 0 :(得分:2)

完成答案改造。

为了使用maven camel run plugin,你需要添加一个“模板camel-context文件”所有这个文件需要做的就是导入你的java配置。

Java Config

@Configuration
@ComponentScan("com.mcf.xml.free.route")
public class RouteJavaConfig extends CamelConfiguration {

}

Maven Camel插件

            <plugin>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-maven-plugin</artifactId>
                <version>${camel.version}</version>
            </plugin>

Spring 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: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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

  <context:annotation-config/>
  <bean class="com.mcf.xml.free.config.RouteJavaConfig"/>
</beans>

如果你想完全免费使用xml,你可以删除你的Spring Xml Context并在你的java配置中添加一个新的main方法。并删除camel maven插件,转而使用exec:java命令。

更新了Java Config for no Xml

import org.apache.camel.spring.javaconfig.CamelConfiguration;
import org.apache.camel.spring.javaconfig.Main;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.mcf.xml.free.route")
public class RouteJavaConfig extends CamelConfiguration {
    public static void main(String[] args) throws Exception {
        Main main = new Main();
        main.setConfigClass(RouteJavaConfig.class);
        main.run();
    }
}

exec maven插件(使用exec:java运行)

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <configuration>
        <mainClass>com.mcf.xml.free.config.RouteJavaConfig</mainClass>
    </configuration>
</plugin>