私はEclipse Mavenを使用しています。ここでSpring mvcおよびSpring securityを使用しています。私がそれを起動することにしたとき、コンテキストを初期化することに成功しません:
Could not open ServletContext resource [/WEB-INF/spring-dispatcher-servlet.xml]
以下の詳細:
これは私の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>Web Manager</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/app-config.xml, /WEB-INF/spring/security-config.xml
</param-value>
</context-param>
<!-- Spring MVC -->
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
これは私のapp-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="fr.tessa.webmanager" />
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<!-- misc -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Configure the multipart resolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="1000000" />
</bean>
<!-- Configures Hibernate - Database Config -->
<import resource="dbconfig/validationdb-config.xml" />
<import resource="dbconfig/db-config.xml" />
これは私のsecurity-config.xml
です
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<http use-expressions="true">
<intercept-url pattern="/welcome*" access="ROLE_USER" />
<form-login login-page="/login" default-target-url="/welcome"
authentication-failure-url="/loginfailed" />
<logout logout-success-url="/logout" />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="mkyong" password="123456" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
私の問題は、アプリケーションを起動したときに次のようなエラーが発生することです
Infos: Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-dispatcher-servlet.xml]
févr. 19, 2013 10:50:09 AM org.springframework.web.servlet.FrameworkServlet initServletBean
Grave: Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/spring-dispatcher-servlet.xml]; nested exception is Java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/spring-dispatcher-servlet.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.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.Java:631)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.Java:588)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.Java:645)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.Java:508)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.Java:449)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.Java:133)
at javax.servlet.GenericServlet.init(GenericServlet.Java:212)
at org.Apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.Java:1173)
at org.Apache.catalina.core.StandardWrapper.load(StandardWrapper.Java:993)
at org.Apache.catalina.core.StandardContext.loadOnStartup(StandardContext.Java:4350)
at org.Apache.catalina.core.StandardContext.start(StandardContext.Java:4659)
at org.Apache.catalina.core.ContainerBase.start(ContainerBase.Java:1045)
at org.Apache.catalina.core.StandardHost.start(StandardHost.Java:785)
at org.Apache.catalina.core.ContainerBase.start(ContainerBase.Java:1045)
at org.Apache.catalina.core.StandardEngine.start(StandardEngine.Java:445)
at org.Apache.catalina.startup.Embedded.start(Embedded.Java:825)
at org.codehaus.mojo.Tomcat.AbstractRunMojo.startContainer(AbstractRunMojo.Java:558)
at org.codehaus.mojo.Tomcat.AbstractRunMojo.execute(AbstractRunMojo.Java:255)
at org.Apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.Java:101)
at org.Apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.Java:209)
at org.Apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.Java:153)
at org.Apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.Java:145)
at org.Apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.Java:84)
at org.Apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.Java:59)
at org.Apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.Java:183)
at org.Apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.Java:161)
at org.Apache.maven.DefaultMaven.doExecute(DefaultMaven.Java:320)
at org.Apache.maven.DefaultMaven.execute(DefaultMaven.Java:156)
at org.Apache.maven.cli.MavenCli.execute(MavenCli.Java:537)
at org.Apache.maven.cli.MavenCli.doMain(MavenCli.Java:196)
at org.Apache.maven.cli.MavenCli.main(MavenCli.Java:141)
at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at Sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at Sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at Java.lang.reflect.Method.invoke(Unknown Source)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.Java:290)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.Java:230)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.Java:409)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.Java:352)
Caused by: Java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/spring-dispatcher-servlet.xml]
at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.Java:140)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.Java:328)
... 48 more
/WEB-INF/spring-dispatcher-servlet.xml
が見つからないと言っていますが、web.xml
には入れませんでした。私の構成ファイルはapp-config.xml
です
Spring reference doc からの引用:
DispatcherServletの初期化時に、Spring MVCはWebアプリケーションのWEB-INFディレクトリで[servlet-name] -servlet.xmlという名前のファイルを探し、そこに定義されたBeanを作成します...
サーブレットの名前はspring-dispatcher
ので、/WEB-INF/spring-dispatcher-servlet.xml
。このサーブレット構成が必要で、そこにWeb関連のBean(コントローラー、ビューリゾルバーなど)を定義する必要があります。サーブレットコンテキストとグローバルアプリケーションコンテキスト(app-config.xml
あなたの場合)。
もう1つ、サーブレット構成xmlの命名規則が気に入らない場合は、構成を明示的に指定できます。
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Java設定でこのエラーが発生する場合、通常はアプリケーションコンテキストをDispatcherServlet
コンストラクターに渡すのを忘れているためです。
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(WebConfig.class);
ServletRegistration.Dynamic dispatcher = sc.addServlet("dispatcher",
new DispatcherServlet()); // <-- no constructor args!
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");
コンテキストをコンストラクタargとして追加して修正します。
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(WebConfig.class);
ServletRegistration.Dynamic dispatcher = sc.addServlet("dispatcher",
new DispatcherServlet(ctx)); // <-- hooray! Spring doesn't look for XML files!
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");
戦争の構築中に、pom.xmlのmaven warプラグインブロックにすべてのファイル(特にxmlファイル)が含まれていることを確認してください。ただし、.Javaファイルを含める必要はありません。
<plugin>
<groupId>org.Apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<configuration>
<webResources>
<resources>
<directory>WebContent</directory>
<includes>
<include>**/*.*</include><!--this line includes the xml files into the war, which will be found when it is exploded in server during deployment -->
</includes>
<excludes>
<exclude>*.Java</exclude>
<excludes>
</resources>
</webResources>
<webXml>WebContent/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
The file name u used spring-dispatcher-servlet.xml
kindly check in web.xml
servlet name as spring-dispatcher at both tag <servlet> and <servlet-mapping>
in your case it should be
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class></servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern></url-pattern>
</servlet-mapping>
WebLogicでこの例外が発生しましたが、これはWebLogicのバグであることがわかりました。詳細については、こちらをご覧ください: Spring Boot exception:ServletContextリソース[/WEB-INF/dispatcherServlet-servlet.xml]を開けませんでした