![](/img/trans.png)
[英]Consider defining a bean of type 'com.repository.UserRepository' in your configuration
[英]Consider defining a bean of type 'com.face.ImageRepository' in your configuration?
我有一個Java Spring啟動項目,正在其中創建一個發布請求。代碼如下所示:
主要:
@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.
感謝您對此的任何幫助! 非常感謝,
您的spring容器不會掃描圖像存儲庫,因為它沒有注釋。 要解決此問題,您應該使用@Repository批注對ImageRepository進行批注,如下所示-
@Repository public interface ImageRepository extends CrudRepository<Image, Long> { }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.