1. 程式人生 > >java.io.FileNotFoundException: class path resource [templates/] spring-cloud-netflix-eureka-server

java.io.FileNotFoundException: class path resource [templates/] spring-cloud-netflix-eureka-server

用main方法啟動spring cloud eureka server工程的時候,報如下錯誤:

java.io.FileNotFoundException: class path resource [templates/] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/cloud/spring-cloud-netflix-eureka-server/1.3.5.RELEASE/spring-cloud-netflix-eureka-server-1.3
.5.RELEASE.jar!/templates/ at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:215) ~[spring-core-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:53) ~[spring-core-4.3.12.RELEASE.jar:4.3
.12.RELEASE] at org.springframework.ui.freemarker.FreeMarkerConfigurationFactory.getTemplateLoaderForPath(FreeMarkerConfigurationFactory.java:338) [spring-context-support-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.ui.freemarker.FreeMarkerConfigurationFactory.createConfiguration
(FreeMarkerConfigurationFactory.java:290) [spring-context-support-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer.afterPropertiesSet(FreeMarkerConfigurer.java:116) [spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) [spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) [spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) [spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) [spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) [spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) [spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) [spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) [spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) [spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) [spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) [spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] at com.xxx.SpringCloudEurekaServerApplication.main(SpringCloudEurekaServerApplication.java:19) [classes/:na]

如何解決?

後來發現調查發現spring-context-support這個jar包裡的這個類:FreeMarkerConfigurationFactory.java下面有這樣一段程式碼:

   protected TemplateLoader getTemplateLoaderForPath(String templateLoaderPath) {
        if(this.isPreferFileSystemAccess()) {
            try {
                Resource ex = this.getResourceLoader().getResource(templateLoaderPath);
                File file = ex.getFile();
                if(this.logger.isDebugEnabled()) {
                    this.logger.debug("Template loader path [" + ex + "] resolved to file path [" + file.getAbsolutePath() + "]");
                }

                return new FileTemplateLoader(file);
            } catch (Exception var4) {
                if(this.logger.isDebugEnabled()) {
                    this.logger.debug("Cannot resolve template loader path [" + templateLoaderPath + "] to [java.io.File]: using SpringTemplateLoader as fallback", var4);
                }

                return new SpringTemplateLoader(this.getResourceLoader(), templateLoaderPath);
            }
        } else {
            this.logger.debug("File system access not preferred: using SpringTemplateLoader");
            return new SpringTemplateLoader(this.getResourceLoader(), templateLoaderPath);
        }
    }

通過這段程式碼知道,也就是在debug模式下,會列印這個exception,於是把application.properties中的debug註釋掉:

#debug=true
#logging.level.org.springframework.web=DEBUG
#logging.level.com.xxx=DEBUG

把debug模式註釋掉,上面的錯誤就不會打印出來了!

更多系列文章推薦: