java - 在为子接口的 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..");
}
}
解决方案
您应该始终遵循编码约定最佳实践。
以下是您在编码约定方面犯的一些错误。
@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;
如果在完成上述所有更改后也不起作用,请告诉我。
推荐阅读
- javascript - 尝试将卡片元素添加到 reactjs 应用程序
- python - 将多个元组转换为嵌套字典
- python - 连续更改字符串日期类型
- python - 如何在 MongoDB 查询中编写 3 个条件?
- git - 无法克隆私有存储库 github:身份验证失败
- javascript - 以正确的方向显示图像 - Blazor 服务器端
- python - 实例化后从另一个类中创建一个动态类
- c++ - VS Code + MacPorts,查找头文件
- arrays - Array name 不就是一个常量指针,不能用在指针运算中,那么这个程序为什么会起作用呢?
- python - How to count distinct (x,y) pairs from a set of 3 dimensional tuples