【问题标题】:Spring Boot properties in Camel route or endpoint configurationCamel 路由或端点配置中的 Spring Boot 属性
【发布时间】:2020-06-14 23:09:23
【问题描述】:

我是 Camel 的新手,边走边学。似乎有很多可能的方法可以在某些框架(例如 Spring Boot)和 Camel 之间交换信息。我很难从 Spring Boot 属性中弄清楚如何(甚至 if)做到这一点。我的意思是application.propertiesapplication.yml

以下 SO 项目 (Spring Boot properties usage in Apache Camel route) 提出了一个非常相似的问题,但答案似乎不起作用。我承认我不太明白提供的最后一个答案。

那我想做什么?由于我在 Camel 还是个新手,我正在做一些非常基本和简单的事情。我有一个非常小的 Spring Boot 应用程序,它使用 Camel 将文件从一个位置简单地复制到另一个位置。

这是我的路线:

src/main/java/mypackage/CopyFileRoute.java:

@Component
public class CopyFileRoute extends RouteBuilder {

  @Override
  //@formatter:off
  public void configure() throws Exception {
    this
    .from("file:{{properties.source-path}}/{{properties.file-name}}?noop=true")
    .to("file:{{properties.dest-path}}/{{properties.file-name}}");
  }
  //@formatter:on

}

src/main/resources/application.yml:

properties:
  source-path: demo/copyFrom
  dest-path: demo/copyTo
  file-name: test.txt

我在 Camel 用户指南(https://camel.apache.org/manual/latest/using-propertyplaceholder.htmlhttps://camel.apache.org/components/latest/properties-component.html)中阅读了有关属性替换(或占位符)的内容,但无法使其正常工作。当然,用户指南中的示例是在 XML 配置中的,而我是在 Java 配置中进行的。这在 Java 代码中不起作用吗?

顺便说一句,我尝试合并 Camel 属性“桥”(BridgePropertyPlaceholderConfigurer),但这也不起作用。我不确定如何我应该使用它。

更新

我用“桥”尝试了以下方法,但是,这也不起作用:

@Configuration
@ComponentScan("mypackage")
public class Configurer {

  @Bean
  public CamelContext camelContext() {
    return new DefaultCamelContext();
  }

  @Bean
  public BridgePropertyPlaceholderConfigurer bridgePropertyPlaceholder() {
    BridgePropertyPlaceholderConfigurer bridge = new BridgePropertyPlaceholderConfigurer();
    bridge.setLocation(new ClassPathResource("application.properties"));
    return bridge;
  }

}

【问题讨论】:

  • properties.source-path是application.properties中的名字吗?
  • 不,它在 application.yml 中定义。我尝试了一个快速测试,在 application.properties 中放置了相同的属性,但这也不起作用。

标签: java spring-boot apache-camel


【解决方案1】:

使用 Java DSL 你可以做到这一点

对于 Camel 版本 2.x.x

@Override
    protected CamelContext createCamelContext() throws Exception {
        CamelContext context = super.createCamelContext();
        context.addComponent("properties", new PropertiesComponent("file:C:\\Your\\path\\application.properties"));
        return context;
    }

或对于版本 3.x.x

@Override
    protected CamelContext createCamelContext() throws Exception {
        CamelContext context = super.createCamelContext();
        PropertiesComponent pc = context.getPropertiesComponent();
        pc.setLocation("classpath:org/apache/camel/component/properties/myproperties.properties");
        return context;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    • 2017-07-05
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    相关资源
    最近更新 更多