Repository 接口上的 ApplicationRunner NoSuchBeanException

ApplicationRunner NoSuchBeanException on Repository interface

我定义了这个模型class:

package test.model;

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

@Entity
public class Property {  
  @Id 
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private long id;

  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  private String streetName;

  public String getStreetName() {
    return streetName;
  }

  public void setStreetName(String streetName) {
    this.streetName = streetName;
  }
}

我创建了一个存储库 class 以便我可以对数据库进行操作:

package test.repository;

import test.model.Property;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface PropertyRepository extends CrudRepository<Property,Long> {
}

接下来我有一个申请运行ner class:

package test.cron;

import test.model.Property;
import test.repository.PropertyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;

@ComponentScan("test.repository")
@Service
public class MyRunner implements ApplicationRunner {
  @Autowired
  PropertyRepository propertyRepository;

  public void run(ApplicationArguments args) {
    System.out.println("In MyRunner.run()");
    Property prop = new Property();
    prop.setStreetName("Main street");
    propertyRepository.save(prop);
  }
}

这是我的主要应用程序,它使用了@EnableJpaRepositories 注释:

package test.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@EnableJpaRepositories
@ComponentScan({"test.cron","test.repository"})
@SpringBootApplication
public class TestApplication extends SpringBootServletInitializer {
  /** Main method */
  public static void main(String[] args) {
    SpringApplication.run(TestApplication.class, args);
  }
}

当我 运行 应用程序时,我收到此错误消息:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field propertyRepository in test.cron.MyRunner required a bean of type 'test.repository.PropertyRepository' that could not be found.

The injection point has the following annotations:
        - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'test.repository.PropertyRepository' in your configuration.

根据我正在阅读的内容,spring 应该创建存储库的实例。

我错过了什么?

尝试在主 class 中添加 @EnableJpaRepositories 注释。 无论如何,spring 初始化 所有这一切都是一个创业公司。如果您的存储库未被识别为 bean,则意味着 spring 尚未将其封装。因此,您需要添加一个 @ComponentScan 来告诉 spring 显式扫描您的存储库 class.

执行以下操作将解决问题

删除 @EnableJpaRepositories @ComponentScan({"test.cron","test.repository"}),它不需要 spring 引导会处理,如果您的项目构建不是 war 文件,也删除 SpringBootServletInitializer

将您的 TestApplication 移动到顶级包,即测试,您的应用程序运行器将工作。