一尘不染

添加spring-security- oauth2后无法实例化接口org.springframework.context.ApplicationListener

spring-boot

在我添加spring-security-oauth2到pom.xml之后:

<dependency>
    <groupId>org.springframework.security.oauth</groupId>
    <artifactId>spring-security-oauth2</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>

我开始得到

Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationListener : org.springframework.boot.context.logging.ClasspathLoggingApplicationListener
    at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:439)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:418)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:409)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:268)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:247)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1245)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1233)

运行我的@SpringBootApplication。请注意,我使用的是Spring Boot
2.0.0.M5,我认为这可能是问题的根源(WRT版本)。

这是我正在使用的整个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>
        <artifactId>server</artifactId>
        <groupId>mahlzeit</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>api</artifactId>

    <properties>
        <spring.boot.version>2.0.0.M5</spring.boot.version>
    </properties>

    <dependencies>

        <!-- Spring Framework Boot -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${spring.boot.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- Spring Framework -->

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-jwt</artifactId>
            <version>1.0.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4.1208-jdbc42-atlassian-hosted</version>
        </dependency>

        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

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

    <repositories>
        <!-- Required since this is currently using Spring RC version -->
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

我怀疑这可能与版本冲突有关-多数关于stackoverflow的答案就是这样-但我无法确定我在这里遇到的实际问题。


Spring Boot 2.0.0.M5 的发行说明说明


Spring Security的 OAuth 2.0支持 功能OAuth项目正在迁移到核心Spring Security。OAuth
2.0客户端支持已被添加,其他功能将在适当时候迁移。

如果您依赖尚未迁移的Spring Security
OAuth功能,则需要org.springframework.security.oauth:spring-security- oauth2手动添加和配置。如果只需要OAuth 2.0客户端支持,则可以使用Spring Boot
2.0提供的自动配置。我们还将继续支持Spring Boot 1.5,以便较旧的应用程序可以继续使用它,直到提供升级路径为止。


阅读 1134

收藏
2020-05-30

共1个答案

一尘不染

解决了示例pom的问题后,我现在可以看到故障的完整堆栈跟踪:

Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationListener : org.springframework.boot.context.logging.ClasspathLoggingApplicationListener
    at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:439)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:418)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:409)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:268)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:247)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1245)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1233)
    at mahlzeit.api.Application.main(Application.java:14)
Caused by: java.lang.NoClassDefFoundError: org/springframework/context/event/GenericApplicationListener
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:255)
    at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:431)
    ... 7 more
Caused by: java.lang.ClassNotFoundException: org.springframework.context.event.GenericApplicationListener
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 21 more

问题的根本原因org.springframework.context.event.GenericApplicationListener是不在类路径上。此类是spring- context模块的一部分,并且是Spring Framework 4.2中的新增类。

查看pom,您没有使用Boot的依赖项管理(通过导入spring-boot-dependenciesBom或通过spring-boot- starter- parent用作项目的父项来进行管理。这意味着未管理任何传递性依赖项的版本。这在您的项目中引起了问题正在使用4.0.9.RELEASE通过spring- context传递spring-security-oauth2

我强烈建议您使用Spring Boot的依赖项管理。如果您不想这样做或由于某些未说明的原因而不能这样做,则必须手动确保所有传递依赖项都支持版本。

2020-05-30