Spring如何使用Java 8类,但它在Java 7上运行? [英] How does Spring use Java 8 classes, yet it runs on Java 7?

查看:153
本文介绍了Spring如何使用Java 8类,但它在Java 7上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在Spring Framework中下载了Spring MVC(版本4.0.2.RELEASE)模块的源代码。我的目的是针对模块的实际源代码而不是实际的 .jar 文件运行。 (长话,仅用于测试目的)。

I recently downloaded the source code for the Spring MVC (Version 4.0.2.RELEASE) module within the Spring Framework. My intention is to run against the actual source code for the module instead of the actual .jar file. (Long story, only for testing purposes).

下载源代码后,我删除了 jar 文件我的项目,编译并部署到服务器。当我点击调度程序servlet处理的其中一个URL时,我看到了错误:

After downloading the source code, I removed the jar file from my project, compiled and deployed to the server. When I hit one of the URLs handled by the dispatcher servlet I was presented with the error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0': Invocation of init method failed; nested exception is java.lang.Error: Unresolved compilation problems: 
    The import java.time cannot be resolved
    The method toZoneId() is undefined for the type TimeZone
    ZoneId cannot be resolved

经过一些研究和挖掘源代码后,我意识到错误中提到的类是JDK / JRE的一部分1.8。在删除 .jar 文件之前,我的项目在JDK / JRE 1.7上运行没有问题。

After some research and digging into the source code I realized the class mentioned in the error is part of JDK/JRE 1.8. Before removing the .jar file my project ran without issue on JDK/JRE 1.7.

我的问题是怎么做Spring包含JDK 1.8中的类,但仍设法在JDK / JRE 1.7下运行?为什么在使用Spring的 .jar 文件时不会抛出异常,但是在我提供源代码时抛出异常(并且缺少jar文件)?

My question is how does Spring include classes from JDK 1.8, yet still manage to run under JDK/JRE 1.7? Why is the exception not thrown when using Spring's .jar file, but it is thrown when I provide the source (and the jar file is missing)?

全栈跟踪

Mar 26, 2014 7:27:18 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet dispatcher
java.lang.Error: Unresolved compilation problems: 
    The import java.time cannot be resolved
    The method toZoneId() is undefined for the type TimeZone
    ZoneId cannot be resolved

    at org.springframework.web.servlet.mvc.method.annotation.ServletRequestMethodArgumentResolver.<init>(ServletRequestMethodArgumentResolver.java:23)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.getDefaultArgumentResolvers(RequestMappingHandlerAdapter.java:522)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.afterPropertiesSet(RequestMappingHandlerAdapter.java:485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1274)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1186)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:858)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)


推荐答案

标题问题的答案是:使用Java 8构建Spring,但与Java 7(和6)兼容。

The answer to the headline question is: Spring builds with Java 8, but is compatible with Java 7 (and 6).

ServletRequestMethodArgumentResolver 导入 java.time.ZoneId ,但它仅指除非Java 8在类路径上,否则嵌套类中的该类未实例化。 Spring源代码中有很多这样的例子(例如,自从它开始支持Java 5以来一直如此)。

ServletRequestMethodArgumentResolver does import java.time.ZoneId, but it only refers to that class in a nested class which isn't instantiated unless Java 8 is on the classpath. There are many examples like this in Spring source code (always have been, e.g. since it started supporting Java 5).

这篇关于Spring如何使用Java 8类,但它在Java 7上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