【问题标题】:Spring repository components not found in gradle subproject (Springboot)在 gradle 子项目(Springboot)中找不到 Spring 存储库组件
【发布时间】:2015-05-19 00:56:50
【问题描述】:

我有一个多项目 gradle 项目:

ht-java

  • ht 域
  • ht-scraper

ht-scraper 使用位于 ht-domain 中的域模型和 daos。 当我启动 springboot 应用程序时,出现以下错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.hypetube.domain.dao.ChannelRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

当我在 ht-scraper 中有存储库类时,一切正常。

我的 Spring 应用如下所示:

@Configuration
@EnableAutoConfiguration
@ComponentScan({"com.hypetube", "com.hypetube.domain"})
@EnableConfigurationProperties //use this to register other properties sources e.g. property files
public class Application {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Autowired
    private Environment env;

    @PostConstruct
    public void printActiveSpringProfiles() {
        log.info("Following spring profiles are active: {}", new ArrayList<>(Arrays.asList(env.getActiveProfiles())));
    }
}

settings.gradle:

include 'ht-domain', 'ht-scraper'

build.gradle (ht.scraper):

compile project(":ht-domain")

一切正常;该项目编译没有错误是迫在眉睫。 就在我启动应用程序时发生错误。

【问题讨论】:

  • 那个项目可以在线使用吗?
  • 很遗憾不是,它是一个商业项目。我不能全部发布。会不会是子项目 jar 的清单有问题?清单目前为空
  • 也许可以,很难说。如果可能,请提供重现错误的示例项目。
  • 很好,稍后看看。

标签: gradle spring-boot


【解决方案1】:

很奇怪为什么spring-boot 插件不包含ht-domain 子项目。要解决测试,请将以下代码添加到 ht-scraper/build.gradle

test {
    classpath = project(":ht-domain").sourceSets.main.runtimeClasspath
}

但是运行打包的应用程序仍然存在问题。我已经从PlaylistRepository 类中删除了所有注释,并将以下注释添加到Application

@EnableMongoRepositories({"com.hypetube.domain"})

不,它开始很好 - 至少没有类路径问题。希望以某种方式有所帮助,正如我所说的非常奇怪的行为:/

【讨论】:

  • 这似乎有效。而不是使用@EnableMongoRepositories,而是在编译依赖项中使用 project(":ht-domain").sourceSets.main.runtimeClasspath 。感谢蛋白石
  • 那也应该做的工作。然而相当奇怪的行为。
  • 是的,你是对的。我认为有些东西是错误的,可能在 springboot 中。
  • @EnableMongoRepositories(basePackageClasses = UserRepository.class) 仍然需要,即使 project(":ht-domain").sourceSets.main.runtimeClasspath 在 compile deps....很奇怪
猜你喜欢
  • 1970-01-01
  • 2021-01-24
  • 1970-01-01
  • 2018-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-24
  • 1970-01-01
相关资源
最近更新 更多