我有以下项目结构:
root project
|-- controller
|-- core
|-- widgets
|-- widgetA
|-- widgetB
除控制器外,这些项目都内置于jar中。然后将jar部署到类文件夹,控制器使用context.xml指定它们的位置。
这一切在部署期间都很有效(除了Tomcat的'redeploy'没有重新部署,我怀疑类加载器有问题,但卸载/部署工作正常)。但是,我们目前无法在STS中本地运行它,因为控制器需要系统无法找到的“小部件”项目中的bean。
当我将控制器项目作为Spring Boot App执行时,项目开始正常。它加载并发现'核心'项目中定义的bean。但出于某种原因,Spring不会扫描“小部件”项目。同样,当部署它时,Spring会很好地扫描它们(我假设因为context.xml定义了要加载的JarResources)。但我似乎无法弄清楚如何让STS“看到”该项目。
编辑:为清楚起见,这是我得到的错误:
May 22, 2018 2:32:00 PM org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'Controller': Unsatisfied dependency expressed through field 'widgetMap'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.Map<java.lang.String, com.mycompany.project.widgets.WidgetInterface>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
System is shutting down
May 22, 2018 2:32:00 PM org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor shutdown
INFO: Shutting down ExecutorService 'getAsyncExecutor'
May 22, 2018 2:32:00 PM org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service [Tomcat]
May 22, 2018 2:32:00 PM org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener logAutoConfigurationReport
INFO:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
May 22, 2018 2:32:00 PM org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter report
SEVERE:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field widgetMap in com.mycompany.project.controller.Controller required a bean of type 'com.mycompany.project.widgets.WidgetInterface' that could not be found.
Action:
Consider defining a bean of type 'com.mycompany.project.widgets.WidgetInterface' in your configuration.
答案 0 :(得分:1)
我解决了这个问题。
原来我没有将个别小部件添加到我的构建路径,而只是添加'小部件'类。
因为我使用的是@Autowired Map对象,所以我需要在构建路径上至少一个我的WidgetInterface实现。由于小部件实现项目不存在,因此无法正确实例化地图,并且错误输出。