Spring Boot抱怨OptaPlanner

问题描述

我有一个小的OptaPlanner应用程序,如果没有Spring Boot,它可以很好地运行。我基本上重新调整了云平衡opta应用的用途。我还包括了所有Spring Boot依赖项。 POM父级是spring-boot-starter-parent。而且我有optaplanner-spring-boot-starter作为依赖项。

通过此设置,我仍然可以毫无问题地运行opta应用。然后,我在项目中添加了Spring Boot应用程序。该应用程序如下:

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

不幸的是,当我运行此春季启动应用程序时,出现以下错误异常:

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 Boot应用程序位于同一软件包中。即使在constraints.drl文件中,我也有以下声明:

package org.optaplanner.examples;
  dialect "java"

所以我有点困惑。以下是完整的例外。我必须注意,我具有随云平衡示例附带的求解器: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,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$0(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,nor a constraints.drl resource.

解决方法

在错误消息中滚动到右侧。春季启动日志是隐藏错误消息的第一部分:

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有一点偏见()?