在为子接口的JpaRepository创建Bean作为empRepository时遇到问题

时间:2018-09-02 14:31:27

标签: java spring-mvc spring-boot

Find usages

说明:

com.sathya.service.EmployeeServiceImpl中的字段employeeRepository需要一个类型为'com.sathya.repository.EmployeeRepository'的bean。

操作:

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

我正在为以下代码获取上述错误:

example1.py

实体类别代码

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
***************************

application.yml

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 + "]";
}

}

控制器类

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

1 个答案:

答案 0 :(得分:0)

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

以下是有关编码约定的一些错误。

@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

如果在进行所有上述更改后仍无法正常工作,请告诉我。