如何使用自定义范围和@Autowired依赖关系实例化Spring bean? [英] How to instantiate Spring bean with custom scope and @Autowired dependencies?

查看:443
本文介绍了如何使用自定义范围和@Autowired依赖关系实例化Spring bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的项目中,我们使用Spring请求范围的bean.现在,我们需要支持异步请求,并且请求范围的Bean不适用于子线程.我知道 RequestContextFilter ,它是异步的支持",但似乎RequestContextFilter希望主线程等待子线程完成,而对我们而言并非如此.我们的主线程在使用@Async注释生成新线程之后立即返回,并且DispatcherServlet清除了RequestContextHolder.因此,当子线程到达需要它们的请求范围的Bean时,@Autowired就会失败.

In our project, we use Spring request scoped beans. Now we've a requirement to support async requests and request scoped beans don't work for child threads. I'm aware of RequestContextFilter and it's "support" for async but it appears that RequestContextFilter expects the main thread to wait for the child threads to finish, which isn't the case for us. Our main thread immediately returns after spawning new threads using @Async annotation and DispatcherServlet clears out the RequestContextHolder. Thus when the child threads get to the point where they need a request scoped bean, @Autowired fails.

我也知道

I'm also aware of SimpleThreadScope but it doesn't clean up thread-local attributes and in a thread-pooling situation, is not only dangerous to use but downright useless.

我需要的是自定义范围.到目前为止,我已经找到了3个有用的示例,但是它们都不足,因为它们实例化为自定义范围一部分的bean是没有任何依赖关系的普通POJO.不用说这在现实生活中是不存在的.谁能提出一种实例化自定义范围的Bean的方法,该范围在其他范围内对Bean具有@Autowired依赖性?

What I need is a custom scope. So far, I've found 3 useful examples but all of them fall short in that the beans they instantiate as part of the custom scope are plain POJOs without any dependencies. Needless to say that's non-existent in a real life application. Can anyone suggest a way to instantiate custom scoped beans that have @Autowired dependencies on beans from other scopes?

到目前为止我发现了什么

What I found so 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自定义范围JMS

推荐答案

请参见有关范围内的bean作为依赖项的Spring文档.

.

我指的是链接所指向的<aop:scoped-proxy/>.每次引用自动装配字段时,都会根据某些条件调用自定义范围的get()方法来查找实例.

I'm referring to the <aop:scoped-proxy/> which is what the link points to. Each time the autowired field is referenced, your custom scope's get() method is called to lookup the instance based on some criteria.

.

我知道我可以查找依赖关系(尽管不确定如何,作用域不是bean,也许我需要在实例化过程中传递应用程序上下文?).我不明白的是,如果将那些依赖项标记为@Autowired,如何将这些依赖项注入到我的bean中?还是在说自定义范围的bean不应该具有@Autowired依赖项?

I understand I can look up the dependencies (though unsure how, a scope isn't a bean, perhaps I need to pass application context during instantiation?). What I don't understand is how to inject those dependencies into my bean if those're marked @Autowired? Or are you saying the custom scoped bean shouldn't have @Autowired dependencies?

它会自动运行; Spring为该bean注入一个代理,并在该bean的每个方法调用上调用scope.get(),并在当前调用的上下文中返回您想要的特定实例.

It works automatically; Spring injects a proxy for the bean and the scope.get() is invoked on every method call on that bean, returning the specific instance you want in the context of the current invocation.

看看AbstractRequestAttributesScope以了解其工作原理(在这种情况下,从HTTP请求获取实例,如果不存在,则创建该实例).

Take a look at the AbstractRequestAttributesScope to see how it works (in that case, gets the instance from the HTTP Request and, if it doesn't exist, creates it).

因此,您的代码在代理上调用了foo();框架调用范围以获取所需的实例,然后对该实例调用foo().

So, your code calls foo() on the proxy; the framework calls the scope to get the desired instance and then calls foo() on that instance.

您要调用的公开方法必须在接口上或未声明为final.

The exposed methods you wish to call must either be on an interface or not declared final.

这篇关于如何使用自定义范围和@Autowired依赖关系实例化Spring bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