In Spring, component scanning is a feature that allows the framework to detect and register Beans (annotated with @Component
, @Service
, @Repository
, @Controller
, or any custom stereotype annotations) automatically during application startup.
Here’s how you can enable and use this feature effectively:
1. Enable Component Scanning in a Java Configuration Class
To enable automatic scanning, use the @ComponentScan
annotation in your configuration class. This is commonly used to define the base packages to scan for Spring-managed components.
Example:
package org.kodejava.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "org.kodejava.spring") // Specify base package to scan
public class AppConfig {
}
Here, Spring will scan the org.kodejava.spring
package and its sub-packages for any classes annotated with @Component
, @Service
, @Repository
, or @Controller
.
2. Use Stereotype Annotations
Add one of the following annotations to your classes to mark them as Spring-managed components:
@Component
: Generic for any Spring-managed component.@Service
: Specifically for service layer components.@Repository
: For DAO components (adds exception translation).@Controller
/@RestController
: For web controllers in a Spring MVC application.
Example:
package org.kodejava.spring.service;
import org.springframework.stereotype.Service;
@Service
public class MyService {
public String getMessage() {
return "Hello, Spring Component Scanning!";
}
}
3. Shortcut with @SpringBootApplication
If you’re using Spring Boot, the @SpringBootApplication
annotation already includes component scanning. It automatically scans the package where the main application class resides and its sub-packages.
Example:
package org.kodejava.spring;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication // Includes @ComponentScan by default
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
In this case, Spring Boot will scan all components in com.example
and its sub-packages automatically.
4. Advanced Configuration (Optional)
a. Scanning Multiple or Specific Packages
You can specify multiple packages to scan:
@ComponentScan(basePackages = {"org.kodejava.spring.service", "org.kodejava.spring.repository"})
b. Filter Components
You can filter which types of components to include or exclude using the includeFilters
or excludeFilters
attributes of @ComponentScan
.
Example:
@ComponentScan(
basePackages = "org.kodejava.spring",
includeFilters = @ComponentScan.Filter(MyCustomAnnotation.class),
excludeFilters = @ComponentScan.Filter(RestController.class)
)
This will scan the org.kodejava.spring
package but include only components annotated with @MyCustomAnnotation
and exclude all @RestController
components.
c. Scan by Custom Annotation
You can create your custom annotation and use it as a filter:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Component // Marks it as a Spring component
public @interface MyCustomAnnotation {
}
Then annotate classes using @MyCustomAnnotation
and configure the scanner accordingly.
5. XML-based Configuration (Legacy Approach)
If you’re using XML-based configuration (rare in modern Spring apps), you can configure component scanning like this:
<context:component-scan base-package="org.kodejava.spring"/>
6. Tips
- Place your configuration class or main application class at a high-level base package to ensure all sub-packages are scanned automatically.
- Avoid scanning unnecessary packages outside your application (e.g., third-party libraries or system packages) to improve performance.
- Use
@Lazy
with components for lazy initialization if needed.
By using these approaches, you can enable automatic detection and registration of Spring beans with ease!
Maven Dependencies
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.2.6</version>
</dependency>