首页 > 解决方案 > 在为子接口的 JpaRepository 创建 bean 作为 empRepository 时遇到问题

问题描述

2018-09-03 00:05:17.266  WARN 14948 --- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeController': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeServiceImpl': Unsatisfied dependency expressed through field 'employeeRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.sathya.repository.EmployeeRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2018-09-03 00:05:17.267  INFO 14948 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2018-09-03 00:05:17.285  INFO 14948 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-09-03 00:05:17.311  INFO 14948 --- [  restartedMain] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-09-03 00:05:17.559 ERROR 14948 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

描述:

com.sathya.service.EmployeeServiceImpl 中的字段 employeeRepository 需要找不到类型为“com.sathya.repository.EmployeeRepository”的 bean。

行动:

考虑在您的配置中定义“com.sathya.repository.EmployeeRepository”类型的 bean。

我对以下代码感到上述错误:

package com.sathya.repository;

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

import com.sathya.entity.Employee;

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Integer>{
}

实体类代码

package com.sathya.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="emp-tab")
public class Employee {

@Id
private Integer empid;
@Column(length=15)
private String ename;
private Integer salary;
private Double deptno;
public Integer getEmpid() {
    return empid;
}
public void setEmpid(Integer empid) {
    this.empid = empid;
}
public String getEname() {
    return ename;
}
public void setEname(String ename) {
    this.ename = ename;
}
public Integer getSalary() {
    return salary;
}
public void setSalary(Integer salary) {
    this.salary = salary;
}
public Double getDeptno() {
    return deptno;
}
public void setDeptno(Double deptno) {
    this.deptno = deptno;
}
@Override
public String toString() {
    return "Employee [empid=" + empid + ", ename=" + ename + ", salary=" + 
salary + ", deptno=" + deptno + "]";
}

}

应用程序.yml

server:
   port: 4343
   context-path: /Ems
spring:
   datasource:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/world
      username: root
      password: root
   jpa:
      show-sql: true
      hibernate:
      ddl-auto: update
   mvc:
      view:
         prefix: /
         suffix: .jsp

服务类和接口

package com.sathya.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sathya.entity.Employee;
import com.sathya.repository.EmployeeRepository;
@Service
public class EmployeeServiceImpl implements IEmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
@Override
public boolean insertEmployee(Employee e) {
    emp.save(e);
    return false;
}
}
-----------
package com.sathya.service;

import com.sathya.entity.Employee;

public interface IEmployeeService {
public boolean insertEmployee(Employee e);

}

控制器类

package com.sathya.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

 import com.sathya.entity.Employee;
 import com.sathya.service.IEmployeeService;

 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;

 @Controller
 public class EmployeeController {

@Autowired
private IEmployeeService service;
@GetMapping("/addEmployee")
public ModelAndView getEmployee()
{
    return new ModelAndView("AddEmployee");
}
@PostMapping("/insertEmployee")
public ModelAndView insertEmployee(HttpServletRequest request)
{
    Employee e=new Employee();
    e.setEmpid(Integer.parseInt(request.getParameter("empid")));
    e.setEname(request.getParameter("ename"));
    e.setSalary(Integer.parseInt(request.getParameter("salary")));
    e.setDeptno(Double.parseDouble(request.getParameter("deptno")));
    boolean flag=service.insertEmployee(e);
    if(flag==true)
        return new ModelAndView("index","message","Employee "+e.getEname()+"  
  Added to database...Successfully");
    else
        return new ModelAndView("index","message","Employee "+e.getEname()+"  
  is not Added to database..");
    }
 }

标签: javaspring-mvcspring-boot

解决方案


您应该始终遵循编码约定最佳实践。

以下是您在编码约定方面犯的一些错误。

@GetMapping("/AddEmployee") //change this to addEmployee

@Qualifier("empRepository") //no need of this line

private empRepository emp; //change this with below line of code rename the class

private EmployeeRepository employeeRepository;

另请参阅此https://docs.spring.io/spring-data/jpa/docs/1.6.0.RELEASE/reference/html/jpa.repositories.html#d0e1599

如果在完成上述所有更改后也不起作用,请告诉我。


推荐阅读