重写servlet的初始化方法,在里面加上获取Bean的逻辑,在创建servlet的过程中Bean也就会被创建。代码入下所示:
@Override
public void init() throws ServletException {
super.init();
ServletContext servletContext = this.getServletContext();
WebApplicationContext ctx = WebApplicationContextUtils
.getWebApplicationContext(servletContext);
userManager = (UserManager) ctx.getBean("userManager");
}