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

查看:229
本文介绍了从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.

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

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

JBoss Web服务允许您使用@Resource注释将WebServiceContext注入服务类。有没有类似的可用于普通的servlet?解决这个特殊问题的Web服务就是使用大锤粉碎一个坚果。

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 Framework。

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天全站免登陆