Bird
0
0

How can you customize the base packages scanned by @SpringBootApplication if your main class is not in the root package?

hard📝 Application Q9 of 15
Spring Boot - Spring Annotations
How can you customize the base packages scanned by @SpringBootApplication if your main class is not in the root package?
AAdd @ComponentScan(basePackages = {"com.example.pkg"}) alongside @SpringBootApplication
BChange the package of the main class to root package only
CUse @EnableAutoConfiguration(basePackages = ...)
DAdd @SpringBootApplication(basePackages = ...)
Step-by-Step Solution
Solution:
  1. Step 1: Understand default scanning behavior

    @SpringBootApplication scans from its package and sub-packages only.
  2. Step 2: Customize scanning packages

    Use @ComponentScan with basePackages attribute to specify custom packages to scan.
  3. Final Answer:

    Add @ComponentScan(basePackages = {"com.example.pkg"}) alongside @SpringBootApplication -> Option A
  4. Quick Check:

    Customize scanning with @ComponentScan basePackages [OK]
Quick Trick: Use @ComponentScan to set scanning packages explicitly [OK]
Common Mistakes:
  • Trying to set basePackages on @SpringBootApplication directly
  • Moving main class package unnecessarily
  • Using @EnableAutoConfiguration for scanning

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes