首页 > 解决方案 > 为什么弹簧靴找不到我的豆子?

问题描述

我有以下错误:

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.

通常这是因为我忘记对服务或界面进行注释,但我整个上午都在寻找课程并且找不到任何丢失的注释..

此时的界面只是:

@Component
public interface ComposeCounterService {
CLASSX init(List<YYY> owners) throws JAXBException;
}

和实施服务如下,如果在这种情况下很重要,则包含 init() 方法。

@Service
public class ComposeCounterImpl implements ComposeCounterService {
/*** loots of code
}

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

它包含以下包扫描:

@SpringBootApplication
scanBasePackages = {"com.yyy.zzz.xxx")

我还尝试了一系列扫描,例如:

scanBasePackages = {"com.yyy.zzz.xxx", "com.yyy.zzz.xxx.service.composeXML"})

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

我很确定我在这里遗漏了一些东西,请发送帮助。

编辑:注入风格:

private final ComposeCounterService composeCounterService;

public ControlService(ComposeCounterService composeCounterService) {
    this.composeCounterService = composeCounterService;
}

标签: javaspringspring-bootannotationsspring-bean

解决方案


错误的导入是:

import org.jvnet.hk2.annotations.Service;

正确的是:

import org.springframework.stereotype.Service;

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


推荐阅读