Spring/Java 错误:命名空间元素 'annotation-config' ... 在 JDK 1.5 及更高版本上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23813369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:21:19  来源:igfitidea点击:

Spring/Java error: namespace element 'annotation-config' ... on JDK 1.5 and higher

javaspringillegalstateexception

提问by user3665944

I have Spring/Java App that is compiled with Compiler compliance level 1.5.

我有使用编译器合规性级别 1.5编译的 Spring/Java 应用程序。

I have a new Linux setup where I downloaded Apache Tomcat 8.0.8.

我有一个新的 Linux 设置,我在其中下载了Apache Tomcat 8.0.8

I downloaded JDK 8u5.

我下载了JDK 8u5

I set the path in bash as follows:

我在bash中设置路径如下:

PATH=$PATH:$HOME/jdk1.8.0_05/bin
export PATH

Java -version reports:

Java -version 报告:

java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

And set in setnenv.sh (for Tomcat):

并在 setnenv.sh(用于 Tomcat)中设置:

JDK_HOME=/home/userid/jdk1.8.0_05

When I deploy my WAR file I get below error. I think Tomcat doesn't seem to use the Java I installed. I have followed the setup instructions. PS: I also tried JRE instead of JDK and same issue.

当我部署我的 WAR 文件时,出现以下错误。我认为 Tomcat 似乎没有使用我安装的 Java。我已按照设置说明进行操作。PS:我也试过 JRE 而不是 JDK 和同样的问题。

22-May-2014 11:34:54.070 INFO [http-nio-8080-exec-4] org.apache.catalina.core.ApplicationContext.log Initializing Spring root WebApplicationContext
22-May-2014 11:34:54.512 SEVERE [http-nio-8080-exec-4] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
 org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from file [/home/userid/apache-tomcat-8.0.8/webapps/myApplication-QA/WEB-INF/classes/config/spring/securityContext.xml]; nested exception is **java.lang.IllegalStateException: Context namespace element 'annotation-config' and its parser class [org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser] are only available on JDK 1.5 and higher**
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:420)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:92)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:423)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:353)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4750)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5170)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.manager.ManagerServlet.start(ManagerServlet.java:1270)
    at org.apache.catalina.manager.HTMLManagerServlet.start(HTMLManagerServlet.java:673)
    at org.apache.catalina.manager.HTMLManagerServlet.doPost(HTMLManagerServlet.java:221)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:301)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.filters.CsrfPreventionFilter.doFilter(CsrfPreventionFilter.java:213)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:615)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:78)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1033)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalStateException: Context namespace element 'annotation-config' and its parser class [org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser] are only available on JDK 1.5 and higher
    at org.springframework.context.config.ContextNamespaceHandler.parse(ContextNamespaceHandler.java:65)
    at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:69)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1253)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1243)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:135)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:92)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:507)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:398)
    ... 49 more

22-May-2014 11:34:54.518 INFO [http-nio-8080-exec-4] org.apache.catalina.core.ApplicationContext.log Closing Spring root WebApplicationContext

采纳答案by Andrei Stefan

The class that's throwing the exception is using this code to check for Java version:

抛出异常的类使用此代码来检查 Java 版本:

static {
        javaVersion = System.getProperty("java.version");
        // version String should look like "1.4.2_10"
        if (javaVersion.indexOf("1.7.") != -1) {
            majorJavaVersion = JAVA_17;
        }
        else if (javaVersion.indexOf("1.6.") != -1) {
            majorJavaVersion = JAVA_16;
        }
        else if (javaVersion.indexOf("1.5.") != -1) {
            majorJavaVersion = JAVA_15;
        }
        else {
            // else leave 1.4 as default (it's either 1.4 or unknown)
            majorJavaVersion = JAVA_14;
        }
    }

So, when Spring 2.5 was first released, the code didn't assume it will be run in a Java version that's later than 1.7. For Java 8 and beyond, the code above will assume default 1.4 version. Because of this, the annotations part will complain.

因此,当 Spring 2.5 首次发布时,代码并未假设它将在 1.7 之后的 Java 版本中运行。对于 Java 8 及更高版本,上面的代码将假定默认为 1.4 版本。因此,注释部分会抱怨。

I think you either need to upgrade your Spring version or use Java 7. Spring 2.5 has been EOLedfor quite some time now, anyway.

我认为您要么需要升级 Spring 版本,要么使用 Java 7。无论如何Spring 2.5 已经停产一段时间了。

回答by arun

I happen to be another unfortunate user of a very old project (developed in 2008!) which is still using Spring 2.5. And it is not a Maven project either, so upgrading to later versions of Spring was taking a long time with dependencies failing and giving build errors. I downgraded tomcat JRE to 1.7 and it worked fine.

