In this article, we will see @ComponentScan example in spring boot.
- @ComponentScan introduced in Spring 3.1, available in org.springframework.context.annotation package.
- In XML based configuration we suppose to write <context:component-scan>, @ComponentScan annotation behaves same way.
- Using this annotation we tell to Spring scan these packages and available classes.
- Generally, we use this annotation with the configuration class.
Let’s see an example, how to use this annotation and what will problem without it.
open eclipse and create maven project, Don’t forget to check ‘Create a simple project (skip)click on next.
Open eclipse and create maven project, Don’t forget to check ‘Create a simple project (skip)’click on next. Fill all details(GroupId – ComponentScanExample, ArtifactId – ComponentScanExample and name – ComponentScanExample) and click on finish.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>ComponentScanExample</groupId> <artifactId>ComponentScanExample</artifactId> <version>0.0.1-SNAPSHOT</version> <name>ComponentScanExample</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
Define classes.
Book.java
package com.componentscan.entity; public class Book { private int bookId; private String bookName; public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public int getBookId() { return bookId; } public void setBookId(int bookId) { this.bookId = bookId; } }
BokController.java
package com.componentscan.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.componentscan.entity.Book; @RestController @RequestMapping("/book") public class BookController { @RequestMapping(value = "/getBook",method = RequestMethod.GET) @ResponseBody public Book getBookDetails() { Book book = new Book(); book.setBookId(1); book.setBookName("rich dad poor dad"); return book; } }
SpringMain.java
package com.componentscan.main; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @ComponentScan(basePackages={"com.componentscan"}) @EnableAutoConfiguration public class SpringMain { public static void main(final String[] args) { SpringApplication.run(SpringMain.class, args); } }
Run the SpringMain.java class and test from the postman.
We are using @ComponentScan annotation with SpringMain.java class. Although in real time scenario generally, we write with configuration class. So what will happen if we remove this annotation attribute? Something like below –
@Configuration
//@ComponentScan(basePackages={“com.componentscan”})
@EnableAutoConfiguration
public class SpringMain {
public static void main(final String[] args) {
SpringApplication.run(SpringMain.class, args);
}
}
While deployment we will not have any issue. But if you try to hit same URI from the postman we will get 404 found.
So what’s going on? Since we have defined our controller classes and pojo(Book.java) in different packages Spring is not able to identify those classes. If we have a different package structure for the controller, service, and repository then we must need to tell spring explicitly using @ComponentScan annotation. That’s all about @ComponentScan example in spring boot.
You may like.
- @RestController and @Controller annotation example in Spring Boot.
- @RequestMapping annotation example In Spring Boot.
- @RequestBody and @ResponseBody annotation example in Spring Boot.
- @PathVariable and @RequestParam annotations in Spring Boot.
- @RequestHeader annotation example by using Spring Boot.
- @SpringBootApplication annotation example in Spring Boot.
- @Component, @Controller, @Service and @Repository annotations example using Spring Boot.
- @Configuration annotation example using spring boot.
@ComponentScan annotation docs.