Webapplicationcontextutils can get
Webapplicationcontextwebapplicationcontext can fetch the bean and then execute the method to get the data.
package cn.sccl.common.web;import java.util.list;import java.util.map;import javax.servlet.servletcontext;import javax.servlet.servletcontextevent;import javax.servlet.servletcontextlistener;import org.apache.commons.logging.log;import org.apache.commons.logging.logfactory;import org.springframework.context.applicationcontext;import org.springframework.web.context.contextloaderlistener;import org.springframework.web.context.support.webapplicationcontextutils;import cn.sccl.common.service.bizcodemanager;import cn.sccl.common.web.util.log4jwebconfigurer;import cn.sccl.pms.model.division;import cn.sccl.pms.model.divisionquery;import cn.sccl.pms.service.divisionmanager;public class startuplistener extends contextloaderlistener implements servletcontextlistener {@Overridepublic void Contextinitialized (servletcontextevent event) {super.contextinitialized (evenT); Servletcontext context = event.getservletcontext ();//Get servletcontext//can also be obtained in the implementation of the HttpServlet interface, Servletcontext servletcontext = this.getservletcontext (); setupcontext (context);} Protected void setupcontext (Final servletcontext context) {// Webapplicationcontext ctx = webapplicationcontextutils.getwebapplicationcontext (ServletContext) It is recommended to use this, because getrequiredwebapplicationcontext requires that the ServletContext must have applicationcontextapplicationcontext in the //. ctx = webapplicationcontextutils.getrequiredwebapplicationcontext (context); // Administrative divisions DivisionManager divisionManager = (Divisionmanager) ctx.getbean (" Divisionmanager ");//Get Managerlist<division> divisions = divisionmanager.query (new Divisionquery ());//Put data in the servletcontext page and get it in application. Because application and ServletContext correspond to Context.setattribute ("divisions", divisions);}}
This article is from the "Jianbo" blog, make sure to keep this source http://jianboli.blog.51cto.com/12075002/1890712
Spring Tool class: Webapplicationcontextutils