简体   繁体   中英

Consider defining a bean of type 'package' in your configuration

I am trying to make a simple REST service in Spring Boot. Everything was working fine until I used CrudRepository. Now I am getting this error-

***APPLICATION FAILED TO START


Description:

Field er in company.springBoot.io.Employee.EmployeeService required a bean of type 'company.springBoot.io.Employee.EmployeeRepo' that could not be found.

Action:

Consider defining a bean of type 'company.springBoot.io.Employee.EmployeeRepo' in your configuration.***

Here is my code-

Controller-

@RestController
public class EmployeeController {


@Autowired
EmployeeService employeeService;

@GetMapping("/employees")
public List<Employee> getAllEmployees(){
    return employeeService.getAllEmployees();
}
@GetMapping("/employees/{id}")
public Employee getEmployee(@PathVariable int id) {
    return employeeService.getEmployee(id);
}

@PostMapping("/employees")
public String addEmployee(@RequestBody Employee e) {
    employeeService.addEmployee(e );
    return "Employee Records were added successfully";
}

@PutMapping("/employees/{id}")
public String updateEmployee(@RequestBody Employee e, @PathVariable int id) {
    return employeeService.updateEmployee(e, id);
    }

@DeleteMapping("/employees/{id}")
public @ResponseBody String deleteEmployee(@PathVariable int id) {
    return employeeService.deleteEmployee(id);

}

}

Service-

@Autowired
EmployeeRepo er;

public List<Employee> getAllEmployees() {


    List<Employee> list= new ArrayList<>();
    er.findAll()
    .forEach(list::add);
    return list;
        //return list;
}


public void addEmployee(Employee e) {
    // TODO Auto-generated method stub

    er.save(e);

}


public String updateEmployee(Employee e, int id) {
    // TODO Auto-generated method stub
er.save(e);
    String s= "The Employee with id  has been replaced by Employee with id "+e.getId();
    return s;
}


public String deleteEmployee(int id) {
    //  Auto-generated method stub
    er.deleteById(id);
    return "Employee withh id "+id+" has been removed from the company";
}


public Employee getEmployee(int id) {
    return er.findById(id).get();
}


}

Interface For CrudRepository

package company.springBoot.io.Employee;

import org.springframework.data.repository.CrudRepository;

public interface EmployeeRepo extends CrudRepository<Employee, Integer>{

}

Root

package company.springBoot.io.EmployeeRest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages= 
{"company.springBoot.io.Employee"})
public class EmployeeRestApplication {

public static void main(String[] args) {
    SpringApplication.run(EmployeeRestApplication.class, args);
}
}

Can someone help me with this?

-------------------------------------- Update -------------------------------------- I changed the folder structure like this- 在此处输入图片说明

and Now I am getting this error-

在此处输入图片说明

You are missing annnotation

package company.springBoot.io.Employee;

import org.springframework.data.repository.CrudRepository;
@Repository /// here is the trick
public interface EmployeeRepo extends CrudRepository<Employee, Integer>{

}

Spring didnt scan that interface because of missing annotation thus failed to create such dependency.

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