2016-09-06 93 views
1

我對項目使用完全基於java的配置。基於Spring的java配置 - 無法打開ServletContext資源

在部署Web應用程序,我得到以下錯誤:

2016年9月6日上午08時13分37秒org.apache.catalina.core.ApplicationContext登錄 信息:初始化春根WebApplicationContext的 06九月,2016 8:13:37 AM org.springframework.web.context.ContextLoader initWebApplicationContext INFO:Root WebApplicationContext:初始化開始 2016年9月6日上午8時13分37秒org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh INFO:刷新根WebApplicationContext:啓動日期[Tue Sep 06 08:13:37 CEST 2016];上下文層次結構的根 2016年9月6日上午8時13分38秒org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO:從ServletContext資源加載XML bean定義[/net.brewspberry.util.SpringWebappConfiguration] Sep 06 ,2016 8:13:38 AM org.springframework.web.context.ContextLoader initWebApplicationContext SEVERE:上下文初始化失敗 org.springframework.beans.factory.BeanDefinitionStoreException:IOException從ServletContext資源解析XML文檔[/net.brewspberry.util。 SpringWebappConfiguration];嵌套異常是java.io.FileNotFoundException:無法打開ServletContext資源[/net.brewspberry.util.SpringWebappConfiguration] at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344) at org。 springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181) at org.springframework.beans.factory。 support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188) at org.springframework.we b.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: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) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoade r.java:326) 在org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) 在org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4813) 在有機apache.catalina.core.StandardContext.startInternal(StandardContext.java:5272) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) at org.apache.catalina.core.ContainerBase $ StartChild。調用(ContainerBase.java:1407) at org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1397) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPo olExecutor $ Worker.run(的ThreadPoolExecutor。java:617) at java.lang.Thread.run(Thread.java:745) 引起:java.io.FileNotFoundException:無法打開ServletContext資源[/net.brewspberry.util.SpringWebappConfiguration] at org.springframework .web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141) 在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330) ...... 21多個

事情是我不明白爲什麼Spring在我註冊一個類配置時嘗試加載XML配置:

Webap pInitializer:

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中necesary作爲WebAppInitializer替換它。我試過有或沒有這條線:

 mvcContext.register(SpringWebappConfiguration.class); 

但它並沒有改變任何東西。此外,在日誌中,Spring在初始化器中搜索XML應用程序上下文,並將其定義爲註釋應用程序上下文。我不明白...

任何想法?

+0

'[/net.brewspberry.util.SpringWebappConfiguration];嵌套的異常是java.io.FileNotFoundException:無法打開ServletContext資源'你確定有關路徑?? ...檢查http://stackoverflow.com/questions/23440573/caused-by-java-io-filenotfoundexception-could- not-open-servletcontext-resource –

+0

另請檢查:http://stackoverflow.com/questions/22350721/could-not-open-servletcontext-resource-web-inf-applicationcontext-xml – Fr333du

回答

0

您必須指定調度程序servlet來使用SpringWebappConfiguration作爲上下文,或將SpringWebappConfiguration設置爲applicationRoot上下文。

相關問題