import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface FormRepository extends MongoRepository<Form, Integer> {
Optional<Form> findById(Integer formId);
}
這是我創建的一個 mongo 存盤庫。我按如下方式訪問它:
@Service
public class FormServiceImpl implements FormService {
@Autowired
FormRepository formRepository;
@Override
public List<Question> getQuestions(Integer formId, ){
List<Question> questionList = new ArrayList<>();
..........
......................
........ initialise questionlist .............
......................
formRepository.save(new Form(questionList, "firstForm"));
return questionList;
}
}
我得到的錯誤->
***************************
APPLICATION FAILED TO START
***************************
Description:
Field formRepository in *.service.impl.FormServiceImpl required a bean of type '*.service.FormRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type '*.service.FormRepository' in your configuration.
Process finished with exit code 1
我的想法:@Repository 應該足以將 FormRepository 注冊為 bean。然而,事實并非如此。我無法將 FormRepository 介面注冊為 bean。上面的兩個檔案位于同一個包中。我已經用 * 替換了前綴路徑 - 但請放心 * 在任何地方都有相同的含義。如果有任何其他日志,請告訴我。
編輯 :
我在我的主應用程式類上添加了@EnableMongoRepositories。這導致了這個錯誤->
2022-04-24 19:20:29.085 ERROR 2941 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field formRepository in *.service.impl.FormServiceImpl required a bean named 'mongoTemplate' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean named 'mongoTemplate' in your configuration.
Process finished with exit code 1
我認為 MongoTemplate 和 MongoRepository 是兩個非常不同的東西。為什么我的 mongo repo 需要對 MongoTemplate 的依賴?
uj5u.com熱心網友回復:
由于您使用的是 mongo,因此您需要添加額外的注釋。
@SpringBootApplication
@EnableMongoRepositories(basePackageClasses = FormRepository .class)
public class MainClass {}
還要確保您的檔案夾結構正確。
對于另一個問題,您應該在“POM”中添加依賴項
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
這都是關于 mongo 配置的。希望這可以幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/465235.html
標籤:爪哇 春天 mongodb 粗鲁 mongorepository
上一篇:mongodb在陣列中查找欄位
