我想在 JBoss 中编写一个简单的 servlet,它将调用 Spring bean 上的一个方法。目的是允许用户通过点击 URL 来启动内部工作。
在 servlet 中获取对 Spring bean 的引用的最简单方法是什么?
JBoss Web 服务允许您使用 @Resource 注释将 WebServiceContext 注入(inject)到您的服务类中。有什么类似的东西可以在普通的 servlet 中工作吗?解决此特定问题的 Web 服务将使用大锤来粉碎坚果。
最佳答案
有一种更复杂的方法可以做到这一点。 org.springframework.web.context.support
内有 SpringBeanAutowiringSupport
,可让您构建如下内容:
public class MyServlet extends HttpServlet {
@Autowired
private MyService myService;
public void init(ServletConfig config) {
super.init(config);
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
config.getServletContext());
}
}
这将导致 Spring 查找与该 ServletContext
相关联的 ApplicationContext
(例如,通过 ContextLoaderListener
创建)并注入(inject)其中可用的 Spring beans ApplicationContext
.
关于java - 从 JBoss 中的 servlet 访问 Spring bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/467235/