繁体   English   中英

spring - 启动(创建 bean 时出错) - org.springframework.beans.factory.UnsatisfiedDependencyException:使用名称创建 bean 时出错

[英]spring - boot (error while creating beans ) - org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name

尝试使用 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:创建名为“customerController”的bean时出错:通过字段“service”表示的依赖关系不满足; 嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“customerService”的 bean 时出错:通过字段“repo”表示不满足的依赖关系; 嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“customerRepository”的 bean 时出错:调用 init 方法失败; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.ekart.fabfeet.service.CustomerRepository.getAllCustomers()! 没有找到类型 CustomerModel 的属性 getAllCustomers!

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] 在 org.srin gframework.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] 在 org.springframework.beans.factory.support.AbstractBeanFacto ry.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.Z93F725A07423FE1C889F448B33D22) 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 ] 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spri ng-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] 在 org.springframework.boot.SpringApplication.run(SpringApplication.Z93F725A0742 3FE1C889F448B33D21F46Z: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() 方法,文档在这里

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

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

在您的存储库中,您正在扩展CrudRepository<CustomerModel, Integer> ,但您的实体的 ID 列是字符串,而不是 integer。

这就是它无法为您匹配存储库的原因。 将其修复为字符串,它应该可以正常工作

显然他正在尝试实现QueryMethods ,在这种情况下方法签名不正确, QueryMethods是 model ,其中查询是根据实体属性的名称和方法名称中支持的关键字构建的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM