Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding custom scope in spring

Tags:

scope

spring

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

like image 975
Anupam Gupta Avatar asked May 20 '11 13:05

Anupam Gupta


1 Answers

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

like image 129
Paul41979 Avatar answered Sep 30 '22 02:09

Paul41979