无法自动装配 mongodb 存储库 spring-boot

Could not autowire mongodb repository spring-boot

我还是 Spring Boot 的新手,我使用 Spring-Boot 在 mongodb 数据库中添加了一个名为 Article 的文档,我想在该文章中添加评论。但是 Spring-boot 无法在我的应用程序中自动装配我的存储库。

这是我的存储库 class,它实现了包含 ajouterComment 方法的 ArticleRepositoryCustom 接口。

public class ArticleRepositoryImp implements ArticleRepositoryCustom {

@Autowired
private MongoTemplate mongoTemplate;


@Override
public void ajouterComment(String auth,String commentAuth, String text, Date date) {
    Comment comment=new Comment("comentAuth", "auth", date);
    mongoTemplate.save(comment);
    Criteria criteria = Criteria.where("author").is(auth);
     mongoTemplate.upsert(Query.query(criteria), new Update().push("comments",comment), Article.class);
}

}

文章库

public interface ArticleRepository extends MongoRepository<Article, ObjectId>,ArticleRepositoryCustom {
public Article findByAuthor(String author);
}

这是我的 SpringBootApplication class

@SpringBootApplication
public class Example2MongoDbApplication implements CommandLineRunner {

@Autowired
private ArticleRepository repository;

public static void main(String[] args) {
    SpringApplication.run(Example2MongoDbApplication.class, args);
}

@Override
public void run(String... arg0) throws Exception {

    repository.save(new Article(UUID.randomUUID(),"Ettaibi",new Date(),"LKITAB2"));
    System.out.println(repository.findByAuthor("med"));
    repository.ajouterComment("Med", "Said", "Hello Med", new Date());  

    }

当我 运行 我的应用程序出现以下异常时

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'example2MongoDbApplication': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private demo.ArticleRepository demo.Example2MongoDbApplication.repository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'articleRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property ajouterComment found for type void!
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at demo.Example2MongoDbApplication.main(Example2MongoDbApplication.java:18)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private demo.ArticleRepository demo.Example2MongoDbApplication.repository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'articleRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property ajouterComment found for type void!
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 15 more
 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'articleRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property ajouterComment found for type void!
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 17 more

这是我的class文章

@Document
public class Article {
@Id
private ObjectId id;

private UUID authorId;
private String author;
private Date date;
private String title;
private byte text;
public List<Comment> comments;



public List<Comment> getComments() {
    return comments;
}
public void setComments(List<Comment> comments) {
    this.comments = comments;
}
public ObjectId getId() {
    return id;
}
public void setId(ObjectId id) {
    this.id = id;
}
public UUID getAuthorId() {
    return authorId;
}
public void setAuthorId(UUID authorId) {
    this.authorId = authorId;
}
public String getAuthor() {
    return author;
}
public void setAuthor(String author) {
    this.author = author;
}
public Date getDate() {
    return date;
}
public void setDate(Date date) {
    this.date = date;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public byte getText() {
    return text;
}
public void setText(byte text) {
    this.text = text;
}
public Article(UUID authorId, String author, Date date, String title) {
    super();
    this.authorId = authorId;
    this.author = author;
    this.date = date;
    this.title = title;
}

}

有人可以帮我吗?

我很确定,基于 this documentation,特别是 3.3.1 中的配置部分,您的自定义存储库似乎没有被拾取,因为它以 Imp 而不是 Impl 结尾。以下是相关文字:

If you use namespace configuration, the repository infrastructure tries to autodetect custom implementations by scanning for classes below the package we found a repository in. These classes need to follow the naming convention of appending the namespace element's attribute repository-impl-postfix to the found repository interface name. This postfix defaults to Impl.

他们确实提到了一种 XML 配置后缀的方法,如果您想这样做的话。可能还有 Java 配置或注释方法,但我不知道。