找不到SpringBoot存储库(Hibernate)

时间:2015-05-01 12:03:10

标签: hibernate jpa spring-boot

您好我正在使用spring-boot和hibernate创建简单的webapp。 application.properties 文件包含db conneciton的属性。 贝娄是我的档案:

应用环境

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> {}

CustomerController:

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

1 个答案:

答案 0 :(得分:0)

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