我碰巧是一个非常老的项目(2008 年开发!)的另一个不幸用户,该项目仍在使用 Spring 2.5。而且它也不是 Maven 项目,因此升级到更高版本的 Spring 需要很长时间,依赖项失败并出现构建错误。我将 tomcat JRE 降级到 1.7 并且运行良好。

Just documenting how to do that in eclipse in case someone needs help:

只是记录如何在 eclipse 中做到这一点,以防有人需要帮助:

  1. If you do not already have Java 7, download JDK 7 and install it somewhere like C:\Program Files\Java\jdk1.7.0_71

  2. Then go to eclipse and double click on Servers > Tomcat vX.Y server

  3. Click on Runtime Environment, then Installed JREs

  4. Then Add> Standard VMand click Next. You see a window like this:

    Adding a new JRE

  5. Click on Directory...and browse to the folder where you installed the JDK (i.e. C:\Program Files\Java\jdk1.7.0_71. JDK is fine, no need for JRE). Click Finish.

  6. Then make sure to select the JRE for Java 7 and you are all set.

  1. 如果您还没有 Java 7,请下载 JDK 7 并将其安装在类似的地方 C:\Program Files\Java\jdk1.7.0_71

  2. 然后去eclipse并双击 Servers > Tomcat vX.Y server

  3. 点击Runtime Environment,然后Installed JREs

  4. 然后Add>Standard VM并单击Next。你会看到一个这样的窗口:

    添加新的 JRE

  5. 单击Directory...并浏览到您安装 JDK 的文件夹(即C:\Program Files\Java\jdk1.7.0_71.JDK 很好,不需要 JRE)。单击Finish

  6. 然后确保为 Java 7 选择 JRE,一切就绪。

回答by Juan David Grisales Garzon

i had the same problem, but i have a solution:

我有同样的问题,但我有一个解决方案:

in your project file pom.xml replace:

在您的项目文件 pom.xml 中替换:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring</artifactId>
    <version>2.5.6</version>
</dependency>

for:

为了:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>

and say good bye to your problems!!

和你的问题说再见!!

This two dependecies are the replacement for the firts dependency in the most part.

这两个依赖在很大程度上替代了第一个依赖。

回答by MAFAIZ

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>

TO =>

到 =>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>

OR

或者

  1. Right click on project property.
  2. Go to/ Click on "Java Build Path".
  3. Select Libraries tab and double click on "JRE System Libry"
  4. On that pop up select the JDK 1.7 (If you dont have JDK 1.7 ,first install it, then select it from "Execution environment")
  1. 右键单击项目属性。
  2. 转到/单击“Java 构建路径”。
  3. 选择库选项卡并双击“JRE 系统库”
  4. 在该弹出窗口中选择 JDK 1.7(如果您没有 JDK 1.7,请先安装它,然后从“执行环境”中选择它)

Property

财产

回答by Dmitry

I have the similar problem. Old Spring MVC/Spring Faces application under spring 2.5.5 doesn't run on Java 8.

我有类似的问题。spring 2.5.5 下的旧 Spring MVC/Spring Faces 应用程序不能在 Java 8 上运行。

I spent few days trying to find solution because we need to run Java 8.

我花了几天时间试图找到解决方案,因为我们需要运行 Java 8。

First idea was: to upgrade complete Spring package up to 4.1.6. I used Maven. Problem of this method that after that it is necessary to rework nearly the whole project. It is because for example in Spring 4 were removed JSF implementation and some special taglibs where completely removed, like <sf:..>. And were some more major and minor problems with configuration, adapters, handlers, mappings....

第一个想法是:将完整的 Spring 包升级到 4.1.6。我使用了 Maven。这种方法的问题是,此后几乎需要对整个项目进行返工。这是因为例如在 Spring 4 中删除了 JSF 实现和一些完全删除的特殊标签库,如<sf:..>. 还有一些主要和次要的配置、适配器、处理程序、映射问题......

Second approach was to replace Spring JARs partially.One by one. Again no success. Impossible to replace any jar without touching dependencies.

第二种方法是部分替换 Spring JAR。一个接一个。再次没有成功。不可能在不涉及依赖项的情况下替换任何 jar。

I believe that after few weeks or monthes of struggling I can get success on both approaches. But have no so much time. And I gave up. My solution is:

我相信经过几周或几个月的努力,我可以在这两种方法上都取得成功。但是没有那么多时间。而我放弃了。我的解决办法是:

I found source file JdkVersion.java from org.springframework.core package. http://www.java2s.com/Code/Jar/o/Downloadorgspringframeworkcoresources300releasejar.htm. I created org.springframework.core package in my project with only one class JdkVersion. After that made simple change of the code to check Java 8 version. Smth like that:

