Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

0gam

ComponentScan 본문

Spring Framework

ComponentScan

byeon0gam 2018. 5. 23. 11:23


@ComponentScan

If you understand component scan, you understand Spring.

Spring is a dependency injection framework. It is all about beans and wiring in dependencies.

The first step of defining Spring Beans is by adding the right annotation - @Component or @Service or @Repository.

However, Spring does not know about the bean unless it knows where to search for it.

This part of “telling Spring where to search” is called a Component Scan.

You define the packages that have to be scanned.

Once you define a Component Scan for a package, Spring would search the package and all its sub packages for components/beans.

Defining a Component Scan

  • If you are using Spring Boot, check configuration in Approach 1.
  • If you are doing a JSP/Servlet or a Spring MVC application without using Spring Boot use Approach 2.

Approach 1 : Component Scan in a Spring Boot Project

Executive Summary

  • If your other packages hierarchies are below your main app with the @SpringBootApplication annotation, you’re covered by implicit components scan.
  • If there are beans/components in other packages which are not sub packages of the main package, you should manually add them as @ComponentScan

Approach 2: Non Spring Boot Project

In a non Spring Boot Project, we would typically define the component scan explicitly in an XML application context or a Java Application Context.


Reference : http://www.springboottutorial.com/spring-boot-and-component-scan


'Spring Framework' 카테고리의 다른 글

Spring exception handling  (0) 2018.05.25
spring security session-registry  (0) 2017.02.08
@Controller , @RestController  (0) 2016.09.20
produces, consumes  (0) 2016.07.27
@JsonIgnoreProperties(ignoreUnknown = true)  (0) 2016.07.07