18

I made a jar with Spring components to include in multiple projects (core.jar).

I created a new Spring project, have @ComponentScan to the correct package, but it is not aware of the components in the jar.

How to make Spring's @ComponentScan search components in included JARs ?

Miciurash
  • 5,064
  • 4
  • 16
  • 21

2 Answers2

28

Give it the appropriate package name to scan in the JAR.

@ComponentScan(basePackages = {"com.example.from.jar"})
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • 1
    @Miciurash I'm having same issue; does Spring actually load every jar in the classpath into memory to scan? If not, how does it decide which jars to scan? Does it scan the jars without actually loading them (treating them like plain zip archives basically)? – Samuel Neff Mar 24 '22 at 16:08
6

I had a similar issue with Spring boot and @ComponentScan and this document helped me solving the issue: http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-structuring-your-code.html.

The point was that my @SpringBootApplication class was in the root package of one of basePackages of @ComponentScan. After I moved it to a subpackage, Spring was able to recognize all @ComponentScans.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Thx. This helped me alot! I prefer structuring my project other than adding additional helpers like @ComponentScan all over the projects. – groovedigga Oct 09 '19 at 13:37