SpringBoot整合MongoDB遇UnsatisfiedDependencyException报错求助
SpringBoot + MongoDB CRUD服务启动报错排查与解决
报错信息
初始报错:
ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productController': Unsatisfied dependency expressed through field 'productServiceImpl'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.cts.eaution.impl.ProductServiceImpl' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
完整错误日志:
Error creating bean with name 'productController': Unsatisfied dependency expressed through field 'productService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productServiceImpl': Unsatisfied dependency expressed through field 'productRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productRepository' defined in com.cts.eauction.repository.ProductRepository defined in @EnableMongoRepositories declared on MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration: Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryDependentConfiguration.class]: Unsatisfied dependency expressed through method 'mongoTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoDatabaseFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoDatabaseFactorySupport]: Factory method 'mongoDatabaseFactory' threw exception; nested exception is java.lang.IllegalArgumentException: Database name must not contain slashes, dots, spaces, quotes, or dollar signs!
相关代码与配置
Controller类
@RestController @RequestMapping("/e-auction/api/v1/seller/") public class ProductController { @Autowired public ProductServiceImpl productServiceImpl; /* @Autowired public ProductRepository productRepository; */ @GetMapping("/show-bids") public List<Product> getAllProducts() { System.out.println("Hello Product..."); return productServiceImpl.findAll(); //return productRepository.findAll(); } }
ServiceImpl类
class ProductServiceImpl implements ProductService { @Autowired private ProductRepository productRepository; @Override public List<Product> findAll() { return productRepository.findAll(); } }
Service接口
@Service public interface ProductService { List<Product> findAll(); }
Repository接口
public interface ProductRepository extends MongoRepository<Product, String> { }
POM.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <!-- <version>2.7.5</version> --> <version>2.6.13</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.cts</groupId> <artifactId>eauction</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eauction</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.properties配置
#server server.port=8082 spring.data.mongodb.host=localhost spring.data.mongodb.port=27017 spring.data.mongodb.database=productdb
解决方案
从报错日志能定位到两个核心问题,逐一修正即可:
Service实现类未纳入Spring容器管理
ProductServiceImpl类缺少@Service注解,导致Spring无法扫描并创建该类的Bean实例,Controller注入时找不到匹配对象。修改后的代码如下:@Service public class ProductServiceImpl implements ProductService { @Autowired private ProductRepository productRepository; @Override public List<Product> findAll() { return productRepository.findAll(); } }同时建议Controller注入接口而非实现类,降低代码耦合:
@Autowired public ProductService productService;MongoDB数据库名包含非法空格
报错提示数据库名不能包含空格,你的配置spring.data.mongodb.database=productdb末尾多了一个空格,修改配置文件:spring.data.mongodb.database=productdb
完成以上两处修改后,重启应用即可解决启动报错问题。
内容的提问来源于stack exchange,提问作者Karthikeyan
相关产品推荐
相关产品推荐