我从 org.springframework.core 包中找到了源文件 JdkVersion.java。http://www.java2s.com/Code/Jar/o/Downloadorgspringframeworkcoresources300releasejar.htm。我在我的项目中创建了 org.springframework.core 包,只有一个类 JdkVersion。之后对代码进行了简单的更改以检查 Java 8 版本。像这样:

public static final int JAVA_18 = 5;

...

...

        javaVersion = System.getProperty("java.version");
    // version String should look like "1.4.2_10"
    if (javaVersion.contains("1.8.")) {
        majorJavaVersion = JAVA_18;         
        System.out.println("JAVA_VERSION: "+javaVersion);

    } else if (javaVersion.contains("1.7.")) {
        majorJavaVersion = JAVA_17;
    }

Even this code change is not really necessary, only just for fun.It is because this source came from Spring 3.0.0 package where Spring guys already changed the Java version check. Versions higher than 7 are not considered as old java.

甚至这个代码更改也不是真正必要的,只是为了好玩。这是因为这个源来自 Spring 3.0.0 包,其中 Spring 人已经更改了 Java 版本检查。高于 7 的版本不被视为旧的 java。

Now application starts properly. It call JdkVersion class from my project instead of jar.

现在应用程序正常启动。它从我的项目而不是 jar 中调用 JdkVersion 类。

Works so far! Thanks for all from this thread who gave this idea.

到目前为止有效!感谢所有从这个线程中提出这个想法的人。

Thanks

谢谢

回答by vallismortis

I need to support Spring 2.5.5 on Java 8, so I used the approach from this answerto provide a future-proof drop-in replacement for JdkVersion.classwith as few side-effects as possible (nobody else posted a complete class, and I didn't want to hiHyman the other answer). There is no need to check for Java 8, simply default to Java 7, which is the highest version the class cared about:

我需要在 Java 8 上支持 Spring 2.5.5,所以我使用了这个答案中的方法来提供一个面向未来的直接替代品,JdkVersion.class副作用尽可能少(其他人没有发布完整的类,我没有不想劫持另一个答案)。无需检查 Java 8,只需默认为 Java 7,这是该类关心的最高版本:

Extract the jar file:

解压jar文件:

mkdir spring
cd spring
jar xvf ../spring.jar

Check the Spring version in META-INF/MANIFEST.MF(you should see something like version=2.5.5). Look up the appropriate versionof JdkVersion.javaand use that as a starting point (the example below is for Spring 2.5.5 and you don't want to change any method signatures from the version you're working with).

检查 Spring 版本META-INF/MANIFEST.MF(您应该看到类似version=2.5.5)。中查找相应版本JdkVersion.java和使用,作为一个起点(下面的例子是对Spring 2.5.5和你不想改变从您正在使用的版本的任何方法签名)。

Check the major and minor version of the JdkVersion.classfile:

检查文件的主要和次要版本JdkVersion.class

javap -verbose org/springframework/core/JdkVersion.class

javap -verbose org/springframework/core/JdkVersion.class

We see that the class was original compiled as target 48.0(looking this up, we find that is Java 1.4):

我们看到该类最初被编译为目标48.0查找这个,我们发现它是 Java 1.4):

Classfile /tmp/spring/org/springframework/core/JdkVersion.class
  Last modified Jun 23, 2008; size 1286 bytes
  MD5 checksum 98e6ea08ce9186c50cc71d14465fc3dd
  Compiled from "JdkVersion.java"
public abstract class org.springframework.core.JdkVersion
  minor version: 0
  major version: 48
  flags: ACC_PUBLIC, ACC_SUPER, ACC_ABSTRACT
Constant pool:
...

Create org/springframework/core/JdkVersion.javawith the following content:

org/springframework/core/JdkVersion.java使用以下内容创建:

package org.springframework.core;

public abstract class JdkVersion
{
    public static final int JAVA_13 = 0;
    public static final int JAVA_14 = 1;
    public static final int JAVA_15 = 2;
    public static final int JAVA_16 = 3;
    public static final int JAVA_17 = 4;

    private static final String javaVersion;
    private static final int majorJavaVersion;

    static
    {
        javaVersion = System.getProperty("java.version");

        if (javaVersion.indexOf("1.7.") != -1)
        {
            majorJavaVersion = JAVA_17;
        } else if (javaVersion.indexOf("1.6.") != -1) {
            majorJavaVersion = JAVA_16;
        } else if (javaVersion.indexOf("1.5.") != -1) {
            majorJavaVersion = JAVA_15;
        } else if (javaVersion.indexOf("1.4.") != -1) { // new
            majorJavaVersion = JAVA_14;                 // new
        } else {
            majorJavaVersion = JAVA_17;                 // changed from JAVA_14
        }
    }

