spring|January 14, 2022|1 min read

Spring - Learn Multiple Ways to use PackageScan Annotation

TL;DR

Use @ComponentScan with single packages, multiple packages via arrays, or basePackageClasses to configure Spring bean discovery.

Spring - Learn Multiple Ways to use PackageScan Annotation

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

Related Posts

Spring Boot - Fixing Autowire Bean Not found

Spring Boot - Fixing Autowire Bean Not found

Introduction In a Spring boot app, we tend to use annotation, so that Spring…

How to Solve Spring Okta/Saml issue of SAML message intended destination endpoint did not match the recipient endpoint

How to Solve Spring Okta/Saml issue of SAML message intended destination endpoint did not match the recipient endpoint

Introduction I was trying to integrate Okta with Spring, and when I deploy the…

Java Log4j Logger - Programmatically Initialize JSON logger with customized keys in json logs

Java Log4j Logger - Programmatically Initialize JSON logger with customized keys in json logs

Introduction Java log4j has many ways to initialize and append the desired…

Java - Union and Intersection of two lists

Java - Union and Intersection of two lists

Suppose you have two lists, and you want Union and Intersection of those two…

Linkage Error Loader Constraint Violation - JUnit test case development issue

Linkage Error Loader Constraint Violation - JUnit test case development issue

Its good to write unit tests cases, and this part is mostly forgotten by…

How to mock a constructor - Junit test case development issues

How to mock a constructor - Junit test case development issues

While writing JUnit test cases, we encounter cases like we want to initialize a…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…