java - 文件未找到异常 : Could not open ServletContext resource [/<NONE>]

标签 java spring spring-boot

我正在尝试在非 Spring Boot(而是 Spring 4.2)环境中使用 Spring Boot Devtools,这当然意味着要做一些 Spring Boot 所做的事情。我遇到以下异常

SEVERE: Exception sending context initialized event to listener instance of class com.myapp.spring.config.MyAppContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/<NONE>]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/<NONE>]
    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:609)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:510)
    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 com.myapp.spring.config.MyAppContextLoaderListener.contextInitialized(MyAppContextLoaderListener.java:52)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4810)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5255)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
    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 [/<NONE>]
    at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
    ... 22 more

Apr 18, 2016 5:11:44 PM org.apache.catalina.core.StandardContext startInternal

这是 ContextListener

public class MyAppContextLoaderListener extends ContextLoaderListener
{
    private static final String DEFAULT_ACTIVE_PROFILES = "";
    private static final String SERVER_PROPERTIES = "jdbc/serverProperties";
    private static final String SERVER_PROPERTIES_DEFAULTS = "jdbc/serverProperties-defaults";
    private static final String TOMCAT_CONTEXT_NAME = "java:/comp/env/";
    private static final String JETTY_CONTEXT_NAME = "";
    private static final String JETTY_SERVERNAME_TOKEN = "jetty";

    private static final Logger log = LoggerFactory.getLogger(MyAppContextLoaderListener.class);
    private static final String SPRING_PROFILES_ACTIVE = "spring.profiles.active";

    public MyAppContextLoaderListener()
    {
    }

    public MyAppContextLoaderListener(WebApplicationContext context)
    {
        super(context);
    }

    @Override
    public void contextInitialized(ServletContextEvent event) {
        log.info( "Starting in timezone {}...", TimeZone.getDefault().getID() );
        long start = System.nanoTime();

        String activeProfiles = System.getProperty(SPRING_PROFILES_ACTIVE, null);
        if(activeProfiles == null){

            activeProfiles = lookupActiveProfiles(event);
        }

        log.info("Setting " + SPRING_PROFILES_ACTIVE + "=" + activeProfiles);
        System.setProperty(SPRING_PROFILES_ACTIVE, activeProfiles);

        super.contextInitialized(event);

        log.info(String.format("started in %s Seconds.", TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - start)));
    }

    private String lookupActiveProfiles(ServletContextEvent event)
    {
        String activeProfiles = DEFAULT_ACTIVE_PROFILES;

        boolean isJetty = event.getServletContext().getServerInfo().contains(JETTY_SERVERNAME_TOKEN);
        String name = (isJetty ? JETTY_CONTEXT_NAME : TOMCAT_CONTEXT_NAME);
        Context context = null;
        try
        {
            context = (Context) new InitialContext().lookup(name);
        }
        catch (NamingException ex)
        {
            log.error("Couldn't configure profiles from jndi", ex);
        }

        if (context != null)
        {
            try (FileReader serverPropertiesDefaults
                    = new FileReader(convertToFileName(context, SERVER_PROPERTIES_DEFAULTS));
                FileReader serverProperties = new FileReader(convertToFileName(context, SERVER_PROPERTIES)))
            {


                Properties properties = new Properties();
                properties.load(serverPropertiesDefaults);
                properties.load(serverProperties);

                activeProfiles = properties.getProperty(SPRING_PROFILES_ACTIVE, DEFAULT_ACTIVE_PROFILES);

            }
            catch (NamingException | IOException e)
            {
                log.error("Couldn't configure profiles from jndi", e);
            }
        }
        return activeProfiles;
    }

    private String convertToFileName(Context context, String name) throws NamingException
    {
        //strip off the "file:"
        return ((String)context.lookup(name)).substring(5);
    }

}

它在寻找什么?

更新

我找到了这个嵌套类

JerseyAutoConfiguration...

@Order(Ordered.HIGHEST_PRECEDENCE)
public static final class JerseyWebApplicationInitializer
        implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // We need to switch *off* the Jersey WebApplicationInitializer because it
        // will try and register a ContextLoaderListener which we don't need
        servletContext.setInitParameter("contextConfigLocation", "<NONE>");
    }

}

我认为我添加的这段代码禁用了它

@EnableAutoConfiguration( 排除 = JerseyAutoConfiguration.class ) 公共(public)类 WebConfig 实现 WebApplicationInitializer

但似乎并非如此,我如何禁用此初始化程序?

最佳答案

所以当然不支持,但答案是 JerseyAutoConfiguration 仍在加载。它的 WebInitializer 具有最高优先级,即使排除自动配置也会加载。注意:我们不使用 Jersey,有一个标准的 api 类也会导致它加载。

https://github.com/spring-projects/spring-boot/issues/5740

关于java - 文件未找到异常 : Could not open ServletContext resource [/<NONE>],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36722397/

相关文章:

java - 玩! Java 8 的框架支持 可选

java - Android:麦克风 AudioSource 导致参数不受支持,VerifyAndSetParameter 失败错误

spring - 使用 Spring 的 LocalSessionFactoryBean 时的 Permgen 问题

java - MockMvc Controller 测试并返回 NullPointerException

java - Hibernate 和 Spring - org.hibernate.PropertyAccessException : Could not set field value [1] value by reflection

java - 使用 JAXB 从 webservice 解析 xml 的一些问题

java - OpenEJB 中的@Module 注解是什么以及如何使用它?

java - 使用 mvn exec :java 执行 spring 应用程序

java - 如何正确向Spring Restful WebService提交特殊字符?

java - 调用 Rest 身份验证 Controller 时出现 EOFException