问题:Spring初始化HttpServlet不是用@Comonent的方式,所以用@Autowired无法注入bean。
解决:可以实现init方法,在里面绑定bean,方式如下(假设要注入的bean类为A):
......
private A a;
@Override
public void init() throws ServletException {
super.init();
ServletContext servletContext = this.getServletContext();
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
this.a = (A) context.getBean("a");
}
......