How to instantiate Spring bean with custom scope and @Autowired dependencies? -


in our project, use spring request scoped beans. we've requirement support async requests , request scoped beans don't work child threads. i'm aware of requestcontextfilter , it's "support" async appears requestcontextfilter expects main thread wait child threads finish, isn't case us. our main thread returns after spawning new threads using @async annotation , dispatcherservlet clears out requestcontextholder. when child threads point need request scoped bean, @autowired fails.

i'm aware of simplethreadscope doesn't clean thread-local attributes , in thread-pooling situation, not dangerous use downright useless.

what need custom scope. far, i've found 3 useful examples of them fall short in beans instantiate part of custom scope plain pojos without dependencies. needless that's non-existent in real life application. can suggest way instantiate custom scoped beans have @autowired dependencies on beans other scopes?

what found far:

https://github.com/spring-by-example/spring-by-example/tree/master/modules/sbe-thread-scope/src/main/java/org/springbyexample/bean/scope/thread

https://github.com/billkoch/spring-async-mdc

spring bean custom scope jms

continuing discussion other question's answer here...

see spring documentation scoped beans dependencies.

.

i'm referring <aop:scoped-proxy/> link points to. each time autowired field referenced, custom scope's get() method called lookup instance based on criteria.

.

i understand can dependencies (though unsure how, scope isn't bean, perhaps need pass application context during instantiation?). don't understand how inject dependencies bean if those're marked @autowired? or saying custom scoped bean shouldn't have @autowired dependencies?

it works automatically; spring injects proxy bean , scope.get() invoked on every method call on bean, returning specific instance want in context of current invocation.

take @ abstractrequestattributesscope see how works (in case, gets instance http request and, if doesn't exist, creates it).

so, code calls foo() on proxy; framework calls scope desired instance , calls foo() on instance.

the exposed methods wish call must either on interface or not declared final.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -