【问题标题】:SpringBoot repository not found (Hibernate)找不到 Spring Boot 存储库(休眠)
【发布时间】:2015-05-01 12:03:09
【问题描述】:

您好,我正在使用 spring-boot 和 hibernate 创建简单的 webapp。 application.properties 文件包含数据库连接的属性。 下面是我的文件:

应用上下文:

package hbwc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
    }
}

客户实体:

package hbwc.customer;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String firstName;
    private String lastName;

    protected Customer() {
    }

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return String.format(
                "Customer[id=%d, firstName='%s', lastName='%s']",
                id, firstName, lastName);
    }

 }

客户资料库:

package hbwc.customer;

import org.springframework.data.repository.CrudRepository;

public interface CustomerRepository extends CrudRepository<Customer, Long> {}

客户控制器:

package hbwc.customer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
public class CustomerController {

    @Autowired
    CustomerRepository repository;

    @RequestMapping("/customers")
        public String index() {
        List<Customer> all = (List<Customer>) repository.findAll();
        return "ok";
    }
}

我正在使用 gradlew 来构建和运行这个示例。但是我有问题 运行它(./gradlew run)。我遇到了异常,它告诉我找不到我的 CustomerRepository。 堆栈跟踪:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [hbwc.customer.CustomerRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 18 common frames omitted

请给我一些建议,我的代码有什么问题。

application.properties(项目根目录)

spring.datasource.url=jdbc:mysql://localhost/hbwc
spring.datasource.username=root
spring.datasource.password=baza1

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop

【问题讨论】:

  • 您需要配置一个数据源,并且您需要在您的类路径上安装一个 spring 数据的 impl(例如 spring-data-jpa)。见docs.spring.io/spring-boot/docs/current/reference/html/…
  • application.properties 文件中的属性不够?
  • 看看这里的例子,它看起来还不错spring.io/guides/gs/accessing-data-jpa。您可以尝试将“@Repository”添加到您的 CustomerRepository 接口或“@EnableJpaRepositories”到您的应用程序配置类。根据示例,这两者都不是必需的,但无论如何都要尝试。
  • 关于我上面的评论,请参阅以下内容,这表明 @EnableJpaRepositories 可能,如建议的那样,是缺失的部分:stackoverflow.com/questions/28443222/…

标签: hibernate jpa spring-boot


【解决方案1】:

您的存储库类上没有任何类型的组件注释。尝试添加@Repository 或@Component。然后您应该(假设启用了组件扫描)能够将存储库自动装配到您的控制器中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 2020-04-12
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    相关资源
    最近更新 更多