Can you quote from the books why @RestController wouldn't be a stereotype?
If they say it's because it's not in the org.springframework.stereotype package, that's a really silly reason.
Spring doesn't explicitly define what a stereotype is, but I'd call any annotation a Spring stereotype if that annotation is itself annotated with a stereotype annotation.
@Controller, @Repository and @Service are annotated with @Component, so they are stereotypes. @RestController is annotated with @Controller, so it's a stereotype.
posted 1 month ago
I'm studying for the Spring professionnal certification and this is one of the question they provide in their study guide :
"Is @Controller a stereotype? Is @RestController a stereotype?"
And those are the answers I found in 2 books that were recommanded to me to study this certification :
1."You might think at this point, since @RestController is a specialization of @Controller, this makes it a stereotype annotation, right?
Wrong. First, there are five stereotype annotations in Spring: @Component, @Repository, @Service, @Indexed and @Controller and they are grouped under the org.sprigframework.stereotype package.
All of them, except @Indexed are meta-annotated with a single Spring annotation, which means that the meta-annotated annotation can be treated the same as the annotation it is annotated with.
They are part of the stereotype package and have a fundamental role in declaring beans.
@Indexed is a special case because it was added in Spring 5 to indicate that the annotated element represents a stereotype for the index.
This is useful when classpath scanning is replaced with reading a metadata file generated at compile time for identifying bean types.
Anyway, @RestController is different because it is a composed annotation that is meta-annotated with @Controller and @ResponseBody.
Because of that @RestController cannot be treated the same way as @Controller and thus it cannot be a stereotype annotation if it cannot be treated the same as one."
Pivotal Certified Professional Core Spring 5 Developer Exam A Study Guide Using Spring Framework 5, written by Iuliana Cosmina and published by Apress.
2. "Is @Controller a stereotype? Is @RestController a stereotype?
According to the first reference of this section, the @Controller is a stereotype annotation.
In addition, it is located in the org.springframework.stereotype package.
The @RestController annotation is a stereotype annotation, given that its declaration is annotated with the @Controller annotation."
Core Spring 5 Certification in Detail, written by Ivan Krizsan
And since it is a question I might get on my exam, i'm quite puzzled by the different answers I got.
Stephan van Hulst
posted 1 month ago
Pivotal Certified Professional Core Spring 5 Developer Exam A Study Guide Using Spring Framework wrote:Anyway, @RestController is different because it is a composed annotation that is meta-annotated with @Controller and @ResponseBody.
Because of that @RestController cannot be treated the same way as @Controller and thus it cannot be a stereotype annotation if it cannot be treated the same as one.
I really don't see from what premises the author draws their conclusion.
Nowhere does it say that an annotation is not a stereotype if it can't be treated the same way as @Controller specifically. If that were true, then @Repository and @Service wouldn't by stereotypes either. The author also uses circular reasoning: @RestController is not a stereotype because it can't be treated as a stereotype. By that logic, if I treat it as a stereotype then it's a stereotype.
Spring uses the word "stereotype" very loosely. Basically they say that annotations that indicate that a certain class fulfills a certain role, and cause that class to be discovered by Spring's component scanning, can be treated as stereotypes. @RestController falls squarely within that line of thinking. It clearly indicates that a class fulfills the role of a REST controller, and because it's meta-annotated with @Controller, it causes the class to be picked up by Spring's component scanning.
Personally I would consider any annotation that is (indirectly) meta-annotated with @Component to be a stereotype.
posted 1 month ago
I was really confused by that reasonning aswell. Glad I am not the only one.
Thank you so much for your answer.