问题
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