Oct 15 2017

Spring Stereotype Annotations

Category: MiscellaneousIuliana @ 21:18

Yes, finally a technical post. ;)

Having finished writing my third  Spring book, it was about time I should start writing technical posts as well. Being an accessible online person, quite a few people that read my books find mistakes or have interesting questions. I am really happy when people find mistakes, this means that they are actually reading the books, taking them seriously, including reading the references to the official documentation, which is more on point and more detailed than a book written by an “external” will ever be.

And I am happier when questions that question my own understanding of the framework are risen as well. Because this motivates me to dig deep into the documentation, to ask other technical people I know what their opinion is. It is an opportunity for communication and debate.

The last question I had from a reader was about the @Configuration annotation. He asked why is this annotation not mentioned as being a stereotype annotation in the book and if this is not a mistake on my part. He gave me some links to some official documentation and his opinion about the matter. After I queried  multiple resources, including my technical reviewer, who is a Spring  trainer for Pivotal this is the answer I came up with.

The first stereotype annotation that is introduced in any Spring tutorial, study guide and the official documentation is the @Component. The annotation is declared in the org.springframework.stereotype package that was created to contain annotations denoting the roles of types or methods in the overall architecture (at a conceptual, rather than implementation, level).

According to the documentation a stereotype annotation is a marker for any class that fulfills the role or stereotype of a specific component.

The org.springframework.stereotype package contains the following annotation declarations:

  • @Component
  • @Repository
  • @Controller
  • @Service

The @Component annotation is the general Spring annotation used to create bean declarations. The other three are specializations of it. For example, @Repository is a marker for any class that fulfills the role or stereotype of a “Repository”, originally defined by Domain-Driven Design (Evans, 2003) as “a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects”.

The @Component annotation is also a meta-annotation, which means it can be applied on other annotations. This is useful mostly because annotations meta-annotated with @Component are eligible for component scanning, thus the container recognizes classes annotated with them as bean declarations.

In the official reference documentation, only the four above are mentioned as stereotype annotations, but there is a misconception that any annotation meta-annotated with @Component is a stereotype annotation.

Aside from the three specializations of @Component that were already mentioned, there are currently 4 more specializations meta-annotated with @Component or a specialization of it:

  •  org.springframework.context.annotation.Configuration
  • org.springframework.web.bind.annotation.RestController (annotated with @Controller)
  • org.springframework.web.bind.annotation.ControllerAdvice (annotated with @Component)
  • org.springframework.web.bind.annotation.RestControllerAdvice (annotated with @ControllerAdvice)

The last three could be considered stereotype annotations, although they are not mentioned in the documentation either, but @Configuration is a different kind of animal.

According to the Spring Reference Documentation we could consider that @Configuration is a marker for any class that fulfills the role or stereotype of a configuration class. @ComponentScan is able to detect it according a technical perspective because it has @Component, but semantically would has no sense, because a bean of type configuration will rarely be used for anything else than bean declaration.

Thus, @Configuration is in the edge in some way. It can be considered a kind of stereotype, but only for
infrastructure.

There is also another thing to consider here: the org.springframework.stereotype package and its description in the Spring JavaDoc. It might just be that only the annotations is this package are considered stereotype annotations. Maybe it’s all about categorization, and @Configuration is not considered a stereotype annotation because it’s not in that package and is also used for configuration, not for declaring a specific type of component.

So, bad news is that the situation is fuzzy when it comes to @Configuration. The good news: unless you are using my Spring books to learn Spring, the situation to think about it being a stereotype annotation or not will never come up. Just stick to the official documentation and you should be fine.

Stay safe, stay happy!

Tags: ,

Leave a Reply