我正在为项目使用完全基于java的配置。
部署webapp时,出现以下错误:
2016年9月6日上午8:13:37 org.apache.catalina.core.ApplicationContext log 信息:初始化Spring根WebApplicationContext 2016年9月6日上午8:13:37 org.springframework.web.context.ContextLoader initWebApplicationContext 信息: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资源加载XML bean定义[/net.brewspberry.util.SpringWebappConfiguration] 2016年9月6日上午8:13:38 org.springframework.web.context.ContextLoader initWebApplicationContext 严重:上下文初始化失败 org.springframework.beans.factory.BeanDefinitionStoreException:从ServletContext资源解析XML文档的IOException [/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) at 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) at 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) at java.util.concurrent.FutureTask.run(FutureTask.java:266) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at 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在注册Class配置时尝试加载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;
}
}
Webapp配置:
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应用程序上下文,它被定义为注释应用程序上下文。我不明白......
有什么想法吗?
答案 0 :(得分:0)
您必须指定调度程序servlet以将SpringWebappConfiguration用作上下文或将SpringWebappConfiguration设置为applicationRoot上下文。