Rahul Raj
Rahul Raj

Reputation: 3459

Spring Boot - Scan whole packages without @EntityScan/@EnableJpaRepositories

Below is my Spring Boot starter class.

@SpringBootApplication
@ComponentScan({"com.example"})
@EntityScan("com.example.entity")
@EnableJpaRepositories("com.example.repository")
public class SpringAppApplication {

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

Now, how can I configure something like @ComponentScan({"com.example.*"}) so that I can avoid adding @EntityScan("com.example.entity") and @EnableJpaRepositories("com.example.repository")

Upvotes: 0

Views: 2941

Answers (1)

Urosh T.
Urosh T.

Reputation: 3824

@SpringBootApplication annotation is actualy 3 annotations in one which includes the @ComponentScan (take a look here). And if you have it in the root package of your project (which is considered a good practice) you do not have to do anything there.

@EntityScan and @EnableJpaRepositories are different and are related to spring data library so it really kinda makes sense NOT to have them included in @SpringBootApplication.

Because these annotations could easily be on a configuration class for database or something like that (separating your configuration classes also has it's benefits sometimes).

If you want, you can always write your own custom annotation and inherit the two (or 4) annotations you want. There are plenty of online resources out there on the topic.

Upvotes: 1

Related Questions