The Spring framework provides you with some special annotations. These annotations are used to create Spring beans automatically in the application context. The stereotype annotations in spring are @Component, @Controller, @Service & @Repository.
The main stereotype annotation is @Component.
“Annotations denoting the roles of types or methods in the
overall architecture (at the conceptual, rather than implementation, level)”
from java docs.
There are some stereotype meta-annotations which is derived
from @Component annotation.
Meta Annotation: - Generally in java, an annotation is
termed as meta-annotation if it is used on another annotation.
@Controller: - Used to create spring beans at the Controller
layer.
@Service: - Used to create spring bean at the Service
Layer. We mark beans @Service to indicate that they are holding business logic.
Besides being used in the service layer, there is not any special use for this
annotation.
@Repository: - Used to create a spring bean at the Persistence Layer, which will act as a database repository at the DAO layer. Its job is to catch persistence-specific exceptions and re-throw them as one of the Spring’s unified unchecked exceptions.
To detect these beans automatically, spring uses classpath
scanning annotations.
Then it registers each bean in the ApplicationContext.
The major difference between these stereotypes is that they
are used for different classifications. When we annotate a class for auto
detection, we should use the respective stereotype.
No comments:
Post a Comment