我正在开发crud应用程序,在使用springboot时遇到了困难,它在启动时失败。这就是我得到的:
20764警告[main]——组织。springframework。上下文注释。AnnotationConfigApplicationContext:上下文初始化期间遇到异常-取消刷新尝试:org。springframework。豆。工厂UnsatifiedPendencyException:创建名为“patientServiceImpl”的bean时出错:通过方法“setPatientDAO”参数0表示的未满足的依赖关系;嵌套的异常是org。springframework。豆。工厂NoSuchBeanDefinitionException:没有类型为“testgroup”的合格bean。私人诊所。刀。PatientDAO'可用:至少需要1个符合autowire候选资格的bean。依赖项批注:{}
型号:
package testgroup.private_clinic.model;
import javax.persistence.*;
@Entity
@Table(name="Patients")
public class Patient {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;
@Column(name="patient_name")
String name;
@Column(name = "patient_surname")
String surname;
@Column(name = "patient_patronimic")
String patronimic;
@Column(name="adress")
String adress;
@Column(name = "status")
String status;
@Column(name="diagnosis")
String diagnosis;
//+getters and setters
控制器:
package testgroup.private_clinic.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import testgroup.private_clinic.model.Patient;
import testgroup.private_clinic.service.PatientService;
import java.util.List;
@RestController
public class PatientController {
PatientService patientService;
@Autowired
public void setPatientService(PatientService patientService){
this.patientService = patientService;
}
@RequestMapping(method = RequestMethod.GET)
public ModelAndView allPatients(){
List<Patient> patients = patientService.allPatients();
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("patients");
modelAndView.addObject("patientList", patients);
return modelAndView;
}
@RequestMapping(value= "/edit{id}", method = RequestMethod.GET)
public ModelAndView editPage(@PathVariable("id") int id){
Patient patient = patientService.getByID(id);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("editPage");
modelAndView.addObject("patient", patient);
return modelAndView;
}
@RequestMapping(value="/edit", method = RequestMethod.POST)
public ModelAndView editPatient(@ModelAttribute("patient") Patient patient){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("redirect:/");
patientService.edit(patient);
return modelAndView;
}
}
仓库:
package testgroup.private_clinic.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import testgroup.private_clinic.model.Patient;
import javax.transaction.Transactional;
import java.util.*;
@Repository
public class PatientDAOImpl implements PatientDAO {
SessionFactory sessionFactory;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}
@Override
@Transactional
public List<Patient> allPatients() {
Session session = sessionFactory.getCurrentSession();
return session.createQuery("from Patient").list();
}
@Override
@Transactional
public void add(Patient patient) {
Session session = sessionFactory.getCurrentSession();
session.persist(patient);
}
@Override
@Transactional
public void delete(Patient patient) {
Session session = sessionFactory.getCurrentSession();
session.delete(patient);
}
@Override
@Transactional
public void edit(Patient patient) {
Session session = sessionFactory.getCurrentSession();
session.update(patient);
}
@Override
@Transactional
public Patient getByID(int id) {
Session session = sessionFactory.getCurrentSession();
return session.get(Patient.class, id);
}
}
服务:
package testgroup.private_clinic.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import testgroup.private_clinic.model.Patient;
import testgroup.private_clinic.dao.PatientDAO;
import javax.transaction.Transactional;
import java.util.List;
@Service
public class PatientServiceImpl implements PatientService{
PatientDAO patientDAO;
@Autowired
public void setPatientDAO(PatientDAO patientDAO){
this.patientDAO = patientDAO;
}
@Transactional
@Override
public List<Patient> allPatients() {
return patientDAO.allPatients();
}
@Transactional
@Override
public void add(Patient patient) {
patientDAO.add(patient);
}
@Transactional
@Override
public void delete(Patient patient) {
patientDAO.delete(patient);
}
@Transactional
@Override
public void edit(Patient patient) {
patientDAO.edit(patient);
}
@Transactional
@Override
public Patient getByID(int id) {
return patientDAO.getByID(id);
}
}
主要类:
package testgroup.private_clinic.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootClass {
public static void main(String[] args){
SpringApplication.run(testgroup.private_clinic.service.SpringBootClass.class, args);
}
}
SpringBoot使用类路径组件扫描意味着,入口点类SpringBootClass将扫描其类路径中的所有bean,除非您将其配置为不扫描。
查看您的项目结构,SpringBootClass在testgroup.private_clinic.service包下,因此Spring Boot只会扫描这个包以查找bean,并且它只找到了患者服务Impl,但是在它将其注入应用程序上下文之前,它需要首先注入它的依赖患者DAO,这不是testgroup.private_clinic.service包的一部分,因此,解释了您的错误。
你有两个选项来解决这个问题:
>
将SpringBootClass移动到基本包testgroup。private_clinic-这将对该包及其子包(例如,服务、dao)下的所有组件进行Spring Boot扫描
在SpringBootClass上使用@ComponentScan,并在那里定义要扫描bean的基本包。
@ComponentScan(basPackages="testgroup.private_clinic")
谢谢,干杯!
我正在使用Spring Boot 2.2.4并尝试自定义执行器 以下是相关的课程
我想在xml JDBCTemolate上进行配置。 看起来是这样的 JDBCrepository: 控制器:
控制器: 型号: 存储库: web.xml
我尝试构建简单的Maven springMVC应用程序。当我试图将应用程序部署到我的服务器时,下面抛出了异常。 unsatisfiedDependencyException:创建com.phapp.comfiguration.webconfiguration中定义的名为“View Resolver”的bean时出错:通过方法“View Resolver”参数0表示未满足的依赖关系;嵌套异常是org
问题内容: 我正在尝试构建一个全新的Spring Framework 4.0项目,而没有所有神奇的东西,而只是简单地将它踢过去。 我在这里关注该教程:http : //spring.io/guides/tutorials/data/并取得了一些成功。我只是停留在这一点上。 当我运行此单元测试时,得到以下堆栈跟踪: 根据观察和研究,似乎是在告诉我有两个EntityManager类。第一个来自hibe