Java 从 JBoss 中的 servlet 访问 Spring bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/467235/
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-08-11 15:00:17  来源:igfitidea点击:

Access Spring beans from a servlet in JBoss

javaspringjakarta-eeservletsjboss

提问by Sophie Gage

I want to write a simple servlet in JBoss which will call a method on a Spring bean. The purpose is to allow a user to kick off an internal job by hitting a URL.

我想在 JBoss 中编写一个简单的 servlet,它将调用 Spring bean 上的一个方法。目的是允许用户通过点击 URL 来启动内部工作。

What is the easiest way to get hold of a reference to my Spring bean in the servlet?

在 servlet 中获取对我的 Spring bean 的引用的最简单方法是什么?

JBoss web services allow you to inject a WebServiceContext into your service class using an @Resource annotation. Is there anything comparable that works in plain servlets? A web service to solve this particular problem would be using a sledgehammer to crush a nut.

JBoss Web 服务允许您使用 @Resource 注释将 WebServiceContext 注入您的服务类。有什么可比的东西可以在普通 servlet 中工作吗?解决此特定问题的 Web 服务将使用大锤压碎坚果。

采纳答案by Chin Huang

Your servlet can use WebApplicationContextUtils to get the application context, but then your servlet code will have a direct dependency on the Spring Framework.

您的 servlet 可以使用 WebApplicationContextUtils 来获取应用程序上下文,但是您的 servlet 代码将直接依赖于 Spring 框架。

Another solution is configure the application context to export the Spring bean to the servlet context as an attribute:

另一种解决方案是配置应用程序上下文以将 Spring bean 作为属性导出到 servlet 上下文:

<bean class="org.springframework.web.context.support.ServletContextAttributeExporter">
  <property name="attributes">
    <map>
      <entry key="jobbie" value-ref="springifiedJobbie"/>
    </map>
  </property>
</bean>

Your servlet can retrieve the bean from the servlet context using

您的 servlet 可以使用从 servlet 上下文中检索 bean

SpringifiedJobbie jobbie = (SpringifiedJobbie) getServletContext().getAttribute("jobbie");

回答by Sophie Gage

I've found one way to do it:

我找到了一种方法:

WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
SpringifiedJobbie jobbie = (SpringifiedJobbie)context.getBean("springifiedJobbie");

回答by Oliver Drotbohm

There is a much more sophisticated way to do that. There is SpringBeanAutowiringSupportinside org.springframework.web.context.supportthat allows you building something like this:

有一种更复杂的方法可以做到这一点。有SpringBeanAutowiringSupport内部的org.springframework.web.context.support,它允许你建立这样的事情:

public class MyServlet extends HttpServlet {

  @Autowired
  private MyService myService;

  public void init(ServletConfig config) {
    super.init(config);
    SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
      config.getServletContext());
  }
}

This will cause Spring to lookup the ApplicationContexttied to that ServletContext(e.g. created via ContextLoaderListener) and inject the Spring beans available in that ApplicationContext.

这将导致 Spring 查找ApplicationContext绑定到那个ServletContext(例如,通过创建ContextLoaderListener)并注入可用的 Spring bean ApplicationContext