Spring - Learn Multiple Ways to use PackageScan Annotation

January 14, 2022

Introduction

In this post, we will see multiple ways to use @PackageScan annotation.

Multiple Ways of Using @PackageScan

  • Provide Single package
@ComponentScan("com.mypackage1")
  • Provide Multiple Packages by String[] array
@ComponentScan({"com.mypackage1", "com.mypackage2"})
  • By Specifying Classes
@ComponentScan(basePackageClasses = {MyClass1.class, MyClass2.class})

This way is type-safe.

  • By Using basePackages attribute
@ComponentScan(basePackages={"com.mypackage1", "com.mypackage2"})
  • Nested Usage of ComponentScan
@ComponentScans(value = { 
    @ComponentScan("com.mypackage1"),
    @ComponentScan("com.mypackage2") 
})

See another post, where we write on Fixing Autowire Bean Not Found


Similar Posts

Latest Posts