Stereotype Annotations in Spring

In this tutorial we would discuss about the Stereotype Annotations in Spring. Stereotype Annotations in Spring are @Repository, @Service and @Controller and @Component is generic stereotype for any Spring-managed component. In the previous version Spring 2.0 introduce the first Stereotype Annotations name as @Repository. The @Component annotations introduced in Spring 2.5 are really just a continuation of the "stereotype" annotations introduced in Spring 2.0. Stereotype annotations are markers for any class that fulfills a role within an application. This helps remove, or at least greatly reduce, the Spring XML configuration required for these components.

These annotations are used to stereotype classes with regard to the application tier that they belong to. Classes that are annotated with one of these annotations will automatically be registered in the Spring application context if <context:component-scan> is in the Spring XML configuration(spring.xml).

The Four Types of Spring Stereotype Components and Their Purposes:
+------------+-----------------------------------------------------+
| Annotation | Meaning                                             |
+------------+-----------------------------------------------------+
| @Component | generic stereotype for any Spring-managed component |
| @Repository| stereotype for persistence layer                    |
| @Service   | stereotype for service layer                        |
| @Controller| stereotype for presentation layer (spring-mvc)      |
+------------+-----------------------------------------------------+

Target:

          Class
Description:

@Component is a generic stereotype for any Spring-managed component. @Repository, @Service, and @Controller are specializations of @Component for more specific use cases, for example, in the persistence, service, and presentation layers, respectively.

Stereotype Annotations in Spring


@Component
public class Circle
{
    private Point center;
    ----
}

Annotation @Repository:

Target:

          Class
Description:
In Spring 2.0 and later, the @Repository annotation is a marker for any class that fulfills the role or stereotype (also known as Data Access Object or DAO) of a repository. Among the uses of this marker is the automatic translation of exceptions.
A class that serves in the persistence layer of the application as a data access object (DAO), otherwise known as a repository in some other technologies. Annotate all your DAO classes with @Repository. All your database access logic should be in DAO classes.
@Repository
public class CircleDaoImpl implements CircleDao
{
    private Point center;
    ----
}

Annotation @Service:

Target:

          Class
Description:
Annotate all your service classes with @Service. All your business logic should be in Service classes.
@Service
public class CircleServiceImpl implements CircleService
{
    private Point center;
    ----
}

Annotation @Controller:

Target:

          Class
Description:
The @Controller is a class level annotation, which indicates that the annotated class is a Spring component of type "controller".
The @Controller annotation indicates that a particular class serves the role of a controller. Spring does not require you to extend any controller base class or reference the Servlet API. However, you can still reference Servlet-specific features if you need to. In Spring MVC you can make controller class very easily by prefixing @Controller before any class declaration.
@Controller
public class CircleController
{
    private Point center;
    ----
}




No comments:

Post a Comment