Spring 启动抱怨 OptaPlanner

Spring Boot complaining about OptaPlanner

我有一个小型 OptaPlanner 应用程序,无需 Spring 启动即可正常运行。我基本上改变了云平衡 opta 应用程序的用途。我还包含了所有 Spring 引导依赖项。 POM 父级是 spring-boot-starter-parent。我有 optaplanner-spring-boot-starter 作为依赖。

使用此设置,我仍然可以 运行 opta 应用程序而不会出现问题。然后我在项目中添加了一个 Spring Boot app。应用如下:

package org.optaplanner.examples;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}
}

不幸的是,当我 运行 这个 spring 启动应用程序时,我得到以下错误异常:

Maybe your @ConstraintProvider annotated class  is not in a subpackage of your @SpringBootApplication annotated class's package.
Maybe move your constraint provider class to your application class's (sub)package.

我有一个 constraints.drl 文件,其中包含所有流口水规则。它位于 src/main/resources/org/optaplanner/examples 下;据我所见,它与 spring 启动应用程序位于同一个包中。即使在 constraints.drl 文件中,我也有以下声明:

package org.optaplanner.examples;
  dialect "java"

所以我有点困惑。下面是完整的 exception.I 必须注意我有云平衡示例附带的求解器:cloudBalancingSolverConfig.xml.

有什么建议吗?

2:27:47.538 [main        ] WARN  Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'solverManager' defined in class path resource [org/optaplanner/spring/boot/autoconfigure/OptaPlannerAutoConfiguration.class]: Unsatisfied dependency expressed through method 'solverManager' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'solverFactory' defined in class path resource [org/optaplanner/spring/boot/autoconfigure/OptaPlannerAutoConfiguration.class]: Unsatisfied dependency expressed through method 'solverFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'solverConfig' defined in class path resource [org/optaplanner/spring/boot/autoconfigure/OptaPlannerAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.optaplanner.core.config.solver.SolverConfig]: Factory method 'solverConfig' threw exception; nested exception is java.lang.IllegalStateException: No classes found that implement EasyScoreCalculator, ConstraintProvider or IncrementalScoreCalculator, nor a constraints.drl resource.
Maybe your @ConstraintProvider annotated class  is not in a subpackage of your @SpringBootApplication annotated class's package.
Maybe move your constraint provider class to your application class's (sub)package.
12:27:47.554 [main        ] ERROR Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'solverManager' defined in class path resource [org/optaplanner/spring/boot/autoconfigure/OptaPlannerAutoConfiguration.class]: Unsatisfied dependency expressed through method 'solverManager' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'solverFactory' defined in class path resource [org/optaplanner/spring/boot/autoconfigure/OptaPlannerAutoConfiguration.class]: Unsatisfied dependency expressed through method 'solverFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'solverConfig' defined in class path resource [org/optaplanner/spring/boot/autoconfigure/OptaPlannerAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.optaplanner.core.config.solver.SolverConfig]: Factory method 'solverConfig' threw exception; nested exception is java.lang.IllegalStateException: No classes found that implement EasyScoreCalculator, ConstraintProvider or IncrementalScoreCalculator, nor a constraints.drl resource.
Maybe your @ConstraintProvider annotated class  is not in a subpackage of your @SpringBootApplication annotated class's package.
Maybe move your constraint provider class to your application class's (sub)package.
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:797)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:538)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=14=](AbstractBeanFactory.java:324)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
    at org.optaplanner.examples.Application.main(Application.java:9)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'solverFactory' defined in class path resource [org/optaplanner/spring/boot/autoconfigure/OptaPlannerAutoConfiguration.class]: Unsatisfied dependency expressed through method 'solverFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'solverConfig' defined in class path resource [org/optaplanner/spring/boot/autoconfigure/OptaPlannerAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.optaplanner.core.config.solver.SolverConfig]: Factory method 'solverConfig' threw exception; nested exception is java.lang.IllegalStateException: No classes found that implement EasyScoreCalculator, ConstraintProvider or IncrementalScoreCalculator, nor a constraints.drl resource.

在错误消息中向右滚动。 spring 引导日志隐藏了错误消息的第一部分:

No classes found that implement EasyScoreCalculator, ConstraintProvider or IncrementalScoreCalculator, nor a constraints.drl resource.
Maybe your @ConstraintProvider annotated class  is not in a subpackage of your @SpringBootApplication annotated class's package.
Maybe move your constraint provider class to your application class's (sub)package.

这意味着它没有看到 constraints.drl。 如果你有 solverConfig.xml,它可能也看不到。

修复:将您的 fooSolverConfig.xml 移至 src/main/resources/solverConfig.xml 或使用 application.properties 说明它的位置。 该求解器配置说明了您的 scoreRules.drl 所在的位置。 您也可以删除 <scoreDrl> 行,然后将 fooScoreRules.drl 设为 src/main/resources/constraints.drl

显然错误消息有点(也?)偏向于 ConstraintStreams :)