Ошибка: Действие: рассмотрите возможность определения bean-компонента типа «пакет» в вашей конфигурации - PullRequest
0 голосов
/ 27 апреля 2018

получаю следующую ошибку:

***************************
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 голосов
/ 27 апреля 2018

Из ошибки я узнаю, что сотрудник Дао Бин не был создан для автоматического подключения к контроллеру.

Анализируя ваш дао-класс сотрудника, вы забыли аннотировать с помощью @Repository, из-за чего компонент не был создан при сканировании контекста.

Аннотируйте класс empoyeedao с помощью @ Repository . Это должно решить вашу проблему.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...