为什么 spring 引导找不到我的 bean?
Why is spring boot not finding my beans?
我有以下错误:
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 文件位于服务包的上一层。在此 post.
中标记为 xxx
它包含以下包扫描:
@SpringBootApplication
scanBasePackages = {"com.yyy.zzz.xxx")
我也尝试过像这样的扫描数组:
scanBasePackages = {"com.yyy.zzz.xxx", "com.yyy.zzz.xxx.service.composeXML"})
并且在 .service 之后没有 composeXML
None 这些作品。
我很确定我在这里遗漏了一些东西,请发送帮助。
编辑:
注入样式:
private final ComposeCounterService composeCounterService;
public ControlService(ComposeCounterService composeCounterService) {
this.composeCounterService = composeCounterService;
}
您需要在 scanBasePackages
属性中添加包 com.yyy.zzz.xxx.service
,因为您的 ControlService
位于此包中。
试试这个,如果您遇到任何其他问题,请告诉我
--编辑
从您的 interface ComposeCounterService
中删除 @Component
(因为界面永远不会初始化)
现在将 bean 名称命名为您的服务 class 为:
@Service("composeCounterImpl")
public class ComposeCounterImpl implements ComposeCounterService {
/*** loots of code
}
现在将构造函数定义为:
@Autowired
public ControlService(@Qualifier("composeCounterImpl") ComposeCounterService composeCounterService) {
this.composeCounterService = composeCounterService;
}
P.S:确保组件扫描中所有包都可用
您是否在 ComposeCounterService 字段上使用了 @Autowired
?
如果是;也许可以尝试使用 @SpringBootApplication
上方的 @ComponentScan
希望这对您有所帮助。
干杯
我是个十足的白痴...
各位,请务必检查您的导入...
我什至忽略了这一点,只粘贴了无法解决问题的代码...
我对@service 注释的导入错误..这就是问题的根本原因。
只花了几个小时很生气的调试。
错误的导入是:
import org.jvnet.hk2.annotations.Service;
正确的是:
import org.springframework.stereotype.Service;
如果您只是让 IDE 建议导入并按 Enter 键,而不阅读它添加的是哪一个,这就是结果。
我有以下错误:
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 文件位于服务包的上一层。在此 post.
中标记为 xxx它包含以下包扫描:
@SpringBootApplication
scanBasePackages = {"com.yyy.zzz.xxx")
我也尝试过像这样的扫描数组:
scanBasePackages = {"com.yyy.zzz.xxx", "com.yyy.zzz.xxx.service.composeXML"})
并且在 .service 之后没有 composeXML None 这些作品。
我很确定我在这里遗漏了一些东西,请发送帮助。
编辑: 注入样式:
private final ComposeCounterService composeCounterService;
public ControlService(ComposeCounterService composeCounterService) {
this.composeCounterService = composeCounterService;
}
您需要在 scanBasePackages
属性中添加包 com.yyy.zzz.xxx.service
,因为您的 ControlService
位于此包中。
试试这个,如果您遇到任何其他问题,请告诉我
--编辑
从您的 interface ComposeCounterService
中删除 @Component
(因为界面永远不会初始化)
现在将 bean 名称命名为您的服务 class 为:
@Service("composeCounterImpl")
public class ComposeCounterImpl implements ComposeCounterService {
/*** loots of code
}
现在将构造函数定义为:
@Autowired
public ControlService(@Qualifier("composeCounterImpl") ComposeCounterService composeCounterService) {
this.composeCounterService = composeCounterService;
}
P.S:确保组件扫描中所有包都可用
您是否在 ComposeCounterService 字段上使用了 @Autowired
?
如果是;也许可以尝试使用 @SpringBootApplication
@ComponentScan
希望这对您有所帮助。
干杯
我是个十足的白痴...
各位,请务必检查您的导入... 我什至忽略了这一点,只粘贴了无法解决问题的代码...
我对@service 注释的导入错误..这就是问题的根本原因。 只花了几个小时很生气的调试。
错误的导入是:
import org.jvnet.hk2.annotations.Service;
正确的是:
import org.springframework.stereotype.Service;
如果您只是让 IDE 建议导入并按 Enter 键,而不阅读它添加的是哪一个,这就是结果。