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

无法自动连接mongodb存储库spring boot

章威
2023-03-14

我在Spring Boot上还是新手,我在mongoDB数据库中使用Spring-Boot添加了一个名为文章的文档,我想在该文章中添加注释。但是Spring-boot不能在我的应用程序中自动连接我的存储库。

下面是我的Repository类,它实现了ArticleRepositoryCustom接口,该接口包含一个OuterComment方法。

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

这是我的Springboot应用程序课程

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

}

谁能帮帮我吗?

共有1个答案

秦才良
2023-03-14

我非常确定,根据本文档,特别是3.3.1中的配置部分,您的自定义存储库似乎没有被选中,因为它以Imp而不是Impl结尾。以下是相关文本:

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

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

 类似资料:
  • 我与SpringBoot和JPA合作。我收到一个无法完成的错误。 这是我的主要课程: 这是我的班级失败的原因: 这是类: 这是错误消息: 错误创建bean的名称'请求LoggerImpl':注入自动生成的依赖失败; 无法自动关联字段:专用com。存储库。请求logdao.com。记录器。impl。RequestLoggerImpl。请求logdao;嵌套的异常是org。springframewor

  • 我正在试验Spring和MongoDB。在我的项目中,我有一个存储库和一个有调度方法的服务。问题是,存储库没有自动运行,它总是为空。 Autowire在主应用程序类中正常工作(通过实现CommandLineRunner进行测试) 我错过什么了吗? 目录 机器应用。JAVA 工人JAVA LineDataRepository

  • 问题内容: 我正在尝试在github仓库上连接詹金斯。 当我指定Repo URL jenkins时,返回以下错误消息: 无法连接到存储库:命令“ git ls-remote -h git@github.com:adolfosrs / jenkins-test.git HEAD”返回状态码128:stdout:stderr:主机密钥验证失败。致命:无法从远程存储库读取。请确保您具有正确的访问权限,并

  • 我已经用@Autowired注释为相应的存储库定义了服务类 存储库接口定义为从JpaReepository扩展 应用程序自动编译服务类 运行时,我得到以下错误

  • 我显然遗漏了一些东西。我正在制作一个简单的应用程序,其中包含并面临以下错误: 我的代码: 应用程序: pom.xml 控制器: 人事服务: 个人服务: PersonRepository(此存储库不能自动连接): 已经在网上搜索了。我什么都没找到。有什么想法吗?

  • 我已经将应用程序和存储库接口的所有实体打包到一个jar中。存储库是用@Repository注释编写的: 我已经在我的Spring启动应用程序中包含了这个jar文件,并试图像这样从控制器自动连接接口: 我的主要应用程序类是这样编写的: 存储库没有自动连接。当我启动Spring boor应用程序时,我得到以下错误: 有人尝试过类似的架构吗?