【问题标题】:Could not autowire mongodb repository spring-boot无法自动装配 mongodb 存储库 spring-boot
【发布时间】:2015-07-29 14:00:53
【问题描述】:

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

这是我的 Repository 类,它实现了 ArticleRepositoryCustom 接口,其中包含 ajouterComment 方法。

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 类

@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$1.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$1.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

这是我的课堂文章

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

}

谁能帮帮我?

【问题讨论】:

    标签: spring mongodb spring-boot spring-data-mongodb


    【解决方案1】:

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

    如果您使用命名空间配置,存储库基础架构会尝试通过扫描我们在其中找到存储库的包下的类来自动检测自定义实现。这些类需要遵循将命名空间元素的属性 repository-impl-postfix 附加到的命名约定找到的存储库接口名称。此后缀默认为 Impl。

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

    【讨论】:

      猜你喜欢
      • 2017-03-28
      • 1970-01-01
      • 1970-01-01
      • 2019-02-28
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多