如何检查为什么 Camel 中的 @BeanInject 没有初始化成员?

How to check why @BeanInject in Camel does not initialize member?

我有以下上下文配置:

<bean id="propertiesFactory" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="ignoreResourceNotFound" value="true"/>
  <property name="locations">
    <list>
      <value>classpath:conf.properties</value>
    </list>
  </property>
</bean>

<bean id="bridgePropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
  <property name="properties" ref="propertiesFactory"/>
</bean>

<bean id="messageThrottlerHolder" class="foo.bar.common.MessageThrottlerHolder"/>
<bean id="cassandraRoute" class="foo.bar.routes.CassandraRouteBuilder"/>

然后在 类 中:MessageThrottlerHolderCassandraRouteBuilder 中有:

@BeanInject
Properties properties;

精彩。

但是 CassandraRouteBuilder may/could 使用没有 NPE 的 properties,因为 properties 已经很好地初始化了。 另一方面,MessageThrottlerHolder 无法使用 properties,因为在构造函数中 propertiesnull 并且无法初始化它们。

如何检查 properties 为何为空? 有什么问题,@BeanInject 以某种方式没有初始化 properties?

如果你认为 Properties 是骆驼 属性 占位符或类似的东西,那你就错了。

Camel 的 @BeanInject 用于通过 bean name/id 在 Camel 寄存器中查找 bean,作为一个穷人 Spring IoC。如果你使用 Spring 那么你可以使用 Spring @Autowired 或者他们有的任何东西。

如果你想从 properties 占位符访问属性,那么你可以注入 CamelContext 并使用它的 API 来解析:http://static.javadoc.io/org.apache.camel/camel-core/2.20.0/org/apache/camel/CamelContext.html#resolvePropertyPlaceholders-java.lang.String-

或者使用 Camel 的 @PropertyInject 而不是指定要查找的键名。我不确定 Spring 是否有任何类似的注释来查找属性,因为你已经打开了 camel-spring 桥。您也可以尝试调查一下。