错误:操作:考虑定义类型" package"的bean。在您的配置中

时间:2018-04-27 09:35:28

标签: spring spring-boot

我得到以下错误:

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

Description:

Field employeeDAO in com.example.demo.controller.EmployeeController required a bean of type 'com.example.demo.dao.EmployeeDAO' that could not be found.


Action:

Consider defining a bean of type 'com.example.demo.dao.EmployeeDAO' in your configuration.

我的文件夹结构:

enter image description here

以下是文件:

1

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories


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

2

package com.example.demo.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.dao.EmployeeDAO;
import com.example.demo.model.Employee;

@RestController
@RequestMapping("/company")
public class EmployeeController {
    @Autowired
    EmployeeDAO employeeDAO;


    @PostMapping ("/employee")
    public Employee createEmployee(@Valid @RequestBody Employee emp){
        return employeeDAO.save(emp);
    }


    @GetMapping ("/findAll")
    public List<Employee> findAll(){
        return employeeDAO.findAll();
    }


    @GetMapping("/employees/{id}")
    public ResponseEntity<Employee> getEmployeeById(@PathVariable(value = "id") Long empid){
        Employee emp = employeeDAO.findOne(empid);
        if (emp==null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok().body(emp);
    }


    @PutMapping("/employees/{id}")
    public ResponseEntity<Employee> updateEmployee(@PathVariable(value = "id") Long empid, @Valid @RequestBody Employee empDetails){
        Employee emp = employeeDAO.findOne(empid);
        if (emp==null){
            return ResponseEntity.notFound().build();
        }
        emp.setName(empDetails.getName());
        emp.setDesignation(empDetails.getExpertise());
        emp.setExpertise(empDetails.getExpertise());

        Employee updateEmployee = employeeDAO.save(emp);
        return ResponseEntity.ok().body(updateEmployee);
    }


    @DeleteMapping("/employees/{id}")
    public ResponseEntity<Employee> deleteEmployee(@PathVariable(value = "id") Long empid){
        Employee emp = employeeDAO.findOne(empid);
        if (emp==null){
            return ResponseEntity.notFound().build();
        }
        employeeDAO.delete(emp);
        return ResponseEntity.ok().build();
    }
}

3

package com.example.demo.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.example.demo.model.Employee;
import com.example.demo.repository.EmployeeRepository;

public class EmployeeDAO {

    @Autowired

    EmployeeRepository employeeRepository;

    public Employee save(Employee emp){
        return employeeRepository.save(emp);
    }

    public List<Employee> findAll(){
        return employeeRepository.findAll();
    }

    public Employee findOne(Long empid){
        return employeeRepository.findOne(empid);
    }

    public void delete(Employee emp){
        employeeRepository.delete(emp);
    }

}

4

package com.example.demo.model;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotBlank;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

@Entity
@Table(name = "Employees")
@EntityListeners(AuditingEntityListener.class)

public class Employee {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long Id;

    @NotBlank
    private String name;

    @NotBlank
    private String designation;

    @NotBlank
    private String expertise;

    @NotBlank
    @Temporal(TemporalType.TIMESTAMP)
    @LastModifiedDate
    private Date createdAt;

    public long getId() {
        return Id;
    }

    public void setId(long id) {
        Id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesignation() {
        return designation;
    }

    public void setDesignation(String designation) {
        this.designation = designation;
    }

    public String getExpertise() {
        return expertise;
    }

    public void setExpertise(String expertise) {
        this.expertise = expertise;
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }
}

5

package com.example.demo.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.example.demo.model.Employee;

public interface EmployeeRepository extends JpaRepository<Employee, Long> { 

    public Employee findOne(Long empid);

}

1 个答案:

答案 0 :(得分:0)

从错误中,我知道尚未创建员工dao bean以自动装入控制器。

通过分析您的员工dao类,您忘记使用@Repository注释,因为上下文扫描尚未创建哪个bean。

使用@Repository 注释empoyeedao类。它应该可以解决你的问题。