【问题标题】:spring - boot (error while creating beans ) - org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with namespring - 引导(创建 bean 时出错) - org.springframework.beans.factory.UnsatisfiedDependencyException:使用名称创建 bean 时出错
【发布时间】:2020-02-23 05:56:37
【问题描述】:

尝试使用 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 时出错:不满意 通过字段“服务”表达的依赖关系;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“customerService”的 bean 时出错:不满意 通过字段“repo”表达的依赖关系;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建名为“customerRepository”的bean:调用init 方法失败;嵌套异常是 java.lang.IllegalArgumentException: 无法为方法 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] 在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:397) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1429) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 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.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 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) ~[spring-context-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE] 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE] 在 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE] 在 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.java:1226) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]

【问题讨论】:

  • 之前尝试过。但还是一样。以下是 Failed to create query for method public abstract java.util.List com.ekart.fabfeet.service.CustomerRepository.getAllCustomerModels() 的错误日志!找不到类型 CustomerModel 的属性 getAllCustomerModels!

标签: java hibernate spring-boot jpa


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    在您的存储库中,您正在扩展 CrudRepository&lt;CustomerModel, Integer&gt;,但您的实体的 ID 列是字符串而不是整数。

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2017-08-20
        • 2017-07-15
        • 2021-09-27
        • 1970-01-01
        • 2019-01-22
        • 2021-11-17
        • 2021-08-25
        • 2021-08-13
        相关资源
        最近更新 更多