About the "Could not open ServletContext resource [/web-inf/applicationcontext.xml]" solution

Source: Internet
Author: User
Tags addchild apache tomcat



problem arises



Having recently learned Maven and tried to re-compose the previous project (Springmvc+myabtis) into a MAVEN project, the MAVEN project recommended that the various resource files be placed in the Src/java/resources directory. So I naturally put Spring's config file (including Spring-mvc, Spring-mybatis and other configurations are written in a file) The Applicationcontext.xml file is also placed under the resources directory, and after the refactoring is complete, the Tomcat error is started:

August 31, 2015 3:26:24 pm org.apache.catalina.core.AprLifecycleListener init info: Loaded APR based Apache Tomcat Native Library 1.1.22. August 31, 2015 3:26:24 pm org.apache.catalina.core.AprLifecycleListener init info: APR Capabilities:ipv6 [false], SENDF Ile [True], accept filters [FALSE], random [true]. August 31, 2015 3:26:25 pm org.apache.coyote.AbstractProtocol INIT info: Initia Lizing Protocolhandler ["http-apr-8080"] August 31, 2015 3:26:25 pm org.apache.coyote.AbstractProtocol INIT info: Initializing Protocolhandler ["ajp-apr-8009"] August 31, 2015 3:26:25 pm Org.apache.catalina.startup.Catalina LOAD Info: initialization Processed in 1784 Ms August 31, 2015 3:26:25 pm Org.apache.catalina.core.StandardService startinternal Info: Starting service Cata Lina August 31, 2015 3:26:25 pm org.apache.catalina.core.StandardEngine startinternal Info: Starting Servlet engine:apache TOMCA t/7.0.26 August 31, 2015 3:26:25 pm org.apache.catalina.startup.HostConfig deploydirectory Info: Deploying Web Application Directory E:\java\apache-tomcat-7.0.26\webApps\host-manager August 31, 2015 3:26:26 pm org.apache.catalina.startup.HostConfig deploydirectory info: Deploying Web Application directory E:\java\apache-tomcat-7.0.26\webapps\manager August 31, 2015 3:26:26 PM Org.apache.catalina.startup.HostConfig deploydirectory Info: Deploying Web application Directory E:\java\ Apache-tomcat-7.0.26\webapps\root August 31, 2015 3:26:26 pm org.apache.catalina.startup.HostConfig deploydirectory Info: Deploying Web application directory E:\java\apache-tomcat-7.0.26\webapps\zhsz August 31, 2015 3:26:26 PM Org.apache.catalina.loader.WebappClassLoader validatejarfile Info: validatejarfile (E:\java\apache-tomcat-7.0.26\ Webapps\zhsz\web-inf\lib\javaee.jar)-jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending Class:javax/servlet/servlet.class August 31, 2015 3:26:32 pm org.apache.catalina.core.ApplicationContext Log Info: No Spring Webapplicationinitializer types detected on Classpathlog4j:error Could not find value for key log4j.appender.std Outlog4j:error Could not instantiate AppenDer named "stdout". Log4j:error Could not find value for key Log4j.appender.debuglog4j:ERROR Could not instantiate Appender Named "Debug". August 31, 2015 3:26:32 pm org.apache.catalina.core.ApplicationContext Log Info: Initializing Spring root Webappli Cationcontext August 31, 2015 3:26:33 pm org.apache.catalina.core.StandardContext listenerstart severity: Exception Sending context Initialized event to listener instance of class Org.springframework.web.context.ContextLoaderListenerorg.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]at Org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions ( xmlbeandefinitionreader.java:341) at Org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions (Xmlbeandefinitionreader.java : 302) at ORG.SPRINGFRAmework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions ( abstractbeandefinitionreader.java:174) at Org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions ( abstractbeandefinitionreader.java:209) at Org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions ( abstractbeandefinitionreader.java:180) at Org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions ( xmlwebapplicationcontext.java:125) at Org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions ( xmlwebapplicationcontext.java:94) at Org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory ( abstractrefreshableapplicationcontext.java:131) at Org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory ( abstractapplicationcontext.java:522) at Org.springframework.context.support.AbstractApplicationContext.refresh ( abstractapplicationcontext.java:436) at Org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext (Contextloader.java : 385) at Org.springframework.web.context.ContextLoader.initWebApplicationContext (contextloader.java:284) at Org.springframework.web.context.ContextLoaderListener.contextInitialized (contextloaderlistener.java:111) at Org.apache.catalina.core.StandardContext.listenerStart (standardcontext.java:4779) at Org.apache.catalina.core.StandardContext.startInternal (standardcontext.java:5273) at Org.apache.catalina.util.LifecycleBase.start (lifecyclebase.java:150) at Org.apache.catalina.core.ContainerBase.addChildInternal (containerbase.java:895) at Org.apache.catalina.core.ContainerBase.addChild (containerbase.java:871) at Org.apache.catalina.core.StandardHost.addChild (standardhost.java:615) at Org.apache.catalina.startup.HostConfig.deployDirectory (hostconfig.java:1095) at Org.apache.catalina.startup.hostconfig$deploydirectory.run (hostconfig.java:1617) at Java.util.concurrent.executors$runnableadapter.cAll (executors.java:471) at Java.util.concurrent.futuretask$sync.innerrun (futuretask.java:334) at Java.util.concurrent.FutureTask.run (futuretask.java:166) at Java.util.concurrent.ThreadPoolExecutor.runWorker ( threadpoolexecutor.java:1110) at Java.util.concurrent.threadpoolexecutor$worker.run (ThreadPoolExecutor.java:603) At Java.lang.Thread.run (thread.java:722) caused by:java.io.FileNotFoundException:Could not open ServletContext Resource [/web-inf/applicationcontext.xml]at Org.springframework.web.context.support.ServletContextResource.getInputStream (servletcontextresource.java:140) At Org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions ( xmlbeandefinitionreader.java:328) ... + More August 31, 2015 3:26:33 pm org.apache.catalina.core.StandardContext startinternal severity: Error listenerstart August 31, 2015 3:2  6:33 pm Org.apache.catalina.core.StandardContext startinternal severity: Context [/zhsz] startup failed due to previous errors August 31, 2015 3:26:33 pm Org.apache.catalina.core.ApplicaTioncontext Log Info: Closing Spring root Webapplicationcontext August 31, 2015 3:26:33 PM Org.apache.catalina.core.StandardContext listenerstop Severity: Exception sending context destroyed event to listener instance of class Org.springframework.web.context.ContextLoaderListenerjava.lang.IllegalStateException:BeanFactory not Initialized or already closed-call ' refresh ' before accessing beans via the Applicationcontextat Org.springframework.con Text.support.AbstractRefreshableApplicationContext.getBeanFactory (Abstractrefreshableapplicationcontext.java : 172) at Org.springframework.context.support.AbstractApplicationContext.destroyBeans ( abstractapplicationcontext.java:1066) at Org.springframework.context.support.AbstractApplicationContext.doClose ( abstractapplicationcontext.java:1040) at Org.springframework.context.support.AbstractApplicationContext.close ( abstractapplicationcontext.java:988) at Org.springframework.web.context.ContextLoader.closeWebApplicationContext (contextloader.java:556) at Org.springframEwork.web.context.ContextLoaderListener.contextDestroyed (contextloaderlistener.java:142) at Org.apache.catalina.core.StandardContext.listenerStop (standardcontext.java:4819) at Org.apache.catalina.core.StandardContext.stopInternal (standardcontext.java:5466) at Org.apache.catalina.util.LifecycleBase.stop (lifecyclebase.java:232) at Org.apache.catalina.util.LifecycleBase.start (lifecyclebase.java:160) at Org.apache.catalina.core.ContainerBase.addChildInternal (containerbase.java:895) at Org.apache.catalina.core.ContainerBase.addChild (containerbase.java:871) at Org.apache.catalina.core.StandardHost.addChild (standardhost.java:615) at Org.apache.catalina.startup.HostConfig.deployDirectory (hostconfig.java:1095) at Org.apache.catalina.startup.hostconfig$deploydirectory.run (hostconfig.java:1617) at Java.util.concurrent.executors$runnableadapter.call (executors.java:471) at java.util.concurrent.futuretask$ Sync.innerrun (futuretask.java:334) at Java.util.concurrent.FutureTask.run (futuretask.java:166) at Java.util.Concurrent. Threadpoolexecutor.runworker (threadpoolexecutor.java:1110) at Java.util.concurrent.threadpoolexecutor$worker.run (threadpoolexecutor.java:603) at Java.lang.Thread.run (thread.java:722) August 31, 2015 3:26:33 PM Org.apache.coyote.AbstractProtocol Start Info: Starting Protocolhandler ["http-apr-8080"] August 31, 2015 3:26:33 PM Org.apache.coyote.AbstractProtocol Start Info: Starting Protocolhandler ["ajp-apr-8009"] August 31, 2015 3:26:33 PM Org.apache.catalina.startup.Catalina Start Information: Server Startup in 7589 MS

