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

Software Security in the AI Era: How to Write Secure Code When AI Writes Code Too

Software Security in the AI Era: How to Write Secure Code When AI Writes Code Too

In 2025, 72% of professional developers used AI-assisted coding tools daily. By…

SQL Injection: The Complete Guide to Understanding, Preventing, and Detecting SQLi Attacks

SQL Injection: The Complete Guide to Understanding, Preventing, and Detecting SQLi Attacks

SQL injection has been on the OWASP Top 10 since the list was created in 200…

Building a Vulnerability Detection System That Developers Actually Use

Building a Vulnerability Detection System That Developers Actually Use

Here’s a stat that should concern every security team: 73% of developers say…

How to Be a Full-Time Freelancer: Resources, Finding Clients, and Building a Sustainable Business

How to Be a Full-Time Freelancer: Resources, Finding Clients, and Building a Sustainable Business

Making the leap from full-time employment to freelancing is one of the most…

Deep Dive on Elasticsearch: A System Design Interview Perspective

Deep Dive on Elasticsearch: A System Design Interview Perspective

“If you’re searching, filtering, or aggregating over large volumes of semi…

Deep Dive on Apache Kafka: A System Design Interview Perspective

Deep Dive on Apache Kafka: A System Design Interview Perspective

“Kafka is not a message queue. It’s a distributed commit log that happens to be…