我有一个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中做事情的方式 –