Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If a Controller has a @Controller annotation, shouldn't that be enough for Spring without component scanning?

According to the Spring docs, to enable autodetection 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 a Controller has an annotation to already indicate what it is, shouldn't that be enough for Spring without component scanning?

like image 715
Bowe Avatar asked Dec 06 '25 09:12

Bowe


1 Answers

How else would Spring find the classes? If you haven't told Spring to look in a certain class or package, those classes aren't going to get loaded, and Spring is never going to find them.

This is more a limitation of the java classloading model (if you can call it a limitation), then it is a limitation of Spring.

like image 166
skaffman Avatar answered Dec 08 '25 22:12

skaffman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!