10

I have Spring boot application

@SpringBootApplication
@EntityScan(basePackages = {"${scan.packages}"})
public class MyApplication {

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

While reading multiple entity scan packages from one property separated by comma like below?

scan.packages=com.mycompany.model.package1 , com.mycompany.model.package2

I got this exception :

java.lang.IllegalArgumentException: Unknown entity: com.mycompany.model.package2.Myclass

Elsayed
  • 2,712
  • 7
  • 28
  • 41
  • Remove the `{}` from the entity scan and Spring then might try to resolve them to an array. If that doesn't work you might want to use a SpEL expression to do that for you. Although you might want to reconsider if this is really what you want/need. – M. Deinum Mar 27 '19 at 10:30

5 Answers5

16

You can scan multiple Entity like this

@EntityScan(basePackages= {"scan.packages1","scan.packages2"})
Devratna
  • 938
  • 1
  • 7
  • 26
0

This should work

@EntityScan(basePackages = {"#{'${scan.packages}'.split(',')}"})
Bunyamin Coskuner
  • 8,719
  • 1
  • 28
  • 48
  • Thanks a lot @Bunyamin , unfortunately it doesn't work – Elsayed Mar 28 '19 at 11:13
  • @Elsayed [the original response](https://stackoverflow.com/questions/6212898/spring-properties-file-get-element-as-an-array) for more context. Even though I couldn't manage to make it work :( – Olimpiu POP Jul 07 '20 at 11:14
0

According to the EntityScan annotation javadoc there are three ways to define the packages where to scan for entities:

  1. value, alias for basePackages: @EntityScan( {"scan.packages1","scan.packages2"})

  2. basePackages, alias for value: @EntityScan(basePackages= {"scan.packages1","scan.packages2"}).

  3. basePackagesClasses, the type safe version: @EntityScan(basePackages=E.class}.Where E was a marker interface just to underline the Entities. Please see the code bellow. It could also be an annotation.

    interface E {
    }
    
    @Entity
    public class Glass implements E {
    // Typical code to be added here
    }
    

Or as an annotation:

@Retention(RetentionPolicy.RUNTIME) 
@interface E {
}

@Entity
@E
public class Glass implements E {
   // Typical code to be added here
}

From my perspective I would choose either using directly value or basePackageClasses. If I can something easier to read I would do that, and I think that is what value does, while the basePackageClasses introduces the added benefit of type safety and I can see multiple reasons to go for that. It all depends on the context.

Olimpiu POP
  • 5,001
  • 4
  • 34
  • 49
0

I hit the same problem and posted an issue on the Spring Boot issue tracker about it.

The answer was that

... As shown by #25436, @EntityScan did support property solution in 2.3 so, if we decide to reinstate support, we might want to consider how to handle multi-value properties.

So it appears some Spring Boot version did support it, but then the support was dropped again... for more info, check the Spring Boot issue linked in the quote.

rec
  • 10,340
  • 3
  • 29
  • 43
-3

try this :

@EntityScan(basePackages= {"${scan.packages1","scan.packages2}"})
Mourad9
  • 5
  • 5