java 如何使用 Spring 的 ThreadPoolExecutorFactoryBean 工厂 bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6083466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:15:31  来源:igfitidea点击:

How to use Spring's ThreadPoolExecutorFactoryBean factory bean

javaspringfactory

提问by tmbrggmn

I would like to be able to inject an ExecutorServiceinstance into my Spring services, and the Spring API suggest using ThreadPoolExecutorFactoryBeanfor this purpose. Very simple question; how the hell do I use the ThreadPoolExecutorFactoryBeanto create an ExecutorServicethat I can wire into my other services?

我希望能够将一个ExecutorService实例注入到我的 Spring 服务中,Spring API 建议ThreadPoolExecutorFactoryBean为此目的使用。很简单的问题;我到底如何使用ThreadPoolExecutorFactoryBean来创建一个ExecutorService我可以连接到我的其他服务的?

I feel like a complete idiot for asking his question, but I can't seem to get this figured out.

问他的问题我觉得自己是个彻头彻尾的白痴,但我似乎无法弄清楚这一点。

采纳答案by skaffman

First off, you need to learn what a FactoryBeanis - read section 3.8.3of the spring docs.

首先,您需要了解什么是 a FactoryBean- 阅读spring 文档的第 3.8.3 节

Then, you read the Javadocfor ThreadPoolExecutorFactoryBeanand understand what it does.

然后,你读的JavadocThreadPoolExecutorFactoryBean,并了解它做什么。

Next, you configure a ThreadPoolExecutorFactoryBeanin your context. This will create an ExecutorService(since it's a FactoryBean, see above), which you can inject into your bean.

接下来,ThreadPoolExecutorFactoryBean在您的上下文中配置 a 。这将创建一个ExecutorService(因为它是 a FactoryBean,请参见上文),您可以将其注入到您的 bean 中。

回答by Duncan Jones

To expand on skaffman's answer, here's a short and sweet example of what one needs to do:

为了扩展skaffman 的回答,这里有一个简短而甜蜜的例子,说明了一个人需要做什么:

<bean id="classNeedingExecutor" class="foo.Bar">
  <property name="executor" ref="threadExecutor" />
</bean>

<bean id="threadExecutor"
  class="org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean">
  <property name="corePoolSize" value="1" />
  <property name="maxPoolSize" value="1" />
</bean>

Again, see the JavaDocsfor an explanation of the other properties that can be set to configure the ExecutorService.

同样,请参阅JavaDocs以了解可设置以配置ExecutorService.

回答by Kevin

You may consider using @Async if you're using spring 3, http://blog.espenberntsen.net/2010/03/08/spring-asynchronous-support/.

如果您使用的是 spring 3,您可以考虑使用 @Async,http://blog.espenberntsen.net/2010/03/08/spring-asynchronous-support/ 。