我正在尝试在非 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 类也会导致它加载。
关于java - 文件未找到异常 : Could not open ServletContext resource [/<NONE>],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36722397/