If the controller has an @Controller annotation, is this not enough for Spring without scanning components?

According to Spring docs , to enable automatic detection of annotated controllers, you add component scanning to your configuration:

<context:component-scan base-package="org.springframework.samples.petclinic.web"/>

My question is: why is this necessary?

If the controller has an annotation to already indicate what it is, should this not be enough for Spring without scanning components?

+3
source share
3 answers

How else can Spring find classes? Unless you told Spring to look in a specific class or package, these classes are not going to load, and Spring will never find them.

Java ( ), Spring.

+5

:

< : -/ >

, , , .

, -config . , , .

, , " ".

//EDIT//

:

< context: annotation-config/" > beans , . , WebApplicationContext DispatcherServlet, @Autowired beans , .

+3

@Controller , , Spring. , .. , , , , bean Spring.

+1

Source: https://habr.com/ru/post/1763268/


All Articles