How to solve the error "Field service in com.xx.xxx.xxxx required a bean of type 'com.aa.bb.cc' that could not be found."

When runung a SpringBoot demo, I  got a error as following:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field service in com.hy.empcloud.EmpControl required a bean of type 'com.hy.empcloud.EmpService' 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.hy.empcloud.EmpService' in your configuration.

After I add a annotation "@Service" to the class com.hy.empCloud.EmpService, the error was solved.

package com.hy.empcloud;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

// To provide service of employees
@Service
public class EmpService {
    private List<Emp> emps;
    
    public EmpService(){
        emps=new ArrayList<Emp>();
        
        emps.add(new Emp(1,"Andy",41));
        emps.add(new Emp(2,"刘德华",42));
        emps.add(new Emp(3,"Bill",43));
        emps.add(new Emp(4,"比尔",44));
    }
    
    public List<Emp> getAll(){
        return emps;
    }
    
    public Emp find(long id) throws Exception{
        
        for(Emp e:emps) {
            if(e.getId()==id) {
                return e;
            }
        }
    
        throw new Exception("No such employee whose's id="+id);
    }
}

Here is the demo.

--END--

2019年8月24日14点24分

原文地址:https://www.cnblogs.com/heyang78/p/11404568.html