Spring:ApplicationStartingEvent 不能转换为 ApplicationPreparedEvent for OptaPlanner Examination App

标签 spring spring-boot module loader optaplanner

我是新手,我尝试启动一个链接到 Optaplanner 的 Spring 应用程序,它将解决问题并将考试安排在时间表上。 我解决了缺少 jars 的各种问题,并在 main 上启动了该应用程序。 但是,它给出了错误:

Exception in thread "main" java.lang.ClassCastException: class org.springframework.boot.context.event.ApplicationStartingEvent cannot be cast to class org.springframework.boot.context.event.ApplicationPreparedEvent (org.springframework.boot.context.event.ApplicationStartingEvent and org.springframework.boot.context.event.ApplicationPreparedEvent are in unnamed module of loader 'app')

这是 TimeTable 类:

package models;

import java.util.List;
import org.optaplanner.core.api.domain.solution.PlanningEntityCollectionProperty;
import org.optaplanner.core.api.domain.solution.PlanningScore;
import org.optaplanner.core.api.domain.solution.PlanningSolution;
import org.optaplanner.core.api.domain.solution.ProblemFactCollectionProperty;
import org.optaplanner.core.api.domain.valuerange.ValueRangeProvider;
import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore;

@PlanningSolution
public class Timetable {

    @ValueRangeProvider(id = "PeriodeRange")
    @ProblemFactCollectionProperty
    public List<Periode> periodeList;

    @ValueRangeProvider(id = "SalleRange")
    @ProblemFactCollectionProperty
    public List<Salle> salleList;

    @PlanningEntityCollectionProperty
    public List<Examen> examenList;

    @PlanningScore
    public HardSoftScore score;
    
    public void TimeTable(List<Periode> periodeList, List<Salle> roomList,
            List<Examen> examenList) {
        this.periodeList = periodeList;
        this.salleList = roomList;
        this.examenList = examenList;
    }

    public List<Periode> getperiodeList() {
        return periodeList;
    }

    public List<Salle> getsalleList() {
        return salleList;
    }

    public List<Examen> getexamenList() {
        return examenList;
    }

    public HardSoftScore getScore() {
        return score;
    }
}

定义求解器的类:

package models;
import java.util.UUID;


import java.util.concurrent.ExecutionException;

import modeles.Timetable;
import org.optaplanner.core.api.solver.SolverJob;
import org.optaplanner.core.api.solver.SolverManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/Timetable")
public class TimetableController {
    @Autowired
    public SolverManager<Timetable, UUID> solverManager;

    @PostMapping("/solve")
    public Timetable solve(@RequestBody Timetable problem) {
        UUID problemId = UUID.randomUUID();
        // Submit the problem to start solving
        SolverJob<Timetable, UUID> solverJob = solverManager.solve(problemId, problem);
        Timetable solution;
        try {
            // Wait until the solving ends
            solution = solverJob.getFinalBestSolution();
        } catch (InterruptedException | ExecutionException e) {
            throw new IllegalStateException("Solving failed.", e);
        }
        return solution;
    }
}

这里是主要的:

package models;

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

@SpringBootApplication
public class TimeTableSpringBootApp {

    public static void main(String[] args) {
        System.setProperty("spring.devtools.restart.enabled", "false");
        SpringApplication.run(TimeTableSpringBootApp.class, args);
    }
}

任何答案将不胜感激。 提前谢谢你。

最佳答案

我怀疑您的项目的依赖树以某种方式在其类路径中有两次 spring 类 ApplicationStartingEvent(来自不同的 jar),这会导致类转换异常。

尝试在您的项目和 optaplanner spring boot 学校时间表快速启动上运行 mvn dependency:tree。我怀疑您在依赖树中混合了 spring 版本。

关于Spring:ApplicationStartingEvent 不能转换为 ApplicationPreparedEvent for OptaPlanner Examination App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65531904/

相关文章:

postgresql - spring boot + hibernate 5 + java 8 + ZonedDateTime = ImplicitNamingStrategy 异常

python - 使用全局变量有可能在导入过程中改变模块行为吗?

java - 当我请求休息服务时,Jquery 不起作用(Spring Boot)

angular - 单实例 Angular 2+ 通知服务/错误处理程序

javascript - 为什么这个 javascript 对象在使用和不使用模块模式时表现不同?

java - 如何将 Spring Controller /模型中的错误传递到 View 文件

java - JPA 在 Spring 中到底是如何工作的?一些疑问

spring - 如何为多个测试类加载一次应用程序上下文

spring - 哪个框架,工具或插件要求使用硬编码的 'database.xsd'文件名?

java - Springboot 没有 [javax.sql.DataSource] 类型的限定 bean