2017  Kodetalk | Feedback | Privacy Policy | Terms | About

What is stereotypes in spring framework?

What is stereotypes in spring framework? what are the advantages of using the stereotype annotations in spring.
Basically stereotypes are the Annotations denoting the roles of types or methods in the overall architecture (at a conceptual, rather than implementation level).

For example:

@Service - Basically the business layer conceptually acts as service layer which in spring annotate as @Service. This layer basically covered under transaction which makes multiple DAO calls from service method, if one transaction fails all transactions should rollback.

@Repository - Annotate all your DAO classes with @Repository. All your database access logic should be in DAO classes.

@Component - Annotate your other components (for example REST resource classes) with component stereotype.

@Autowired - Let Spring auto-wire other beans into your classes using @Autowired annotation.

@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.

Reasons to use them :

The main advantage of using @Repository or @Service over @Component is that it's easy to write an AOP pointcut that targets, for instance, all classes annotated with @Repository. We need not to have to write bean definitions in context xml file. Instead annotate classes and use those by autowiring. Specialized annotations help to clearly demarcate application layers (in a standard 3 tiers application).

Answer is