线程“主”中的异常java.lang.IllegalArgumentException:无法实例化接口org.springframework.context.ApplicationListener

时间:2018-08-16 08:23:08

标签: java spring maven spring-mvc spring-boot

这是我第一次在堆栈上问。一开始我想注意到我的英语太糟糕了。我正在用Java编写程序,该程序必须读取文件并将内容发送到localhost:8083 / list,但这是模块之一。  我的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>

    <parent>
        <groupId>org.antek</groupId>
        <artifactId>spring-boot-multi-module</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>List</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我的父母pom.xml看起来

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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>org.antek</groupId>
    <artifactId>spring-boot-multi-module</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <modules>
        <module>List</module>
        <module>MainPage</module>
    </modules>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.yaml</groupId>
                <artifactId>snakeyaml</artifactId>
                <version>1.18</version>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>5.0.3</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.assertj</groupId>
                <artifactId>assertj-core</artifactId>
                <version>3.10.0</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>4.0.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

我的Appliaction.java看起来像这样

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

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

我的控制器

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@EnableAutoConfiguration
public class ListController {
    @RequestMapping("/list")
    @ResponseBody
    class Lista {
        MainList list = new MainList();
    }
}

最后确定我的程序

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


public class MainList {
    public static int zwracanie(String[] args) throws IOException {
            String filePath = "C:\\Users\\Antoni\\Desktop\\Lista.txt";
            int number = 0;
            BufferedReader fileReader = null;

            try {
                fileReader = new BufferedReader(new FileReader(filePath));
                String numberAsString = fileReader.readLine();
                number = Integer.parseInt(numberAsString);
            } finally {
                if (fileReader != null) {
                    fileReader.close();
                }
            }
            return number;
        }
    }

我正在IntelliJ Idea中工作,当我尝试编译该文件时出现问题

  

线程“主”中的异常java.lang.IllegalArgumentException:无法实例化接口org.springframework.context.ApplicationListener:org.springframework.boot.context.logging.ClasspathLoggingApplicationListener       在org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:448)处       在org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:427)       在org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:418)       在org.springframework.boot.SpringApplication。(SpringApplication.java:268)       在org.springframework.boot.SpringApplication。(SpringApplication.java:247)       在org.springframework.boot.SpringApplication.run(SpringApplication.java:1255)       在org.springframework.boot.SpringApplication.run(SpringApplication.java:1243)       在Application.main(Application.java:8)   造成原因:java.lang.NoClassDefFoundError:org / springframework / context / event / GenericApplicationListener       在java.base / java.lang.ClassLoader.defineClass1(本机方法)       在java.base / java.lang.ClassLoader.defineClass(ClassLoader.java:1009)       在java.base / java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)       在java.base / jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:801)       在java.base / jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:699)       在java.base / jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:622)       在java.base / jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:580)       在java.base / jdk.internal.loader.ClassLoaders $ AppClassLoader.loadClass(ClassLoaders.java:190)       在java.base / java.lang.ClassLoader.loadClass(ClassLoader.java:499)       在org.springframework.util.ClassUtils.forName(ClassUtils.java:274)       在org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:440)       ...另外7个   引起原因:java.lang.ClassNotFoundException:org.springframework.context.event.GenericApplicationListener       在java.base / jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)       在java.base / jdk.internal.loader.ClassLoaders $ AppClassLoader.loadClass(ClassLoaders.java:190)       在java.base / java.lang.ClassLoader.loadClass(ClassLoader.java:499)       ...另外18个

1 个答案:

答案 0 :(得分:1)

删除依赖项

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-framework-bom</artifactId>
</dependency>

您使用版本4.0.3.RELEASE添加了该版本,它与Spring Boot管理的版本产生版本冲突。