当前位置: 首页 > 知识库问答 >
问题:

考虑在您的配置中定义一个类型为'com.face.ImageRepository'的bean?

夏侯航
2023-03-14

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

主要:

@SpringBootApplication
public class Main

{

public static void main(String[] args)
{

    SpringApplication.run(Main.class,args);

}

}
@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.

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

共有1个答案

巫马昆杰
2023-03-14

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

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

 }
 类似资料: