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

Source: Internet
Author: User
Tags addchild root directory tomcat apache tomcat log4j



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 Librar
Y 1.1.22. August 31, 2015 3:26:24 pm org.apache.catalina.core.AprLifecycleListener init info: APR Capabilities:ipv6 [FALSE], Sendfile [tr
UE], accept filters [FALSE], random [true]. August 31, 2015 3:26:25 pm org.apache.coyote.AbstractProtocol init info: Initializing protocolhandler ["http-apr-8080"] August 31, 2 015 3:26:25 pm Org.apache.coyote.AbstractProtocol init information: 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 Catalina August 31, 2015 3:26:25 pm Org.apache.catalina.core . Standardengine startinternal Info: Starting Servlet engine:apache tomcat/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: N o Spring webapplicationinitializer types detected on classpath log4j:error Could don't find value for key log4j.appender.std Out Log4j:errorCould not instantiate appender named "stdout".
Log4j:error Could not find value for key Log4j.appender.debug log4j:error Could not instantiate appender named "Debug". 
August 31, 2015 3:26:32 pm org.apache.catalina.core.ApplicationContext Log Info: Initializing Spring root Webapplicationcontext August 31, 2015 3:26:33 pm org.apache.catalina.core.StandardContext listenerstart severity: Exception Sending context initialized E
Vent to listener instance of class Org.springframework.web.context.ContextLoaderListener 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] 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:26:33 pm Org.apache.catalina.core.StandardContext startinternal serious: Context [/ZHSZ] Startup failed due to previous errors August 31, 2015 3:26:33 pm org.apache.catalina.core.ApplicationContext Log Info: Cl osing Spring Root Webapplicationcontext August 31, 2015 3:26:33 pm org.apache.catalina.core.StandardContext listenerstop severity: E Xception sending context destroyed event to listener instance of class Org.springframework.web.context.ContextLoaderListener Java.lang.IllegalStateException:BeanFactory not initialized or already closed-call ' refresh ' before accessing beans via the ApplicationContext at Org.springframework.context.suppo Rt. 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) atJava.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 info: Server Startup in 7589 MS

Note that, in fact, the focus is a word, that is, could not open ServletContext resource [/web-inf/applicationcontext.xml], the anomaly is also very obvious, filenotexception, Unable to open 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> <s Ervlet-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-time out>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 start Tomcat still error, and we from the error message can be seen in the container is still web-inf down to find Applicationcontext.xml, it is clear that the location of the file has been changed by us, and we also modified Why is the configuration of Web. XML loaded 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:


Simple translation of the small paragraph above the mark: if you do not specifically specify a <context-parameter> element named Contextconfigloction, Spring's Contextloderlistener listener will /web-inf/down to find and load the directory named Applicationcontext.xml this file. 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> & Lt;/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.

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.