Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantages of using spring stereotypes?

Tags:

I am developing a web application using spring-mvc.

Now the @Controller, @Service and @Repository stereotypes are available.

I found @Controller particulary useful, specially because I am using

<context:component-scan base-package="my.cool.controller"/> 

Now, regarding @Service and @Repository, so far looks like

  1. The exceptions are better handled if the class is annotated with the correct stereotype, ok, that is an advantage I acknowlegde
  2. I could use component-scan for services and DAOs/repositories, however I do not like the idea of using component-scan, since it slows the startup time of the application, and that is a key feature for me (even if it is only 1 sec and I redeploy once per week)

So, apart from the better exceptions, any other advantage at all? Does annotating classes have an impact on performance?

like image 809
Juan Antonio Gomez Moriano Avatar asked Apr 17 '13 04:04

Juan Antonio Gomez Moriano


People also ask

What is the use of stereotype annotations in Spring?

The Spring Framework provides you with some special annotations. These annotations are used to create Spring beans automatically in the application context. The main stereotype annotation is @Component .

What are stereotypes in Java?

A stereotype allows a framework developer to identify such a role and declare some common metadata for beans with that role in a central place. A bean may declare zero, one or multiple stereotypes, by applying the stereotype annotation to the bean class or producer method or field.

Is @controller a stereotype?

Spring @Component, @Repository, @Service and @Controller are Stereotype Annotations. @Component is generic stereotype annotation for any Spring-managed component. In the previous version Spring 2.0 introduce the first Stereotype Annotations name as @Repository.

What is the purpose of @bean annotation?

Spring @Bean Annotation is applied on a method to specify that it returns a bean to be managed by Spring context. Spring Bean annotation is usually declared in Configuration classes methods. In this case, bean methods may reference other @Bean methods in the same class by calling them directly.


2 Answers

Explanation of stereotypes :

  • @Service - Annotate all your service classes with @Service. This layer knows the unit of work. All your business logic will be in Service classes. Generally methods of service layer are covered under transaction. You can make 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.
  • You don't 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).

Now, Practically performance impact of using context xml beans & annotations is the same. Component scanning is a bit more expensive (when you scan for @Service, @Component). The annotations are 'parsed' with reflection, the xml - with an xml parser. But, as you said, it is startup-time - it happens only once. And on a moderate machine it starts pretty quickly even with annotations.

like image 174
Jeevan Patil Avatar answered Sep 20 '22 20:09

Jeevan Patil


Component scan saves you from defining each bean manually via xml or java configuration.

Multiple stereo types are there to define layers like service layer, data layer, etc. Also based on different stereo types if you want to do something specific then you can do so.

like image 20
Bhushan Bhangale Avatar answered Sep 19 '22 20:09

Bhushan Bhangale