public class Notify_Url extends HttpServlet {
private static final long serialVersionUID = 201110091627L;
// 保存sysVarClient 引用,通过它获得 key
private ISystemVar sysVarClient;
public Notify_Url() {
super();
}
// 调用 doPost 方法
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);// 调用 doPost 方法
}
// servlet初始化时从spring加载 sysVarClient 通过sysVarClient 获得 key
@Override
public void init() throws ServletException {
super.init();
ServletContext servletContext = this.getServletContext();
WebApplicationContext ctx = WebApplicationContextUtils
.getWebApplicationContext(servletContext);
sysVarClient = (ISystemVar) ctx.getBean("sysVarClient");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//do Something ....
}
}
在servlet中获取spring管理的bean
在init()方法中取得 WebApplicationContext ,通过WebApplicationContext的getBean()方法取得想要的bean。