从 JBoss 中的 servlet 访问 Spring bean [英] Access Spring beans from a servlet in JBoss

查看:17
本文介绍了从 JBoss 中的 servlet 访问 Spring bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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.

推荐答案

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

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

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

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

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

您的 servlet 可以使用

Your servlet can retrieve the bean from the servlet context using

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

这篇关于从 JBoss 中的 servlet 访问 Spring bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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