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)
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”)
让我们自己指定它。