为什么 Spring Boot 找不到我的 beans?

Clo*_*mez 2 java spring annotations spring-bean spring-boot

我有以下错误:

Parameter 0 of constructor in com.yyy.zzz.xxx.service.ControlService required a bean of type 'com.yyy.zzz.xxx.service.composeXML.ComposeCounterService' that could not be found.
Run Code Online (Sandbox Code Playgroud)

通常这是因为我忘记注释服务或接口,但我整个早上都在查看类,找不到任何丢失的注释。

此时的界面只是:

@Component
public interface ComposeCounterService {
CLASSX init(List<YYY> owners) throws JAXBException;
}
Run Code Online (Sandbox Code Playgroud)

实现服务如下,并且包含 init() 方法(如果在这种情况下很重要)。

@Service
public class ComposeCounterImpl implements ComposeCounterService {
/*** loots of code
}
Run Code Online (Sandbox Code Playgroud)

ApplicationConfig 文件位于服务包的上一级。在这篇文章中标记为xxx。

它包含以下包扫描:

@SpringBootApplication
scanBasePackages = {"com.yyy.zzz.xxx")
Run Code Online (Sandbox Code Playgroud)

我也尝试过扫描数组,例如:

scanBasePackages = {"com.yyy.zzz.xxx", "com.yyy.zzz.xxx.service.composeXML"})
Run Code Online (Sandbox Code Playgroud)

并且在 .service 之后没有 composeXML 这些都不起作用。

我很确定我在这里缺少一些东西,请发送帮助。

编辑:注入风格:

private final ComposeCounterService composeCounterService;

public ControlService(ComposeCounterService composeCounterService) {
    this.composeCounterService = composeCounterService;
}
Run Code Online (Sandbox Code Playgroud)

小智 6

错误的导入是:

import org.jvnet.hk2.annotations.Service;
Run Code Online (Sandbox Code Playgroud)

正确的是:

import org.springframework.stereotype.Service;
Run Code Online (Sandbox Code Playgroud)

如果您只是让 IDE 建议导入并按 Enter 键而不阅读它添加的内容,这就是结果。