Access Spring beans from a servlet in JBoss

后端 未结 3 639
再見小時候
再見小時候 2020-11-30 23:21

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.

Wha

3条回答
  •  死守一世寂寞
    2020-11-30 23:57

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

    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 ApplicationContext tied to that ServletContext (e.g. created via ContextLoaderListener) and inject the Spring beans available in that ApplicationContext.

提交回复
热议问题