Notice, in fact, the focus is a word, that is Could not open ServletContext resource [/web-inf/applicationcontext.xml], the exception is also very obvious, Filenotexception, unable to open the ServletContext resource file. Let's look at the configuration of 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" 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>xxybm</ Display-name><listener><listener-class>org.springframework.web.context.contextloaderlistener </listener-class></listener><servlet><servlet-name>springMvc</servlet-name>< servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class><init-param>< param-name>contextconfiglocation</param-name><param-value>classpath:applicationcontext.xml</ param-value></init-param><load-on-startup>2</load-on-startup></servlet>< Servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/app/*</url-pattern ></servlet-mapping><filtEr><filter-name>securityfilter</filter-name><filter-class>net.zhsz.util.logincheckfilter </filter-class></filter><filter-mapping><filter-name>securityfilter</filter-name ><url-pattern>*.jsp</url-pattern></filter-mapping><filter-mapping><filter-name >SecurityFilter</filter-name><url-pattern>/app/*</url-pattern></filter-mapping>< Session-config><session-timeout>30</session-timeout></session-config><error-page> <exception-type>java.lang.Exception</exception-type><location>/500.html</location>< /error-page></web-app>

Because my spring configuration file-- Applicationcontext.xml is placed in the Web-inf root directory, in the project run is normal, after the re-composition of the MAVEN project, my profile should naturally be placed under the Resources folder under SRC, then in Web. XML I will naturally The parameter values for the contextconfiglocation in <init-param> are modified from web-inf/applicationcontext.xml to classpath: Applicationcontext.xml , then after the launch Tomcat still error, And we can see from the error message that the container is still in the Web-inf down to find Applicationcontext.xml, it is clear that the location of the file has been changed by us, and we have also modified the configuration of Web. XML to load from here? The reason is that we are missing another important configuration.



