如何获取springmvc容器中的bean

public class WebContainerListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
System.out.println(“contextInitialized…”);
//把一些固定不变的一些信息查询出来放在application里
//调用service来完成查询
//如何获取springmvc容器中的bean
WebApplicationContext springContext =
WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
//System.out.println(“springContext:” + springContext);
TermService termService = (TermService)springContext.getBean(“termService”);
//System.out.println(“termService:” + termService);
List terms = termService.findAllTerms();
//保存在一个application
event.getServletContext().setAttribute(“terms”,terms);
//学院Service
CollegeService collegeService = (CollegeService)springContext.getBean(“collegeService”);
List colleges = collegeService.findAll();
event.getServletContext().setAttribute(“colleges”,colleges);
}

@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
    System.out.println("contextDestroyed....");
}

}

你可能感兴趣的:(如何获取springmvc容器中的bean)