Does @ComponentScan override @SpringBootApplication?

Viewed 703
2 Answers

@SpringBootApplication annotation can component scan classes from packages under Application class belong to . Also you can add @ComponentScan to scan classes not under package Application class belongs to.

@ComponentScan("external.pkg")
@SpringBootApplication
class MyApp{
}

the annotation @SpringBootApplication contains @EnableAutoConfiguration annotation, which will scan through your jar, class, and maven dependencies in the classpath to register beans. If you declare beans cross packages and only want to use specific beans from specific packages, then use @ComponentScan

@ComponentScan gives you a fine-grained package or class level control on what beans you declared would be registered.

Related