创建名为“solverManager”的 bean 时出错,通过构造函数参数表示的依赖关系不满足
Posted
技术标签:
【中文标题】创建名为“solverManager”的 bean 时出错,通过构造函数参数表示的依赖关系不满足【英文标题】:Error creating bean with name 'solverManager', Unsatisfied dependency expressed through constructor parameter 【发布时间】:2021-12-10 13:01:02 【问题描述】:JHipster 是一个使用 Spring-boot 框架快速生成、开发和部署现代 Web 应用程序和微服务架构的开发平台。 Jhipster 有“直接去”的脚本来部署在谷歌云上。
Optaplanner 是一个帮助解决计划和调度问题的 AI 库。
我的目标是使用 Jhipster 快速开发一个平台,该平台借助 optaplanner 来解决特定问题,并最终将代码部署在 Google Kubernate 上。 我终于通过合并来自Optaplanner 和 Jhipster 的示例代码得出了一个解决方案。
如以下输出所示,它在我的本地机器上运行良好。安装 Jhipster 后:
jhipster
mvn package -Pprod jib:dockerBuild
java -jar target/jhipster-0.0.1-SNAPSHOT.jar
██╗ ██╗ ██╗ ████████╗ ███████╗ ██████╗ ████████╗ █████ ███╗ ███████╗ ██║ ██║ ██║ ╚══██╔══╝ ██╔═══██╗ ██╔════╝ ╚══██╔══╝ ██╔═══ ══╝ ██╔═══██╗ ██║ ████████║ ██║ ███████╔╝ ╚█████╗ ██║ ██████╗ ███████╔╝ ██╗ ██║ ██╔═══██║ ██║ ██╔════╝ ╚═══██╗ ██║ ██╔═══╝ ██╔══██ ║ ╚██████╔╝ ██║ ██║ ████████╗ ██║ ██████╔╝ ██║ ████████╗ ██║ ╚██╗ ╚═════╝╚═════╝╚═╝╚═╝╚═══════╝╚═══════╝╚═╝╚═════╝╚═╝╚═══════╝╚═╝
:: JHipster ???? :: Running Spring Boot 2.5.4 ::
:: https://www.jhipster.tech ::
23:14:22.340 INFO [main ] The following profiles are active: prod
23:14:25.183 INFO [main ] Web application configuration, using profiles: prod
23:14:25.184 INFO [main ] Web application fully configured
23:14:31.135 DEBUG [main ] Identified candidate component class: URL [jar:file:/home/parfait/Programming/javascript/Jhipster/target/jhipster-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/mycompany/myapp/domain/TimeTable.class]
23:14:31.145 DEBUG [main ] Identified candidate component class: URL [jar:file:/home/parfait/Programming/JavaScript/Jhipster/target/jhipster-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/mycompany/myapp/domain/Lesson.class]
23:14:35.403 INFO [main ] JBoss Threads version 3.1.0.Final
23:14:35.477 INFO [main ] Started JhipsterApp in 14.484 seconds (JVM running for 15.042)
23:14:35.479 INFO [main ]
----------------------------------------------------------
Application 'Jhipster' is running! Access URLs:
Local: http://localhost:8080/
External: http://127.0.1.1:8080/
Profile(s): [prod]
----------------------------------------------------------
但是当我执行脚本在 Google Kuberntes 上部署相同的代码时,我遇到了以下错误:
jhipster k8s
./mvnw clean compile jib:build
./mvnw -ntp -Pprod jib:build -Djib.to.image=gcr.io/jhipster17/jhipster
bash kubectl-apply.sh -f
Kubernetes 日志的输出:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'timeTableResource' defined in file
[/app/classes/com/mycompany/myapp/web/rest/TimeTableResource.class]: Unsatisfied dependency expressed through constructor parameter 1;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'solverManager' 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.api.solver.SolverManager]: Factory method 'solverManager'
threw exception; nested exception is java.lang.NullPointerException at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1354)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1204)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:338)
at com.mycompany.myapp.JhipsterApp.main(JhipsterApp.java:69) Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'solverManager' 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.api.solver.SolverManager]: Factory method 'solverManager' threw exception;
nested exception is java.lang.NullPointerException at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:638)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791) ...
17 common frames omitted Caused by: org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.optaplanner.core.api.solver.SolverManager]: Factory method 'solverManager' threw exception; nested exception is java.lang.NullPointerException at
org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ... 31 common frames omitted Caused by:
java.lang.NullPointerException: null at java.compiler@11.0.12/javax.tools.ToolProvider.lambda$matches$0(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method) at java.compiler@11.0.12/javax.tools.ToolProvider.matches(Unknown Source)
at java.compiler@11.0.12/javax.tools.ToolProvider.getSystemTool(Unknown Source)
at java.compiler@11.0.12/javax.tools.ToolProvider.getSystemJavaCompiler(Unknown Source)
at org.kie.memorycompiler.jdknative.NativeJavaCompiler.compile(NativeJavaCompiler.java:73)
at org.kie.memorycompiler.AbstractJavaCompiler.compile(AbstractJavaCompiler.java:41)
at org.kie.memorycompiler.KieMemoryCompiler.compileNoLoad(KieMemoryCompiler.java:129)
at org.kie.memorycompiler.KieMemoryCompiler.compileNoLoad(KieMemoryCompiler.java:101)
at org.kie.memorycompiler.KieMemoryCompiler.compile(KieMemoryCompiler.java:53)
at org.kie.memorycompiler.KieMemoryCompiler.compile(KieMemoryCompiler.java:39)
at org.drools.ancompiler.KieBaseUpdaterANC.inMemoryUpdate(KieBaseUpdaterANC.java:66)
at org.drools.ancompiler.KieBaseUpdaterANC.run(KieBaseUpdaterANC.java:52)
at org.drools.ancompiler.KieBaseUpdaterANC.generateAndSetInMemoryANC(KieBaseUpdaterANC.java:99)
at org.optaplanner.core.impl.score.director.stream.DroolsConstraintStreamScoreDirectorFactory.buildKieBaseFromModel(DroolsConstraintStreamScoreDirectorFactory.java:115)
at org.optaplanner.core.impl.score.director.stream.DroolsConstraintStreamScoreDirectorFactory.buildKieBase(DroolsConstraintStreamScoreDirectorFactory.java:104)
at org.optaplanner.core.impl.score.director.stream.DroolsConstraintStreamScoreDirectorFactory.<init>(DroolsConstraintStreamScoreDirectorFactory.java:71)
at org.optaplanner.core.impl.score.director.ScoreDirectorFactoryFactory.buildConstraintStreamScoreDirectorFactory(ScoreDirectorFactoryFactory.java:223)
at org.optaplanner.core.impl.score.director.ScoreDirectorFactoryFactory.decideMultipleScoreDirectorFactories(ScoreDirectorFactoryFactory.java:103)
at org.optaplanner.core.impl.score.director.ScoreDirectorFactoryFactory.buildScoreDirectorFactory(ScoreDirectorFactoryFactory.java:71)
at org.optaplanner.core.impl.solver.DefaultSolverFactory.buildScoreDirectorFactory(DefaultSolverFactory.java:116)
at org.optaplanner.core.impl.solver.DefaultSolverFactory.buildSolver(DefaultSolverFactory.java:83)
at org.optaplanner.core.impl.solver.DefaultSolverManager.validateSolverFactory(DefaultSolverManager.java:68)
at org.optaplanner.core.impl.solver.DefaultSolverManager.<init>(DefaultSolverManager.java:57)
at org.optaplanner.core.api.solver.SolverManager.create(SolverManager.java:111)
at org.optaplanner.spring.boot.autoconfigure.OptaPlannerAutoConfiguration.solverManager(OptaPlannerAutoConfiguration.java:98)
at org.optaplanner.spring.boot.autoconfigure.OptaPlannerAutoConfiguration$$EnhancerBySpringCGLIB$$2e4db969.CGLIB$solverManager$3(<generated>)
at org.optaplanner.spring.boot.autoconfigure.OptaPlannerAutoConfiguration$$EnhancerBySpringCGLIB$$2e4db969$$FastClassBySpringCGLIB$$277961ad.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
at org.optaplanner.spring.boot.autoconfigure.OptaPlannerAutoConfiguration$$EnhancerBySpringCGLIB$$2e4db969.solverManager(<generated>)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ... 32 common frames omitted
这是 SolverManager bean 注入的代码,基于 Jhipster 示例代码:
@RestController
@RequestMapping("/api")
public class TimeTableResource
private TimeTableRepository timeTableRepository;
private SolverManager<TimeTable, Long> solverManager;
public TimeTableResource(TimeTableRepository timeTableRepository, SolverManager<TimeTable, Long> solverManager)
this.timeTableRepository = timeTableRepository;
this.solverManager = solverManager;
/**
* @code POST /solve
*/
@PostMapping("/solve")
public void solve(TimeTableRepository timeTableRepository, SolverManager<TimeTable, Long> solverManager) throws URISyntaxException
log.debug("REST request to solve ");
solverManager.solveAndListen(TimeTableRepository.SINGLETON_TIME_TABLE_ID, timeTableRepository::findById, timeTableRepository::save);
完整代码可以在here找到。 我不明白为什么在 gke 上部署时会发生这种“不满足的依赖”,而在本地机器上没有这样的错误,在 gce 上也没有。
谁能帮我解决这个问题?
【问题讨论】:
里面隐藏了一个空指针异常。 你可以尝试在k8s本地运行,问题会更容易调试。 @tgdavies,本地没有问题 即使在容器中? 【参考方案1】:有一个NullPointerException
潜伏在堆栈跟踪中:
java.lang.NullPointerException: null at java.compiler@11.0.12/javax.tools.ToolProvider.lambda$matches$0(Unknown Source)
以前从未见过此异常,我可以做出一个有根据的猜测:可能是您的机器上没有 Java 编译器吗?请安装完整的 JDK(带有javac
和java
的JDK),这个问题可能会消失。
原因是约束流——为了为你的约束生成代码——需要将一些 Java 类编译成字节码。没有javac
,这是不可能的。
【讨论】:
以上是关于创建名为“solverManager”的 bean 时出错,通过构造函数参数表示的依赖关系不满足的主要内容,如果未能解决你的问题,请参考以下文章
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“entityManagerFactory”的bean时出错
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“batchConfiguration”的bean时出错
org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名为“sessionFactory”的bean时出错
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“messageSender”的bean时出错