SpringBoot - 通过字段表达不满足的依赖关系

问题描述 投票:0回答:3

我正在尝试使用 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)}

控制器类:

@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
public interface ProductService {
    
    List<Product> findAll();

}

存储库接口:

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>

我通过添加注释(如(服务、存储库、组件、组件扫描))尝试了多种选项,但都不能解决问题。

应用属性: #服务器 服务器端口=8082

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=productdb 

完整错误日志:

 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!

spring mongodb spring-boot spring-data-mongodb
3个回答
3
投票

您必须将

@Service
注释放置在实现上而不是接口上,以便组件扫描能够自动检测到它。像这样:

@Service
class ProductServiceImpl implements ProductService {
    
    @Autowired
    private ProductRepository productRepository;

    @Override
    public List<Product> findAll() {
        return productRepository.findAll();
    }

}

此外,您应该要求接口类型的 bean,而不是控制器中的实现:

@RestController
@RequestMapping("/e-auction/api/v1/seller/")
public class ProductController {
    
    @Autowired
    public ProductService productService;
}

这样你就可以对同一个接口有不同的实现,而且你不应该使用字段注入,除非你有充分的理由使用它。


0
投票

如果上述解决方案不起作用,那么您可以尝试的另一种方法是输入 -

@ComponentScan(basePackages = {"com.package.class", "com.package.anotherClass"}) 

在你的主应用程序类中。

这将扫描包中存在的所有类。


0
投票

就我而言,我在

application.properties
中使用了以下配置行:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

我刚刚删除了线路,一切又恢复正常了。

© www.soinside.com 2019 - 2024. All rights reserved.