    public static String getJavaVersion()
    {
        return javaVersion;
    }

    public static int getMajorJavaVersion()
    {
        return majorJavaVersion;
    }

    public static boolean isAtLeastJava14()
    {
        return true;
    }

    public static boolean isAtLeastJava15()
    {
        return getMajorJavaVersion() >= JAVA_15;
    }

    public static boolean isAtLeastJava16()
    {
        return getMajorJavaVersion() >= JAVA_16;
    }
}

Then compile the new class as Java 1.4:

然后将新类编译为 Java 1.4:

javac -source 1.4 org/springframework/core/JdkVersion.java

javac -source 1.4 org/springframework/core/JdkVersion.java

You can check the major.minor version again as above if needed.

如果需要,您可以像上面一样再次检查 major.minor 版本。

Create the modified jar file (without overwriting the original manifest):

创建修改后的 jar 文件(不覆盖原始清单):

jar Mcf ../spring-modified.jar *

jar Mcf ../spring-modified.jar *

Copy the modified jar file where needed (as spring.jaror as appropriate).

将修改后的 jar 文件复制到需要的地方(视情况spring.jar或视情况而定)。

回答by Amandeep Singh Bhatia

Migrate your spring version from 2.5 to >=3.2.3.

将您的 spring 版本从 2.5 迁移到 >=3.2.3。

For Spring migration you need to do following changes -

对于 Spring 迁移,您需要进行以下更改 -

1) In your pom.xml remove dependency for spring 2.5.6 and add dependency for spring new version.

1)在您的 pom.xml 中删除 spring 2.5.6 的依赖项并添加 spring 新版本的依赖项。

2) Update 'xsi:schemaLocation' in beans tag of applicationcontet.xml file of your project.

2) 更新项目 applicationcontet.xml 文件的 beans 标签中的“xsi:schemaLocation”。

e.g update http://www.springframework.org/schema/beans/spring-beans-2.5.xsdto http://www.springframework.org/schema/beans/spring-beans-3.2.xsdfor spring 3.2.3 version.

例如,更新http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/beans/spring-beans-3.2.xsd春天3.2.3版本。

3) Clean,build and re-deploy your project.

3) 清理、构建和重新部署您的项目。

回答by vikash singh

I was facing the same issue. But, after trying very hard, I found out that this is because of using Java 1.8. I changed it to 1.6 and it worked.

我面临着同样的问题。但是,经过一番努力,我发现这是因为使用了Java 1.8。我将其更改为 1.6 并且有效。

回答by Erik Lievaart

I created a fully automatic version of this answer. Please read that post first for the subtleties he addresses.

我创建了这个答案的全自动版本。请先阅读那篇文章,了解他所提到的微妙之处。

  • create an ant build file with the contents below the bullets
  • copy spring.jarnext to the build.xmlfile
  • run the patch target
  • you are done, the file is patched
  • 使用项目符号下方的内容创建一个 ant 构建文件
  • 复制spring.jarbuild.xml文件旁边
  • 运行补丁目标
  • 大功告成,文件已打好补丁

build.xmlfile contents:

build.xml文件内容:

<project name="spring-patch" default="patch">
    <target name="patch">
        <mkdir dir="src/org/springframework/core"/>
        <get dest="src/org/springframework/core/JdkVersion.java"
                src="http://grepcode.com/file_/repo1.maven.org/maven2/org.springframework/spring/2.5.5/org/springframework/core/JdkVersion.java/?v=source&amp;disposition=attachment" />
        <replace dir="src" value="majorJavaVersion = JAVA_17">
          <replacetoken>majorJavaVersion = JAVA_14</replacetoken>
        </replace>
        <javac srcdir="src" />
        <zip destfile="spring.jar" update="true">
            <fileset dir="src" includes="**/*.class" />
        </zip>
        <delete dir="src" />
    </target>
</project>

回答by Dennis van Beek

Even though this answer has been perfectly answered, in some cases it's not desired or possible to upgrade the Java version or the Spring version.

尽管这个答案已经得到了完美的回答,但在某些情况下,升级 Java 版本或 Spring 版本是不希望或不可能的。

In our case, we have to use JDK 8 to be able to communicate with Sonar (from Jenkins) and we don't want to change the spring-version, because we don't have the test capacity to test this upgrade.

在我们的例子中,我们必须使用 JDK 8 才能与 Sonar(来自 Jenkins)进行通信,我们不想更改 spring 版本,因为我们没有测试此升级的测试能力。

Our solution was to simply override the Java version while building, using

我们的解决方案是在构建时简单地覆盖 Java 版本,使用

-Djava.version=1.6.45