spring java.lang.ClassNotFoundException:应用程序上下文初始化器

mjqavswn  于 2022-12-17  发布在  Spring
关注(0)|答案(3)|浏览(149)

嗨,我有这个例外,当我试图启动 Spring Boot :

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContextInitializer
        at org.springframework.boot.SpringApplication.initialize(SpringApplication.java:262)
        at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:238)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1187)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1176)
        at org.myapp.test.subscription.webservice.Application.main(Application.java:15)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContextInitializer
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 5 more

下面是我的maven配置:

<!-- SPRING -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.4.7.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3.redhat-1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
...
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.4.7.RELEASE</version>
            </plugin>
...

知道为什么吗?我应该添加另一个依赖项还是我使用了错误的版本?会不会是与另一个依赖项冲突?

mbskvtky

mbskvtky1#

@darren-forsythe确实是POM配置:

<dependencyManagement>
     <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.5.7.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

现在起作用了谢谢!

bfnvny8b

bfnvny8b2#

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

它的工作与上述版本以及罚款

ljsrvy3e

ljsrvy3e3#

你问的
Spring应用程序引导时出现ClassNotFoundException
ClassNotFoundException是一个已检查异常,当应用程序尝试通过类的全限定名加载类,但在类路径上找不到其定义时会发生,实际上,这主要发生在尝试使用Class.forName()ClassLoader.loadClass().calls加载类时,在本例中,这发生在IOC上下文的引导过程中。

Dependencies(Jar) hell是最可能的情况

你可能有一些不同版本的透明依赖项,你需要排除或使用合适的plugins来避免这种异常,它也可能发生在运行时,所以最佳实践是检查版本和复制透明依赖项,每当你添加新的maven工件和依赖项到你的pom.xml文件。

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-core</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

因此,您可能不会怀疑,如果我们删除排除项,并以示意图的方式面对此异常,则在上述依赖项的情况下,它将类似于this

相关问题