FileNotFoundException:无法打开ServletContext资源[/ <none>]

时间:2016-04-19 15:08:33

标签: java spring spring-boot

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

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

}

这里是ContextListener

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( exclude = JerseyAutoConfiguration.class )
public class WebConfig implements WebApplicationInitializer

我认为我添加的代码禁用了

Ball

但似乎不是,我该如何禁用此初始化程序?

1 个答案:

答案 0 :(得分:0)

当然不支持,但答案是JerseyAutoConfiguration仍然被加载。它的WebInitializer是最高优先级,即使您排除自动配置也会加载它。注意:我们不会使用泽西那里的标准api类,这也会导致加载。

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