solve the problem



The problem of blind search often does not quickly get an effective solution, so we first look for clues from the official, look at the official spring documents, in the Web chapter can be found in the passage:


Simply translate the marked small paragraph: If you do not intentionally specify a <context-parameter> element with the parameter named Contextconfigloction, Then spring's Contextloderlistener listener will/web-inf/down to find and load the file named Applicationcontext.xml under the directory . So, we should add the <context-param> tag in Web. XML and specify the location of the spring core file again:

<context-param><param-name>contextconfiglocation</param-name><param-value>classpath: Applicationcontext.xml</param-value></context-param>


This will be OK, note that this configuration as far as possible to write in front of the position, because the Web project will initialize the context after Tomcat startup, our configuration is to specify the context configuration of the file location, after the completion of the context initialization should continue to load the remaining various configurations, And our Springmvc servlet is just a configuration of the SPRINGMVC framework, just like the configuration of struts2, and the whole project is managed by spring. The following is the modified Web. XML configuration file:

<?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" 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>xxybm</ Display-name><context-param><param-name>contextconfiglocation</param-name><param-value >classpath:applicationContext.xml</param-value></context-param><listener>< Listener-class>org.springframework.web.context.contextloaderlistener</listener-class></listener ><servlet><servlet-name>springMvc</servlet-name><servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class><init-param><param-name> Contextconfiglocation</param-name><param-value>classpath:applicationcontext.xml</param-value ></init-param><load-on-startup>2</load-on-startUp></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern >/app/*</url-pattern></servlet-mapping><filter><filter-name>securityfilter</ filter-name><filter-class>net.zhsz.util.logincheckfilter</filter-class></filter>< filter-mapping><filter-name>securityfilter</filter-name><url-pattern>*.jsp</ Url-pattern></filter-mapping><filter-mapping><filter-name>securityfilter</filter-name ><url-pattern>/app/*</url-pattern></filter-mapping><session-config>< Session-timeout>30</session-timeout></session-config><error-page><exception-type> java.lang.exception</exception-type><location>/500.html</location></error-page></ Web-app>

Finally, start the project again, run normally, spring and SPRINGMVC both load and initialize normally. Before the online search someone said modified path, asterisking number, but also a few jar package, the answer is strange, and then can not solve the problem, in short this blog hope to encounter this unusual friend help, the End.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

About the "Could not open ServletContext resource [/web-inf/applicationcontext.xml]" solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.