【问题标题】:Consider defining a bean of type 'com.face.ImageRepository' in your configuration?考虑在您的配置中定义“com.face.ImageRepository”类型的 bean?
【发布时间】:2018-08-27 02:32:07
【问题描述】:

我有一个 Java Spring 启动项目,我正在其中创建一个发布请求。代码如下所示:

主要:

@SpringBootApplication
public class Main

{

public static void main(String[] args)
{

    SpringApplication.run(Main.class,args);

}

}

Java 豆:

@Data
@Entity
public class Image {

private @Id @GeneratedValue Long id;
private String imageNo;
private String name;

private Image(){}

public Image(String imageNo, String name){
    this.imageNo = imageNo;
    this.name = name;

 }

}

存储库:

public interface ImageRepository extends CrudRepository<Image, Long> {

 }

数据库加载器:

@Component
public class DatabaseLoader implements CommandLineRunner {

private final ImageRepository repository;

@Autowired
public DatabaseLoader(ImageRepository repository) {
    this.repository = repository;
}

@Override
public void run(String... strings) throws Exception {
    this.repository.save(new Image("1", "Baggins"));
}
}

但是,当我运行该项目时,我收到以下错误:

    Description:

Parameter 0 of constructor in com.face.DatabaseLoader required a bean of type 'com.face.ImageRepository' that could not be found.

Action:

Consider defining a bean of type 'com.face.ImageRepository' in your configuration.

感谢您对此的任何帮助! 非常感谢,

【问题讨论】:

标签: java spring spring-boot


【解决方案1】:

您的 spring 容器不会扫描图像存储库,因为它没有注释。要解决此问题,您应该使用 @Repository 注释来注释 ImageRepository,如下所示 -

@Repository
public interface ImageRepository extends CrudRepository<Image, Long> {

 }

【讨论】:

  • 如果存储库位于主类下面的包中,则不需要此功能。 Spring Boot 将自动扫描存储库接口。
猜你喜欢
  • 2021-09-22
  • 2017-03-19
  • 1970-01-01
  • 1970-01-01
  • 2017-05-30
  • 2018-06-22
  • 1970-01-01
  • 2021-05-30
  • 2021-06-02
相关资源
最近更新 更多