Apache Camel POJO Bean Processor Singleton or Prototype?

只谈情不闲聊 提交于 2019-12-22 09:20:01

问题


I'm using Camel's spring DSL to build routes and I'm using my POJO bean as a processor.

Initially, I as doing:

<bean ref="myPojoBeanProcessor"/>

where, 'myPojoBeanProcessor' was defined as a bean in my spring context.

Then, I switched to:

<bean beanType="com.mycompany.transformation.MyPojoBeanProcessor"/>

The Java DSL equivalent would be, I think:

from("xxx").bean(MyPojoBeanProcessor.class).xxx

I'm wondering if with the later approach, a bean object would be instantiated with every request or would the bean instance be cached and used as a singleton (like the default in spring).

I was unable to find an answer in the camel documentation or on google. Does anyone know the answer to this question?


回答1:


Why don't you just try it out :)

Camel will create an instance one time using the default no-arg constructor, and cache the instance, eg its singleton scoped.




回答2:


Here is the relevant piece of code in MethodCallExpression#createExpression(CamelContext):

if (this.beanType != null)
{
  Expression answer;
  if (ObjectHelper.hasDefaultPublicNoArgConstructor(this.beanType)) {
    this.instance = camelContext.getInjector().newInstance(this.beanType);
    answer = new BeanExpression(this.instance, getMethod());
  } else {
    answer = new BeanExpression(this.beanType, getMethod());
  }
}

Camel is caching the instance it creates in a field.



来源:https://stackoverflow.com/questions/10728029/apache-camel-pojo-bean-processor-singleton-or-prototype

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!