尝试启动应用程序上下文时 ConfigurationPropertiesBinder java spring 出错

Error With ConfigurationPropertiesBinder java spring when trying to start application context

我有一个简单的应用程序,它无法加载并出现以下错误


应用程序启动失败


描述:

试图调用不存在的方法。尝试是从以下位置进行的:

org.springframework.boot.context.properties.ConfigurationPropertiesBinder.register(ConfigurationPropertiesBinder.java:202)

不存在以下方法:

'org.springframework.beans.factory.support.BeanDefinitionBuilder org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition(java.lang.Class, java.util.function.Supplier)'

调用方法的 class、org.springframework.boot.context.properties.ConfigurationPropertiesBinder 是从以下位置加载的:

jar:file:/C:/Users/Kedem/.m2/repository/org/springframework/boot/spring-boot/2.6.6/spring-boot-2.6.6.jar!/org/springframework/boot/context/properties/ConfigurationPropertiesBinder.class

被调用方法的 class、org.springframework.beans.factory.support.BeanDefinitionBuilder 可从以下位置获得:

jar:file:/C:/Users/Kedem/.m2/repository/org/springframework/spring-beans/5.3.1/spring-beans-5.3.1.jar!/org/springframework/beans/factory/support/BeanDefinitionBuilder.class

被调用方法的 class 层次结构是从以下位置加载的:

org.springframework.beans.factory.support.BeanDefinitionBuilder: file:/C:/Users/Kedem/.m2/repository/org/springframework/spring-beans/5.3.1/spring-beans-5.3.1.jar

操作:

更正应用程序的 class 路径,使其包含 classes org.springframework.boot.context.properties.ConfigurationPropertiesBinder and org.springframework.beans.factory.support.BeanDefinitionBuilder

的兼容版本

这是我的 pom 文件:

<?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>org.example</groupId>
    <artifactId>test1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/>
    </parent>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.telegram</groupId>
            <artifactId>telegrambots</artifactId>
            <version>6.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.6.6</version>
        </dependency>


    </dependencies>
</project>

这是我的主class

包 com.test1.api;

进口org.springframework.boot.SpringApplication; 导入 org.springframework.boot.autoconfigure.SpringBootApplication; 导入 org.springframework.boot.context.properties.EnableConfigurationProperties; 导入 org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = "com.test1")
public class MainClass {

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

这是我的application.properties

bot.username=12345
bot.token=abcdsadsadsasa

部分服务class

package com.test1.api;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;

    @Service
    public class Test1Api {
    
    
        @Autowired
        public Test1Api(@Value("${bot.username:12345}") String botUserName, @Value("${bot.token:dsadsadsadsa") String botToken) {
            try {
                TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);
                telegramBotsApi.registerBot(new Nadlan1Bot(botUserName, botToken));
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        }
    }

删除 spring-beans 依赖项。