spring - Spring找不到bean
问题描述
我收到以下错误:
Field delegate in package.rest.api.MyApiController required a bean of type 'package.rest.api.MyApiDelegate' that could not be found.
我尝试将注释 @ComponentScan("package.rest") 添加到主类,但没有帮助。
package package;
@SpringBootApplication
@Import(SpecificationConf.class)
//@ComponentScan("package.rest")
public class MyApp extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
package package.rest.api;
@Controller
@RequestMapping(...)
public class MyApiController implements MyApi {
@Autowired
@Qualifier("myService")
private MyApiDelegate delegate;
}
package package.rest;
@Service("myService")
public class MyApiDelegateImpl implements MyApiDelegate {
...
}
package package.rest.api;
public interface MyApiDelegate {
...
}
由于应用程序上下文的错误配置或不适当的 Maven 配置文件,是否会发生该问题?我试图将类移动到同一个包,但它也没有帮助。
解决方案
鉴于您正在构建 JAR 文件而不是 WAR 文件,请尝试SpringBootServletInitializer
从主类中删除,如下所示:
package package;
@SpringBootApplication
@Import(SpecificationConf.class)
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
推荐阅读
- vuejs2 - vue-google-login 插件可以在 localhost 下工作吗?
- javascript - 使用 NPM 包显示“错误:无法在模块外使用 import 语句”
- size - 抽样理论中的“Large N”是什么意思?
- c# - 如何在多对多关系中的多个表中进行更新?
- amazon-web-services - POSTMAN 在调用 API 时出现内部服务器错误
- html - 使用数据表将所有 `thead th` 保持在粘性位置
- javascript - 为什么我的通知 JS 不起作用?网站.js
- r - 如何在 r 中的 3D 散点图中添加椭圆/多边形和图例?
- ibm-midrange - 如何从命令行在 IBMi 中编译(和链接)COBOL 程序?
- html - 如何避免从检查工具修改 HTML 代码?