如何自动装配 spring-data CrudRepository

How to autowire spring-data CrudRepository

一点细节 运行 下面给出的代码时我遇到了一些问题。我收到以下异常。当我尝试 [CrudRepository for Spring Data][1] 的示例代码时。

我有一个接口:

package com.joydeep.springboot; 

import org.springframework.data.repository.CrudRepository; 
import com.joydeep.springboot.vo.Message; 

public interface Test1 extends CrudRepository<Message, String> { }

VO Class:

package com.joydeep.springboot.vo;

import java.util.Date;    
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Message {
    @Id
    private String id;
    private String text;
    private String author;
    private Date created;

    public Message() {
        super();
    }
    public Message(String id, String text, String author, Date created) {
        super();
        this.id = id;
        this.text = text;
        this.author = author;
        this.created = created;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public Date getCreated() {
        return created;
    }
    public void setCreated(Date created) {
        this.created = created;
    }
    }

控制器class:

package com.joydeep.springboot; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController;  

@RestController 
public class HelloCntrl { 

   @Autowired 
   Test1 test; 

   @RequestMapping("/hello")
   public String sayHi(){
      return "Hi"; 
   } 

}

初始化器class:

package com.joydeep.springboot; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

@SpringBootApplication 
public class CourseAPIStarter { 
   public static void main(String[] args) {
      SpringApplication.run(CourseAPIStarter.class); 
   } 
}

pom.xml:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>io.springboot</groupId>
    <artifactId>rest-course-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Rest service course API</name>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <scope>runtime</scope>
        </dependency>

    </dependencies>
</project>

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'helloCntrl': Unsatisfied dependency expressed through field 'test'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.joydeep.springboot.Test1' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations {@org.springframework.beans.factory.annotation.Autowired(required=true)}

我指的例子来自这个link.

https://spring.io/guides/gs/accessing-data-jpa/

Spring 找不到 Test1 bean,因为你忘了用 @Repository 注释它。您只能自动装配 Spring 托管 类(控制器、组件、存储库、服务 ecc)。 试试这个:

@Repository
public interface Test1 extends CrudRepository<Message, String> { }

您不需要在 Test1 上添加 @Repository 注释。将这 3 个注释放在 CourseAPIStarter class 上,您的应用程序将 运行 像热刀一样。

@ComponentScan(basePackages={"com.joydeep.springboot"}) 

@EntityScan(basePackages={"com.joydeep.springboot.vo"}) 

@EnableJpaRepositories(basePackages={"com.joydeep.springboot"}) 

@ComponentScan 将扫描您的对象,并将它们注册到 Spring。

@EnableJpaRepositories 将使所有 Spring JPA 存储库都可以在您的应用程序中使用。

@EntityScan:当您在模型对象 class 上添加 @Entity 注释时,Spring 将假定名称为 [=] 的 table 18=]。因此,要将此 class 与 Spring 注册为 Entity,您需要使用此注释。