【问题标题】:How to autowire spring-data CrudRepository如何自动装配 spring-data CrudRepository
【发布时间】:2017-01-10 07:54:50
【问题描述】:

一些细节 我在运行下面给出的代码时遇到了一些问题。我收到以下异常。当我尝试 [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 类:

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

控制器类:

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"; 
   } 

}

初始化器类:

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>

上下文初始化期间遇到异常 - 取消刷新尝试: org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“helloCntrl”的bean时出错:通过字段“test”表示不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“com.joydeep.springboot.Test1”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注解 {@org.springframework.beans.factory.annotation.Autowired(required=true)}

我所指的例子来自这个链接。

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

【问题讨论】:

  • 请正确格式化您的代码
  • 错误提示您在 Spring 容器中没有 Test1 类。
  • 只看它,你就有不满意的依赖关系。检查您的 pom.xml 或您正在使用的任何构建工具!
  • 请以正确的方式格式化您的代码,并提供有关您正在做什么的更多详细信息...
  • 添加了更多细节,格式现在更好了。

标签: spring spring-boot spring-data-jpa spring-bean


【解决方案1】:

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

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

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

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

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

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

@EntityScan:当您在模型对象类上添加@Entity 注释时,Spring 将假定存在名称为Message 的表。所以要在 Spring 中注册这个类为Entity,你需要使用这个注解。

【讨论】:

  • 为什么@SpringBootApplication 不够用?谢谢
  • @andrew0007 当您使用@SpringBootApplication 注释来注释您的应用程序类时,它会在内部启用@EnableAutoConfiguration@ComponentScan@ConfigurationPropertiesScan 等。现在,当包含@EnableAutoConfiguration 时,弹簧关系可以猜测应用程序行为并通过读取您添加的 jar 依赖项添加所需的配置,如果它发现在项目中添加了任何数据源,它将自动使用默认值配置这些注释。这意味着单独使用 @SpringBootApplication 将适用于这些注释。
【解决方案2】:

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

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

【讨论】:

  • 我在看link。代码说不需要@Repository。
  • 另外添加注释也无济于事,我仍然遇到同样的异常。
  • 通常在正常的 Spring 配置中(使用 xml 或注释),此错误意味着 bean 未正确注释或您的 web.xml 中没有包含组件扫描行。但是由于您使用的是 Spring Boot,因此这里唯一缺少的是 Message 类。请尝试发布它。其他课程对我来说似乎是正确的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-19
  • 2018-05-08
  • 1970-01-01
  • 2015-06-17
  • 2015-06-19
  • 1970-01-01
  • 2017-04-25
相关资源
最近更新 更多