Spring 配置的2个常见问题

来源:互联网 发布:java友盟别名推送demo 编辑:程序博客网 时间:2024/06/03 18:27

在搭建spring的开发环境时经常会出现一些问题。

 

一、

 

严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

 

这个异常是因为 在导入jar包时 缺少2个jar包  分别是 commons-logging-1.1.1.jar和commons-logging.jar 。因为spring的日志是基于log4j的。

 

二、

 

applicationContext.xml  文件存放位置不同 web.xml 的配置也会稍有不同。一般applicationContext.xml的默认路径为WEB-INFO目录下。

当applicationContext.xml 在WEB-INFO目录下时,web.xml  只需要配置Spring监听即可。

 

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>springStudy</display-name>    <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>    <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list></web-app>

 

而 当applicationContext.xml不在WEB-INFO目录下时。比如在src目录下时(src也是一个经常存放applicationContext.xml的目录)在web.xml配置监听后,还需要指定一下applicationContext.xml所在的目录。web.xml 要加入下列代码

  <listener>   

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
  </context-param>

 

因为ContextLoaderListener监听在服务器启动时会先到默认路径WEB-INFO下找,没有在获取<context-param>的配置路径。

applicationContext.xml在src下时的web.xml

 

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>springStudy</display-name>    <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/classes/applicationContext.xml</param-value>  </context-param>    <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list></web-app>


当ContextLoaderListener无法找到applicationContext.xml时会报异常

 

严重: Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]

 


 

 

 

0 0
原创粉丝点击