我有一個Spring Boot,我已經自動配置了一個Router Bean。 這一切都可以完美運行,但是當我想對這個bean注入到一個特定的Servlet它成爲一個問題:Spring Boot:將Bean注入到HttpServlet中
public class MembraneServlet extends HttpServlet {
@Autowired
private Router router;
@Override
public void init() throws ServletException {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
new HttpServletHandler(req, resp, router.getTransport()).run();
}
}
這應該是要走的路,但
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
不會自動裝配的因爲WebapplicationContext
始終爲空。該應用程序在MVC環境中運行。
是你的httpservlet嵌入spring博ot? –
這不是重複的https://stackoverflow.com/questions/18745770/spring-injection-into-servlet? –
您是否考慮過使用'@ Controller'或'@ RestController'來代替servlet?我認爲這是一種更好的方式在spring-boot中做事情的方式 –