Java Spring 启动抛出异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21788134/
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-13 10:39:03  来源:igfitidea点击:

Spring Starting throws an exception

javaspringmavenspring-boot

提问by Bula

I'm trying to build the simplest app in spring and I have the following code for my single controller

我正在尝试在 spring 中构建最简单的应用程序,并且我的单个控制器有以下代码

    package User;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by Bula on 14/02/14.
 */
@Controller
public class UsersController {

    @RequestMapping("/user")
    public String index()
    {
        return "user_index";
    }

}

Here is the Main.java. The one that boots everything:

这是 Main.java。启动一切的那个:

  package main;

import javafx.application.Application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;


@ComponentScan
@EnableAutoConfiguration
public class Main {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Also this is the pom.xml

这也是 pom.xml

  <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>blog</groupId>
    <artifactId>blog</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.5.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>1.0.0.BUILD-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>1.0.0.BUILD-SNAPSHOT</version>
        </dependency>
    </dependencies>


</project>

I have updated the maven hence all the libraries technically should be there. Here is the error that gets thrown(I tried googling it but nothing showed up which came any close to what I faced ):

我已经更新了 Maven,因此技术上所有的库都应该在那里。这是抛出的错误(我尝试使用谷歌搜索它,但没有出现任何与我面临的情况接近的错误):

    Exception in thread "main" java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotationAwareOrderComparator.sort(Ljava/util/List;)V
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:371)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:343)
    at org.springframework.boot.SpringApplication.initialize(SpringApplication.java:221)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:197)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:877)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:866)
    at main.Main.main(Main.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Process finished with exit code 1

My user_index.html does exist however I don't think the error is in any kind linked with it since it only has a html tag. Here is an image with all the libraries that I have: enter image description here

我的 user_index.html 确实存在,但是我不认为该错误与它有任何关联,因为它只有一个 html 标签。这是我拥有的所有库的图像: 在此处输入图片说明

采纳答案by Virmundi

So I created a maven project in Eclipse from your pom. What I've found is that you're getting the org.springframework.core.annotation.AnnotationAwareOrderComparator in two jars. The 2.5.2 spring jar and the 4.0. Get rid of the 2.5.2 jar. That's old. The new jar has the same class with the method that the new code is calling.

所以我从你的 pom 在 Eclipse 中创建了一个 maven 项目。我发现你在两个罐子里得到 org.springframework.core.annotation.AnnotationAwareOrderComparator 。2.5.2 spring jar 和 4.0. 摆脱 2.5.2 jar。那是旧的。新 jar 与新代码调用的方法具有相同的类。

The issue, at the heart, is that you're double loading the core jars.

问题的核心是您要双重加载核心罐。

回答by Miguel O

The answer above (Virmundi) is correct in the sense says two classes are "colliding". In my case I was defining my maven dependencies fine

上面的答案 (Virmundi) 是正确的,因为两个类正在“碰撞”。在我的情况下,我定义了我的 Maven 依赖关系很好

<!-- Provided Spring dependencies spring-beans, spring-context, spring-core, spring-tx -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>

but had an old Spring jar in my classpath in which version indeed didn't had this method

但是在我的类路径中有一个旧的 Spring jar,其中的版本确实没有这个方法

java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotationAwareOrderComparator.sort(Ljava/util/List;)V