【发布时间】:2018-01-07 20:42:29
【问题描述】:
我正在为一个项目使用完整的基于 Java 的配置。
在部署 webapp 时,我收到以下错误:
2016 年 9 月 6 日上午 8:13:37 org.apache.catalina.core.ApplicationContext 日志 信息:初始化 Spring 根 WebApplicationContext 2016 年 9 月 6 日上午 8:13:37 org.springframework.web.context.ContextLoader initWebApplicationContext INFO: Root WebApplicationContext: 初始化开始 2016 年 9 月 6 日上午 8:13:37 org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh 信息:刷新根 WebApplicationContext:启动日期 [Tue Sep 06 08:13:37 CEST 2016];上下文层次的根 2016 年 9 月 6 日上午 8:13:38 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息:从 ServletContext 资源 [/net.brewspberry.util.SpringWebappConfiguration] 加载 XML bean 定义 2016 年 9 月 6 日上午 8:13:38 org.springframework.web.context.ContextLoader initWebApplicationContext 严重:上下文初始化失败 org.springframework.beans.factory.BeanDefinitionStoreException:IOException 从 ServletContext 资源解析 XML 文档 [/net.brewspberry.util.SpringWebappConfiguration];嵌套异常是 java.io.FileNotFoundException:无法打开 ServletContext 资源 [/net.brewspberry.util.SpringWebappConfiguration] 在 org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344) 在 org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304) 在 org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181) 在 org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217) 在 org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188) 在 org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125) 在 org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94) 在 org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129) 在 org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:612) 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:513) 在 org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444) 在 org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326) 在 org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) 在 org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4813) 在 org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5272) 在 org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) 在 org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407) 在 org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397) 在 java.util.concurrent.FutureTask.run(FutureTask.java:266) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 在 java.lang.Thread.run(Thread.java:745) 原因:java.io.FileNotFoundException:无法打开 ServletContext 资源 [/net.brewspberry.util.SpringWebappConfiguration] 在 org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141) 在 org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330) ... 21 更多
问题是我不明白为什么 Spring 在我注册类配置时尝试加载 XML 配置:
WebappInitializer:
package net.brewspberry.util.config;
import javax.servlet.Filter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import net.brewspberry.filters.AuthentificationFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class SpringWebappInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer implements
WebApplicationInitializer {
public void onStartup(ServletContext servletContext)
throws ServletException {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.setServletContext(servletContext);
// rootContext.setConfigLocation("net.brewspberry.util");
rootContext.register(SpringCoreConfiguration.class);
servletContext.addListener(new ContextLoaderListener(rootContext));
// now the config for the Dispatcher servlet
AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
// mvcContext.setConfigLocation("net.brewspberry.util.config");
//mvcContext.register(SpringWebappConfiguration.class);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
"DispatcherServlet", new DispatcherServlet(mvcContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("*.do");
}
@Override
protected Filter[] getServletFilters() {
return null; // new Filter[] { new AuthentificationFilter() };
}
@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return null;
}
@Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub
return null;
}
}
Web 应用配置:
package net.brewspberry.util.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan({ "net.brewspberry" })
public class SpringWebappConfiguration extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/login.jsp");
}
@Bean(name = "viewResolver")
public InternalResourceViewResolver getViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
我没有必要的 web.xml,因为 WebAppInitializer 替换了它。我试过有没有这条线:
mvcContext.register(SpringWebappConfiguration.class);
但它不会改变任何东西。
此外,在日志中,Spring 搜索 XML 应用程序上下文,而在初始化程序中,它被定义为注释应用程序上下文。没看懂……
有什么想法吗?
【问题讨论】:
-
[/net.brewspberry.util.SpringWebappConfiguration]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource你确定路径吗??...检查stackoverflow.com/questions/23440573/…
标签: java spring spring-mvc