[英]How do I disable MongoDB establishing a connection on start in a Spring Boot application?
我有一個 Spring 引導應用程序,它有兩種不同的數據庫實現,一種是 SQL,一種是 NO-SQL (MongoDB)。 SQL 實現是默認實現,我目前沒有使用 NO-SQL 實現。 但是,應用程序嘗試連接到 MongoDB 服務器,如果不可用,則會顯示錯誤。
我嘗試了以下方法:
SpringBootApplication(exclude = {
MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class
})
但是,在這種情況下,我收到以下錯誤:
Field operations in ...mongodb.CustomThingRepositoryImpl required a bean of type 'org.springframework.data.mongodb.core.MongoOperations' 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 'org.springframework.data.mongodb.core.MongoOperations' in your configuration.
我的 MongoDB 存儲庫類如下所示:
public interface SpringDataMongoThingRepository extends MongoRepository<ThingDocument, String>,
CustomThingRepository<ThingDocument, String> {
}
public interface CustomThingRepository<T, ID> {
[...]
<S extends T> S save(S entity);
void deleteById(ID id);
}
@Component
public class CustomThingRepositoryImpl<T, ID> implements CustomThingRepository<T, ID> {
@Autowired
private MongoOperations operations;
@Autowired
private ThingDataStorageProcessor thingDataStorageProcessor;
[...]
@Component
public class ThingDataStorageProcessor {
@Autowired
private GridFsOperations gridFsOperations;
[...]
我嘗試提供 MongoOperations 和 GridFsOperations 的虛擬實現,但仍然出現錯誤:
Description:
A component required a bean named 'mongoTemplate' that could not be found.
Action:
Consider defining a bean named 'mongoTemplate' in your configuration.
我怎樣才能至少防止 MongoDB 在啟動時連接?
這對我有用:
@Repository
@ConditionalOnProperty(prefix = "spring.data.mongodb", name = "uri")
public interface SpringDataMongoThingRepository...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.