jar包依赖
- 要依赖spring-web的jar包
监听器配置
- 在web.xml配置一个监听器
ContextLoaderListener,实现了 ServletContextListener。在这个 listener 中,当服务器启动时,将 ApplicationContext 对象,其实是它的一个实现类WebApplicationContext,对象存入到了ServletContext 中<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
文件配置
- 配置applicationContext.xml的位置
服务器默认会在WEB-INF目录下查找applicationContext.xml,如果不在默认位置,就需要配置applicationContext.xml的位置。<context-param> <!-- contextConfigLocation 是在 listener 中声明的一个常量,描述的就是 spring 配置文件的位置 --> <param-name>contextConfigLocation</param-name> <!-- classpath:代表在当前工程的类路径下查找,也就是在src下查找 --> <param-value>classpath:applicationContext.xml</param-value> </context-param>