Friday, May 17, 2024
HomeJavaDistinction between @Element, @Service, @Controller, and @Repository in Spring

Distinction between @Element, @Service, @Controller, and @Repository in Spring


Earlier than you study the distinction between @Element, @Service, @Controller, and @Repository annotations within the Spring framework, it is vital to know the position of @Element annotation in Spring. In the course of the preliminary launch of Spring, all beans are was once declared in an XML file. For a big mission, this rapidly turns into an enormous job, and Spring guys acknowledge the issue somewhat rapidly. In later variations, they supply annotation-based dependency injection and Java-based configuration. From Spring 2.5 annotation-based dependency injection was launched, which robotically scans and registers courses as Spring bean which is annotated utilizing @Element annotation.

This implies you do not declare that bean utilizing the <bean> tag and inject the dependency, will probably be carried out robotically by Spring. This performance was enabled and disabled utilizing <context:component-scan> tag.

Now that you already know what does @Element annotation does let’s have a look at what does @Service, @Controller, and @Repository annotation do.

They’re nothing however the specialised type of @Element annotation for sure conditions. As a substitute of utilizing @Element on a controller class in Spring MVC, we use @Controller, which is extra readable and applicable.

By utilizing that annotation we do two issues, first, we declare that this class is a Spring bean and must be created and maintained by Spring ApplicationContext, but in addition we point out that its a controller in MVC setup. This latter property is utilized by web-specific instruments and functionalities.

For instance, DispatcherServlet will search for @RequestMapping on courses which can be annotated utilizing @Controller however not with @Element.

This implies @Element and @Controller are the identical with respect to bean creation and dependency injection however later is a specialised type of former. Even in the event you exchange @Controller annotation with @Compoenent, Spring can robotically detect and register the controller class however it might not work as you anticipate with respect to request mapping. 

@Component vs  @Service vs  @Controller, and @Repository annotation in Spring

The identical is true for @Service and @Repository annotation, they’re a specialization of @Element in service and persistence layer. A Spring bean within the service layer must be annotated utilizing @Service as a substitute of @Element annotation and a spring bean within the persistence layer must be annotated with @Repository annotation.

By utilizing a specialised annotation we hit two birds with one stone. First, they’re handled as Spring bean, and second, you possibly can put particular habits required by that layer.

For instance, @Repository’s not solely serving to in annotation based mostly configure but in addition catch Platform-specific exceptions and re-throw them as one in all Spring’s unified unchecked exception.

Although for that you just additionally must declare org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor as Spring bean in your utility context.

This bean post-processor provides an advisor to any bean that’s annotated with @Repository in order that any platform-specific exceptions are caught after which rethrown as one in all Spring’s unchecked information entry exceptions. 

That is additionally one of many continuously requested Spring Interview Query and a preferred idea from the Spring certification perspective. You will discover a few questions based mostly on these annotations and their utilization within the Spring skilled certification examination too.

How does Element Scanning work in Spring?

From Spring 2.0, Spring gives <context:component-scan> and annotation-driven dependency injection to robotically detect and register Spring bean as a substitute of specifying them within the XML file.

However, it solely scans @Element and doesn’t search for @Controller, @Service, and @Repository on the whole. They’re scanned as a result of they themselves are annotated with @Element.

Simply check out @Controller, @Service, and @Repository annotation definitions:

@Element
public @interface Service {
….
}




@Element
public @interface Repository {
….
}




@Element
public @interface Controller {

}

Thus, it’s not unsuitable to say that @Controller, @Service, and @Repository are particular sorts of @Element annotation. <context:component-scan> picks them up and registers their following courses as beans, simply as in the event that they have been annotated with @Element.

They’re scanned as a result of they themselves are annotated with @Element annotation. If you happen to outline your individual customized annotation and annotate it with @Element, then it should additionally get scanned with <context:component-scan>.

If you wish to study extra about dependency injection, auto-wiring, and several types of configuration in Spring e.g. XML based mostly, annotation-based, and Java configuration in Spring, the documentation of Spring is basically nice useful resource. 

Difference between @Component, @Service, @Controller, and @Repository in Spring

Distinction between @Element, @Service, @Controller, and @Repository in Spring

Here’s a good abstract of what does @Element, @Service, @Controller, and @Repository annotation do in Spring Framework:

  1. @Element is a generic stereotype for any Spring-managed element or bean. 
  2. @Repository is a stereotype for the persistence layer.
  3. @Service is a stereotype for the service layer.
  4. @Controller is a stereotype for the presentation layer (spring-MVC).

And right here is the great diagram to clarify the hierarchy of all these annotations in Spring Framework:

Difference between @Component, @Service, @Controller, and @Repository in Spring

That is all in regards to the distinction between @Element, @Controller, @Service, and @Repository in Spring Framework. As I stated, all of them are used to auto-detect Spring beans when context scanning is enabled and basically present the identical performance with respect to dependency injection.

Their solely distinction is available in their function i.e. @Controller is utilized in Spring MVC to outline controller, that are first Spring bean after which the controller. Equally, @Service is used to annotated courses that maintain enterprise logic within the Service layer and @Repository is used within the Knowledge Entry layer.

In brief, you need to use essentially the most applicable annotation based mostly upon which layer that specific class belongs to.

Different Spring Framework articles and interview questions chances are you’ll like



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments