2

I'm having a problem using multiple path in component scan. I tried the solution from here How to scan multiple paths using the @ComponentScan annotation? but it gives me error:

example

@ComponentScan({"com.tx.loader", "com.tx.common"})
@SpringBootApplication

It says

Unexpected tokens (Use ';' to separate expressions on the same line)

I'm using Kotlin and Intellij IDE.

Sylhare
  • 5,907
  • 8
  • 64
  • 80
chiradee
  • 105
  • 1
  • 3
  • 14

3 Answers3

2

In Kotlin you don't use {} for array declaration, should use [] instead. In your particular case you can also use simplified notation

@ComponentScan("com.tx.loader", "com.tx.common")

or traditional ('basePackages' or 'value' seems to work equally in the annotation):

@ComponentScan(basePackages = ["com.tx.loader", "com.tx.common"])
Sylhare
  • 5,907
  • 8
  • 64
  • 80
Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42
1

Hey in Kotlin I found @ComponentScan to be a bit tricky. Here is how I used it, hopefully it matches your case:

To specify the base package classes:

@ComponentScan(
basePackageClasses = [com.tx.loader.Example1::class, com.tx.common.Example2::class])
)

Or use this one to specify the package names:

@ComponentScan(
    basePackages = ["com.tx.loader", "com.tx.common"]
)

Or you could use it in test with the Filter for services or components to autowire only what you need:

import org.springframework.context.annotation.ComponentScan
import org.springframework.stereotype.Component
import org.springframework.stereotype.Service

@ComponentScan(
    includeFilters = [ComponentScan.Filter(Service::class), ComponentScan.Filter(Component::class)],
)
Sylhare
  • 5,907
  • 8
  • 64
  • 80
1

This one solved my problem
Place it above the main function

@SpringBootApplication
@ComponentScan(basePackages = ["com.*"])
fun main(...) {...}
Spring
  • 831
  • 1
  • 12
  • 42