可是,用Listener准备application作用域的数据。在获取容器的时候会有一些注意事项。
public class InitListener implements ServletContextListener { public void contextInitialized(ServletContextEvent sce) { // 获取容器与相关的Service对象 ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()); PrivilegeService privilegeService = (PrivilegeService) ac.getBean("privilegeServiceImpl"); // 准备数据:topPrivilegeList List<Privilege> topPrivilegeList = privilegeService.findTopList(); sce.getServletContext().setAttribute("topPrivilegeList", topPrivilegeList); System.out.println("------------> 已准备数据 <------------"); // 准备数据:allPrivilegeUrls Collection<String> allPrivilegeUrls = privilegeService.getAllPrivilegeUrls(); sce.getServletContext().setAttribute("allPrivilegeUrls", allPrivilegeUrls); System.out.println("------------> 已准备数据allPrivilegeUrls <------------"); } public void contextDestroyed(ServletContextEvent arg0) { } 该Listener配置在web.xml里,默认通过反射生成实例,来得到这个对象实例来运行 并没有从Spring容器里面获取,Tomcat没有找Spring容器,所以此处无法使用注解 假设使用注解,会生成两个对象,一个Tomcat产生的对象,一个Spring容器注入的对象 Tomcat会使用自己产生的对象,而Spring管理的对象没人使用转载于:https://www.cnblogs.com/bhlsheji/p/5333821.html
相关资源:数据结构—成绩单生成器