[英]Spring @Autowired not working - expected at least 1 bean which qualifies as autowire candidate
I am using Java 14 with Spring Boot 2.4.0-SNAPSHOT.我正在使用 Java 14 和 Spring 启动 2.4.0-SNAPSHOT。
I have a resource, within which I try to Autowire a approvalRequestService
.我有一个资源,我尝试在其中自动装配一个approvalRequestService
。
ApprovalRequestResource.java ApprovalRequestResource.java
@RestController
public class ApprovalRequestResource {
@Autowired
ApprovalRequestService approvalRequestService;
Which is an interface:这是一个接口:
ApprovalRequestService.java ApprovalRequestService.java
@Service
public interface ApprovalRequestService {
List<ApprovalRequestDTO> getApprovalRequests(String token);
}
And has the following implementation:并具有以下实现:
ApprovalRequestServiceImpl.java ApprovalRequestServiceImpl.java
public class ApprovalRequestServiceImpl implements ApprovalRequestService {
@Autowired
ApprovalRequestDAO approvalRequestDAO;
@Autowired
CompanyContactService companyContactService;
@Autowired
JwtTokenUtil jwtTokenUtil;
@Override
public List<ApprovalRequestDTO> getApprovalRequests(String jwtToken) {
When I start Spring Boot, I get the following error:当我启动 Spring Boot 时,出现以下错误:
APPLICATION FAILED TO START应用程序无法启动
Description:描述:
Field approvalRequestService in com.nexct.approvalservice.resources.ApprovalRequestResource required a bean of type 'com.nexct.approvalservice.service.ApprovalRequestService' that could not be found. com.nexct.approvalservice.resources.ApprovalRequestResource 中的字段approvalRequestService 需要找不到类型为“com.nexct.approvalservice.service.ApprovalRequestService”的bean。
The injection point has the following annotations:注入点有以下注解:
- @org.springframework.beans.factory.annotation.Autowired(required=true) @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:行动:
Consider defining a bean of type 'com.nexct.approvalservice.service.ApprovalRequestService' in your configuration.考虑在您的配置中定义“com.nexct.approvalservice.service.ApprovalRequestService”类型的 bean。
Disconnected from the target VM, address: '127.0.0.1:55876', transport: 'socket'与目标 VM 断开连接,地址:'127.0.0.1:55876',传输:'socket'
Process finished with exit code 0进程以退出代码 0 结束
and和
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'approvalRequestResource': Unsatisfied dependency expressed through field 'approvalRequestService';上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“approvalRequestResource”的bean时出错:通过字段“approvalRequestService”表示的依赖关系不满足; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.nexct.approvalservice.service.ApprovalRequestService' available: expected at least 1 bean which qualifies as autowire candidate.嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“com.nexct.approvalservice.service.ApprovalRequestService”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。 Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
Question问题
How have I wired this incorrectly?我如何错误地接线?
It appears that it does not like the following in ApprovalRequestResource.java:它似乎不喜欢 ApprovalRequestResource.java 中的以下内容:
@Autowired
ApprovalRequestService approvalRequestService;
You have to put the @Service annotation the implementation not the interface您必须将 @Service 注释放在实现而不是接口
public interface ApprovalRequestService {
List<ApprovalRequestDTO> getApprovalRequests(String token);
}
@Service
public class ApprovalRequestServiceImpl implements ApprovalRequestService {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.