Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何解决类型为';com.example.test.repository.ConfigRepository';可用:至少需要1个符合autowire条件的bean_Java_Spring_Spring Boot_Spring Mongodb - Fatal编程技术网

Java 如何解决类型为';com.example.test.repository.ConfigRepository';可用:至少需要1个符合autowire条件的bean

Java 如何解决类型为';com.example.test.repository.ConfigRepository';可用:至少需要1个符合autowire条件的bean,java,spring,spring-boot,spring-mongodb,Java,Spring,Spring Boot,Spring Mongodb,以下是我的目录结构 com.example com.example.common com.example.test com.example.test.repository 我的主要春季靴类如下所示 package com.example.test; @Import({ AutoConfig.class }) @SpringBootApplication public class testApplication { public static void main(String[] args)

以下是我的目录结构

  • com.example
  • com.example.common
  • com.example.test
  • com.example.test.repository
  • 我的主要春季靴类如下所示

    package com.example.test;
    
    @Import({ AutoConfig.class })
    @SpringBootApplication
    public class testApplication {
      public static void main(String[] args) {
        SpringApplication.run(testApplication.class, args);
      }
    }
    
    package com.example.common;
    
    @Configuration
    @EnableFeignClients
    @ComponentScan({ "com.example.common" })
    public class AutoConfig {
    
    我的存储库类别

    package com.example.test.repository.ConfigRepository;
    
     @Repository
     public interface ConfigRepository extends MongoRepository<Config, String>, QuerydslPredicateExecutor<Config> {
    
    }
    

    您的
    ConfigRepository
    类位于
    com.example.test.repository
    此包中

    当提供
    @ComponentScan
    时,您正在传递此路径
    com.example.common

    因此,您只需使用下面的
    com.example.test
    路径即可

    此外,在您的
    springboot应用程序
    文件或
    配置
    文件中,您可以提供
    和设置
    基本包
    属性

    package com.example.test;
    
    @Import({ AutoConfig.class })
    @EnableMongoRepositories(basePackages = {"com.example.test.repository"})
    @SpringBootApplication
    public class testApplication {
      public static void main(String[] args) {
        SpringApplication.run(testApplication.class, args);
      }
    }
    
    @Configuration
    @EnableFeignClients
    @ComponentScan({ "com.example.test" })
    public class AutoConfig {
    
    有关
    @enablemongorepositions
    的更多信息,您将从中获得想法。
    这将对您有所帮助。

    但如果我编写com.example.test,com.example.common中存在的类不会被扫描,它包含我所需的所有配置类。它现在正在工作,但为什么不能按预期工作?我的意思是它应该与annotation@repository一起工作,对吗?您需要通知容器这是我的存储库类。以下任一选项<代码>@EnableAutoConfiguration
    让Spring Boot自己解决它,或者
    @enablemongorepositionals(basePackageScan=“com.example.test.repository”)
    让我们自己指定它。