I need help in understanding the custom scope in spring I went through the reference manual and do have some understanding about it but the question which is bugging me is when actually the call to the get method of my implementation of scope interface is made Though my understanding is if a bean with scope=myscope is defined then Spring calls the get method on my scope implementation to retrieve the object . But when I came across an example I noticed something strange.
The call to get method is not made at the execution of following statement
Object targetBean = getApplicationContext().getBean(task.getBeanName());
but on the execution of following statement.
ReflectionUtils.invokeMethod(targetMethod, targetBean, arguments);
Can any one help me by explaining more about custom scope implementation and call of get method.
PS : In the concerned example the Custom scope is used with Threadlocal .. If any one can provide me a working example link of customscope with thread local ,it will be a great help
I have made extensive use of custom scopes in the past to inject stateful objects into singleton services.
My understanding is that a proxy wraps the bean that is custom scoped and the proxy retrieves the bean from the scope on the method invocation of the bean.
See also Spring Indepth
So in your case
Object targetBean = getApplicationContext().getBean(task.getBeanName());
targetBean
will be the proxy
ReflectionUtils.invokeMethod(targetMethod, targetBean, arguments);
Invokes the method on the proxy which calls through org.springframework.beans.factory.config.Scope#get
to retrieve the correct bean
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With