【问题标题】:@ComponentScan on external library not working外部库上的@ComponentScan 不起作用
【发布时间】:2016-05-01 13:20:13
【问题描述】:

在我的 spring boot 项目中,我必须使用一个外部库,它在 spring 上下文中定义了 beans。所以在我的应用程序类中,我在下面添加了我的项目和外部库的基础包,

@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
@EnableHypermediaSupport(type = { EnableHypermediaSupport.HypermediaType.HAL })
@EnableSwagger2
@ComponentScan(basePackages = {"com.mylibrary.test", "com.otherlibrary.springtool"})
//@EnableDiscoveryClient
public class Application extends RepositoryRestMvcConfiguration {
}

但是@Configuration等其他库中的bean没有初始化?

【问题讨论】:

  • 你在哪里添加了这个@ComponentScan?这甚至被扫描了吗?
  • @M.Deinum 我已经编辑了问题
  • 该配置对我来说看起来不错。您可能想使用basePackageClasses 来消除包名称中出现拼写错误的可能性。此外,如果您使用的是 Spring Boot 1.3,您可以将包配置为直接扫描 SpringBootApplication 并避免额外的注释
  • 我也有同样的问题,好像库中包含的“@Configuration”中的“@ComponentScan”,其基本包与调用者的不同,不扫描任何东西

标签: java spring spring-boot


【解决方案1】:

当我尝试使用来自外部库的开放 feign 接口时,我遇到了类似的情况,然后我不得不添加 @EnableFeignClients(basePackages = {"lib.pckg"}),因为 Feign 必须为我创建 bean,而不是 Spring IoC。

如果你提供一些日志错误会很好。

【讨论】:

    【解决方案2】:

    @ComponentScan 适用于使用@Component、@Repository 或@Service 注释的类。确保“com.otherlibrary.springtool”中的类使用上面的注释进行注释,否则您必须使用 @Bean 注释将它们声明为 Spring bean。希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 2016-11-23
      • 2014-10-22
      • 2016-08-31
      • 1970-01-01
      • 2015-04-15
      • 2020-03-07
      相关资源
      最近更新 更多