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);
}
}

最新更新