简体   繁体   中英

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.

I have a resource, within which I try to Autowire a approvalRequestService .

ApprovalRequestResource.java

@RestController
public class ApprovalRequestResource {

    @Autowired
    ApprovalRequestService approvalRequestService;

Which is an interface:

ApprovalRequestService.java

@Service
public interface ApprovalRequestService {

    List<ApprovalRequestDTO> getApprovalRequests(String token);
}

And has the following implementation:

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:

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.

The injection point has the following annotations:

  • @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'com.nexct.approvalservice.service.ApprovalRequestService' in your configuration.

Disconnected from the target VM, address: '127.0.0.1:55876', transport: 'socket'

Process finished with exit code 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'; 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. Dependency annotations: {@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:

@Autowired
ApprovalRequestService approvalRequestService;

You have to put the @Service annotation the implementation not the interface

public interface ApprovalRequestService {

    List<ApprovalRequestDTO> getApprovalRequests(String token);
}

@Service
public class ApprovalRequestServiceImpl implements ApprovalRequestService {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM