java - spring - 启动(创建 beans 时出错) - org.springframework.beans.factory.UnsatisfiedDependencyException : Error creating bean with name

标签 java hibernate spring-boot jpa

尝试使用 jpa/hibernate 创建一个基本的 web 服务。但是 bean 没有被初始化。谁能帮我解决这个问题?

下面是我的 CustomerController.java :

@RestController
public class CustomerController {

    @Autowired
    CustomerService service;

    @SuppressWarnings("deprecation")
    @PostMapping(value = "/getCust", consumes=MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<CustomerModel> retriveCustomers(@RequestBody CustomerModel cust){
        System.out.println(cust); //just to see the object in console
        List<CustomerModel> resp = service.getCustomers();
        return resp;
    }
}

下面是我的 CustomerService.java:

@Service
public class CustomerService {

    @Autowired
    CustomerRepository repo;

    public List<CustomerModel> getCustomers() {
        List<CustomerModel> resp=repo.getAllCustomers();
        return resp;
    }


}

下面是我的 CustomerRepository.java:

@Repository
public interface CustomerRepository extends CrudRepository<CustomerModel, Integer>{

    List<CustomerModel> getAllCustomers();
}

下面是我的 CustomerModel.java:

@Entity
@Table(name="aliens")
public class CustomerModel {

    @Id
    @Column(name="customer_id")
    private String customerId;

    @Column(name="customer_name")
    private String customerName;

    @Column(name="customer_email")
    private String customerEmail;

    @Column(name="customer_phoneNum")
    private String customerPhoneNum;

    @Column(name="customer_password")
    private String customerPassword;


}

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerController': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerService': Unsatisfied dependency expressed through field 'repo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.ekart.fabfeet.service.CustomerRepository.getAllCustomers()! No property getAllCustomers found for type CustomerModel!

org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:397) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1429) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]

最佳答案

在 CrudRepository 上,您可以使用 findAll() 方法,文档 here

当列出对象时,我建议您从 PagingAndSortingRepository 扩展,在那里您将完成分页和排序的实现,这非常方便。

关于错误,可以找到正确的语法here (getAll 不存在,你应该使用 findAll)。

关于java - spring - 启动(创建 beans 时出错) - org.springframework.beans.factory.UnsatisfiedDependencyException : Error creating bean with name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58582939/

相关文章:

java - 应用程序卡在 "Configuring OrikaBean mapper"处。为什么?

java - 为什么服务器向客户端发送空白字符串?

Java - 空指针异常

java - 如何检查包含字符串的数组是否包含另一个字符串数组中的某些单词

java - Hibernate:将多对多映射到 Map

Android Ormlite - 使用条件 where 子句构建查询

java - Hibernate 中的三元(和 n 元)关系

java - 与依赖冲突 'com.squareup.okio: okio'

java - 使用Springboot部署REST服务: whitelabel error

java - 如何在 JUnit 测试中覆盖私有(private)方法