【问题标题】:Camel read properties file骆驼读取属性文件
【发布时间】:2016-12-01 13:43:37
【问题描述】:

如何使用 Java DSL 和 Main 对象配置属性文件的使用?

根据this page,我应该可以调用类似:

main.setPropertyPlaceholderLocations("example.properties");

但是这根本行不通。似乎直到 Camel 2.18 才添加该选项,而我正在运行 2.17.1。

在让应用程序以独立形式运行时设置要使用的属性文件的原始方法是什么?

一些背景故事:

我正在尝试从 Spring 转换为 Java DSL。在转换过程中,我试图让我的 Camel 应用程序自行运行。我知道这是使用main.run(); 实现的。

在使用 CamelContext 时,我的东西“正常运行”,但它不能单独运行。所以我知道在这种情况下使用以下方法会起作用:

PropertiesComponent pc = new PropertiesComponent();
pc.setLocation("classpath:/myProperties.properties");
context.addComponent("properties", pc);

有什么方法可以告诉main 使用该设置吗?或者还有什么需要的吗?

【问题讨论】:

    标签: java spring apache-camel


    【解决方案1】:

    你可以使用下面的sn-p:

    PropertiesComponent pc = new PropertiesComponent();
    pc.setLocation("classpath:/myProperties.properties");
    main.getCamelContexts().get(0).addComponent("properties", pc);
    

    另外,如果您使用camel-spring,您可以使用org.apache.camel.spring.Main 类,它应该使用应用程序上下文中的属性占位符。

    【讨论】:

    • 啊,简短,甜蜜,切中要害。谢谢!你会认为它会比那更干净一些。但我想这就是他们在 Camel 2.18 中引入新方法的原因!
    • 如果你要迁移到 Java 配置,你也应该尝试一下 Spring Boot - Camel has great support for it,删除了很多样板文件。
    【解决方案2】:

    既然您提到您正在从 Spring XML 迁移到 Java Config,这是一个使用属性并将其注入 Camel 路由的最小应用程序(它实际上是 Spring 中的属性管理注入我们的 Camel 路由 bean):

    my.properties

    something=hey!
    

    主类

    打包camelspringjavaconfig;

    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;
    import org.springframework.context.annotation.PropertySource;
    
    @Configuration
    @ComponentScan("camelspringjavaconfig")
    @PropertySource("classpath:my.properties")
    public class MyApplication extends CamelConfiguration {
    
        public static void main(String... args) throws Exception {
            Main main = new Main();
            main.setConfigClass(MyApplication.class);  // <-- passing to the Camel Main the class serving as our @Configuration context
            main.run();   // <-- never teminates
        }
    }
    

    MyRoute 类

    package camelspringjavaconfig;
    
    import org.apache.camel.builder.RouteBuilder;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.core.env.Environment;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyRoute extends RouteBuilder {
    
        @Autowired
        Environment env;   //<-- we are wiring the Spring Env
    
        @Override
        public void configure() throws Exception {
    
            System.out.println(env.getProperty("something"));  //<-- so that we can extract our property
    
            from("file://target/inbox")
                    .to("file://target/outbox");
        }
    }
    

    【讨论】:

    • 这很好,没想到你可以使用注释来配置东西!我希望骆驼页面有一点更干净的流程,这样我就可以找到其中的一些内容
    猜你喜欢
    • 2014-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 2018-12-08
    相关资源
    最近更新 更多