我仍然是Spring Boot的新手,我使用Spring-Boot在mongodb数据库中添加了一个名为Article的文档,我想在该文章中添加注释。但Spring-boot无法在我的应用程序中自动装入我的存储库。
这是我的Repository类,它实现了包含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);
}
}
ArticleRepository
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;
}
}
有人可以帮我吗?
答案 0 :(得分:0)
我很确定基于this documentation,特别是3.3.1中的Configuration部分,看起来你的自定义存储库没有被选中,因为它以Imp而不是Impl结束。以下是相关文字:
如果使用命名空间配置,则存储库基础结构会尝试通过扫描我们在存储库中找到的包下面的类来自动检测自定义实现。这些类需要遵循附加命名空间元素的属性存储库的命名约定 - impl-postfix到找到的存储库接口名称。此后缀默认为Impl。
如果你想这样做,他们确实提到了一种配置后缀的XML方法。可能还有Java配置或注释方法,但我不知道这一点。