Friday, March 10, 2023

How @Configuration, @Component, and @ComponentScan annotations works in Spring Framework? Component Scanning Example

Hello guys, if you are wondering how to does autowiring work in Spring Framework and how does Spring Framework find dependency in runtime then you have come to the right place. Earlier, I have explained How Spring MVC works and how Spring Security works, and In this tutorial, I will tell you how component scanning and auto-wiring works in Spring. You will also learn how to enable component scanning in a Spring application. When you develop a software application using spring, you need to tell the Spring framework where to look for Spring components. Using component scanning it is very much easier as this enables auto-detection of beans by spring container and smoothly.

Spring can automatically scan any classes that have the stereotypes annotations like @Component, @Controller, @Service, and @Repository. The @ComponentScan's base packages attribute defines which packages should be looked through for decorated beans.

This means, Java developers should annotate their @Configuration class with @ComponentScan and our bean classes with one of the stereotype annotations for component scanning to work.
  • @Component
  • @Controller
  • @Repository
  • @Service

The most important from the above is @Component annotation and others are candidate for spring container registration. By the way, if you are wondering what is difference between @Controller, @Reposisotry and @Service annotation in Spring then you can also checkout that tutorial. It's one of the essential concept in Spring and also a popular spring framework interview question. 




How does Spring auto component scanning works?

The first question anyone working in Spring Framework and Spring Boot ask is what is automatic discovery of beans and importance? So, let's first answer that. When beans use dependency injection, it's evident that the number of beans grows, as does the amount of XML that needs to be written. 

As a result, Spring includes a feature called Auto-Wiring, which reduces the amount of XML required by taking into account the assumptions that may be made about beans and properties. 

So with the use of automatic discovery of beans, the size of XML can also be reduced for large applications.

The automatic discovery of beans is governed by certain rules and some of them are given below.
  • context:component-scan tag in the spring-config.xml – Make it easier to locate the beans
  • tag context:annotation-tag in the spring-config.xml – Allows Spring to use annotations
  • @Component annotation – Define a class as an auto-discoverable bean for Spring

So as mentioned above, after using @Component annotation there is no need to declare beans in the Spring-config.xml file and this helps to keep the XML file light as well.



@ComponentScan without arguments Example

You can use @ComponentScan without arguments to scan the current package and all its sub-packages. In fact @ComponentScan along with @Configuration annotation is used to specify the packages that we want to be scanned.

Like mentioned before, it is optional to specify the basePackage value. When used without an argument, you will have less control over the package scanning.

@Configuration
@ComponentScan
public class DemoAppConfig {
//code
}


The location where @ComponentScan is put, the scanning starts from its package by default.

And also there the component scanning starts from its package by default and it needs only needs the location.

Finally, note that in our example, @ComponentScan is equivalent to:

@ComponentScan(basePackages = "com.example.application")





@ComponentScan with arguments Example

There will have a less control over the package scanning when you use it without an argument:

@Configuration
@ComponentScan
public class DemoAppConfig {
   //code
}


The scanning starts from its package by default after the location of @ComponentScan is given.


The @SpringBootApplication which is declared in the main class of the spring boot application is capable of handling,

· @Configuration

· @EnableAutoConfiguration

· @ComponentScan

But if you want to have more control over the classpath scanning, better to move with filters in @ComponentScan.




The @ComponentScan is capable of detecting the stereotype annotations like @Component, @Repository, @Service, @Controller, @RestController, and the meta-annotation @Configuration and also disabling them.

@ComponentScan(basePackages = " com.example.application", useDefaultFilters=false)


Other thing is, if you want to disable of @Repository annotated class without leaving the classes that have a Dao in their name- EX- StudentDao.java

So we can have a class like below.

@Configuration
@ComponentScan(basePackages = "com.example.application ",
includeFilters = @Filter(type = FilterType.REGEX, pattern = ".*Dao"),
excludeFilters = @Filter(Repository.class))
public class AppConfig {
   //your code
}


So as from the above code segment, the classes with Dao are accepted while other classes rejected by the @ComponentScan with arguments.


That's all about how component scanning and auto-wiring works in Spring Framework. In this tutorial, we have discussed the ways which you can use @ComponentScan annotation to auto scan the classes as well as the many attributes that can be modified to achieve the required scanning behavior. Hope you enjoy the tutorial.


Other Java and Spring Tutorial you may like
  • Difference between @Autowired and @Inject in Spring? (answer)
  • 20+ Spring MVC Interview Questions for Programmers (answer)
  • Top 5 Frameworks Java Developer Should Know (frameworks)
  • Top 5 Spring Cloud annotations Java programmer should learn (cloud)
  • 10 Advanced Spring Boot Courses for Java developers (courses)
  • Top 7  Courses to learn Microservices in Java (courses)
  • Difference between @RequestParam and @PathVariable in Spring (answer)
  • Spring Data JPA @Query Example (query example)
  • 5 Courses to learn Spring Cloud for Microservices (courses)
  • @SpringBootApplication vs @EnableAutoConfiguration? (answer)
  • 10 Spring MVC annotations Java developer should learn (annotations)
  • 15 Spring Boot Interview Questions for Java Developers (questions)
  • 5 Courses to Learn Spring Security for Java programmers (courses)
  • Top 5 Courses to Learn and Master Spring Cloud (courses)
  • Top 5 Spring Boot Annotations Java Developers should know (read)

Thanks for reading this article so far. If you find this Spring @Component Tutorial and Example then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

P. S. - If you are new to Java and Spring Framework and want to learn Spring and looking for a free resources then you can also check out these free Spring Framework courses from sites like Udemy and Coursera. it's completely free and all you need to do is create an account to enroll in this course.

1 comment:

Feel free to comment, ask questions if you have any doubt.