繁体   English   中英

考虑在配置中定义“包”类型的bean

[英]Consider defining a bean of type 'package' in your configuration

我正在尝试在Spring Boot中创建一个简单的REST服务。 在使用CrudRepository之前,一切工作正常。 现在我得到这个错误-

***申请未能开始


描述:

company.springBoot.io.Employee.EmployeeService中的字段er需要一个类型为'company.springBoot.io.Employee.EmployeeRepo'的bean。

行动:

考虑在您的配置中定义类型为'company.springBoot.io.Employee.EmployeeRepo'的bean。***

这是我的代码-

控制器-

@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);

}

}

服务-

@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();
}


}

CrudRepository的接口

package company.springBoot.io.Employee;

import org.springframework.data.repository.CrudRepository;

public interface EmployeeRepo extends CrudRepository<Employee, Integer>{

}

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);
}
}

有人可以帮我弄这个吗?

-------------------------------------- 更新 ----------- ---------------------------我这样更改了文件夹结构- 在此处输入图片说明

现在我得到这个错误-

在此处输入图片说明

您缺少注释

package company.springBoot.io.Employee;

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

}

由于缺少注释,Spring没有扫描该接口,因此无法创建此类依赖项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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