【问题标题】:Apache Camel - Calling method from autowired object in @ProduceApache Camel - 从@Produce中的自动装配对象调用方法
【发布时间】:2017-03-30 12:45:00
【问题描述】:

我在骆驼路线的单元测试中有以下内容:

RouteUnitTest.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {RouteUnitTestConfig.class})
public class RouteUnitTest extends CamelTestSupport {
    @Autowired
    MQProperties mqProps;
    @Autowired
    Route route;

    @Produce(uri="...")
    ProducerTempalte template;

    @Test
    //rest of test class

}

MQProperties 对象从属性文件中提取属性,对于单元测试,它读取 application-unit-test.properties 文件,该文件使用 SEDA 端点进行单元测试。我似乎无法在 @Produce 标签中正确表达;我想将 ProducerTemplates uri 设置为 mqProps.getReceiveQueue(),但是在我尝试过的格式中,我得到了一个关于为生产者设置默认端点的异常。我知道应该传入的值不是问题,调用 template.send(mqProps.getReceiveQueue(), exchange);例如工作得很好。我尝试了以下格式:

@Produce(uri="#{@mqProps.getReceiveQueue}")
@Produce(uri="{{mqProps.getReceiveQueue}}")
@Produce(uri="#{mqProps.getReceiveQueue}")

这些都抛出了提到的异常。我是不是把 EL 格式弄错了,还是自动装配有问题?即也许当生产者 uri 的东西都被连接起来时,对象还没有被填充?我在这方面找不到太多其他内容,Camel 资源更多地讨论了 Bean 注入等,但到目前为止我在 @Produce 注释上看到的任何资源通常都使用硬编码字符串,或者直接从属性文件中读取

【问题讨论】:

    标签: java spring spring-boot apache-camel


    【解决方案1】:

    我认为问题出在BeanExpressionContext

    Spring 需要这个来评估 SPEL。这是解决此问题的简单示例。使其适应您的项目。

      public class TestingSupport {
    
      @Bean
      public BeanExpressionContext setBeanExpressionContext(@Autowired MQProperties mqProps, @Autowired ApplicationContext applicationContext){
        return new BeanExpressionContext((ConfigurableBeanFactory) applicationContext.getAutowireCapableBeanFactory(), null);
      }
    }    
    

    测试类看起来像这样。

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = {RouteUnitTestConfig.class, MQProperties.class, TestingSupport.class})
    @TestPropertySource("classpath:application-test.properties")
    public class RouteUnitTest extends CamelTestSupport {
    
      @Value("#{MQProperties['receiveQueue']}")
      String recieveQueue;
    
      @Test
      public void test(){
          assertEquals(1, 1);
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多