7

In my spring boot project i have to use a external library which it has define beans in spring context. So in my Application class i have add below which are the base package for bothe my project and the external library,

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

But the beans in other library such as @Configuration are not initialize?

Harshana
  • 7,297
  • 25
  • 99
  • 173
  • Where have you added this `@ComponentScan`? Is that even being scanned? – M. Deinum Jan 25 '16 at 11:11
  • @M.Deinum I have edit the question – Harshana Jan 25 '16 at 11:46
  • 1
    That configuration looks fine to me. You might want to use `basePackageClasses` to eliminate the possibility of a typo in the package name. Also, if you're using Spring Boot 1.3, you can configure the packages to scan directly on `SpringBootApplication` and avoid an extra annotation – Andy Wilkinson Jan 25 '16 at 13:26
  • I have the same problem, seems like '@ComponentScan' in a '@Configuration' included within a library, whose base package is different from the caller's, does not scan anything – Whimusical Jul 25 '17 at 17:02

2 Answers2

3

@ComponentScan works for the classes that are annotated with @Component, @Repository or @Service. Make sure classes in "com.otherlibrary.springtool" are annotated with above annotation to be found or else you have to declare them as Spring beans with @Bean annotation. Hope it helps.

xafarr
  • 194
  • 5
1

I had something similar when I was trying to use open feign interfaces coming from an external lib and then I had to add the @EnableFeignClients(basePackages = {"lib.pckg"}), because the Feign had to create the beans for me, not the Spring IoC.

It would be good if you provide some log error.

Guilherme Alencar
  • 1,243
  • 12
  • 21