【发布时间】:2017-04-27 09:57:32
【问题描述】:
我有一个 Spring Boot,我在其中自动配置了一个路由器 bean。 这一切都很完美,但是当我想将该 bean 注入自定义 servlet 时,它就成了一个问题:
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 boot嵌入?
-
您是否考虑过使用
@Controller或@RestController代替servlet?我认为这是在 spring-boot 中做事的一种更可取的方式
标签: spring spring-boot