我有一个Java Spring启动项目,我正在创建一个post请求。代码如下所示:
主:
@SpringBootApplication
public class Main
{
public static void main(String[] args)
{
SpringApplication.run(Main.class,args);
}
}
Java bean:
@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> {
}
DatabaseLoader:
@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.
感谢任何帮助! 非常感谢,
答案 0 :(得分:0)
您的spring容器不会扫描Image存储库,因为它没有注释。要解决此问题,您应该使用@Repository注释注释ImageRepository,如下所示 -
@Repository
public interface ImageRepository extends CrudRepository<Image, Long> {
}