有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

基于Spring java的配置无法打开ServletContext资源

我正在为一个项目使用基于java的完整配置

部署webapp时,我遇到以下错误:

Sep 06, 2016 8:13:37 AM org.apache.catalina.core.ApplicationContext log INFO: Initializing Spring root WebApplicationContext Sep 06, 2016 8:13:37 AM org.springframework.web.context.ContextLoader initWebApplicationContext INFO: Root WebApplicationContext: initialization started Sep 06, 2016 8:13:37 AM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh INFO: Refreshing Root WebApplicationContext: startup date [Tue Sep 06 08:13:37 CEST 2016]; root of context hierarchy Sep 06, 2016 8:13:38 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from ServletContext resource [/net.brewspberry.util.SpringWebappConfiguration] Sep 06, 2016 8:13:38 AM org.springframework.web.context.ContextLoader initWebApplicationContext SEVERE: Context initialization failed org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/net.brewspberry.util.SpringWebappConfiguration]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/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.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:129) at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:612) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:513) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4813) at org.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.call(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.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/net.brewspberry.util.SpringWebappConfiguration] at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330) ... 21 more

问题是我不明白为什么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;
}

}

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;
    }

}

我没有网络。WebAppInitializer取代xml是必要的。我试过用或不用那句话:

        mvcContext.register(SpringWebappConfiguration.class);

但这并没有改变任何事情

此外,在日志中,Spring搜索XML应用程序上下文,而在初始值设定项中,它被定义为注释应用程序上下文。我不明白

知道吗


共 (1) 个答案

  1. # 1 楼答案

    必须指定dispatcher servlet才能将SpringWebappConfiguration用作上下文,或将SpringWebappConfiguration设置为applicationRoot上下文