使用GridFSBucket报错如下,
2021-07-29 09:31:02,068 WARN [main] AnnotationConfigServletWebServerApplicationContext: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'fileUploadResource' defined in URL [jar:file:/xxxxxx/FileUploadResource.class]: Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mongodb.client.gridfs.GridFSBucket' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
解决:
@Configuration
public class MongoConfig {
@Autowired
private MongoDbFactory mongoDbFactory;
@Bean
public GridFSBucket getGridFSBucket(){
MongoDatabase db = mongoDbFactory.getDb();
return GridFSBuckets.create(db);
}
}