Autowiring in servlet

后端 未结 2 2001
青春惊慌失措
青春惊慌失措 2020-12-02 20:17

i want to use spring autowiring in servlet so here\'s my code:

@Configurable
public class ImageServlet extends HttpServlet {

   @Autowired
   private Syst         


        
相关标签:
2条回答
  • 2020-12-02 21:01

    Remove the @Configurable annotation from your servlet and add:

    SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext (this);
    

    at the first line of your init() method.

    0 讨论(0)
  • 2020-12-02 21:10

    I followed the solution in the following link, and it works fine: Access Spring beans from a servlet in JBoss

    public class MyServlet extends HttpServlet {
    
      @Autowired
      private MyService myService;
    
      public void init(ServletConfig config) {
        super.init(config);
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
          config.getServletContext());
      }
    }
    
    0 讨论(0)
提交回复
热议问题