What's the difference between @Component, @Repository & @Service annotations in Spring?

 

 

@Component is equivalent to

<bean>

@Service, @Controller , @Repository = {@Component + some more special functionality}

That mean Service,Controller and Repository are functionally the same.

The three annotations are used to separate "Layers" in your application,

  • Controllers just do stuff like dispatching, forwarding, calling service methods etc.
  • Service Hold business Logic, Calculations etc.
  • Repository are the DAOs(Data Access Objects), they access the database directly.

Now you may ask why separate them:(I assume you know AOP-Aspect Oriented Programming)

Lets say you want to Monitors the Activity of the DAO Layer only. You will write an Aspect(A class) class that does some logging before and after every method of your DAO is invoked, you are able to do that using AOP as you have three distinct Layers and are not mixed.

So you can do logging of DAO "around", "before" or "after" the DAO methods. You could do that because you had a DAO in the first place. What you just achieved is Separation of concerns or tasks.

Imagine if there were only one annotation @Controller, then this component will have dispatching, business logic and accessing database all mixed, so dirty code!

Above mentioned is one very common scenario, there are many more use cases of why to use three annotations.

 

In Spring @Component@Service, and @Controller@Component are Stereotype annotations which is used for:

@Controller: where your request mapping from presentation page done i.e. Presentation layer won't go to any other file it goes directly to @Controller class and check for requested path in @RequestMapping annotation which written before method calls if necessary.

@Service: All business logic is here i.e. Data related calculations and all.This annotation of business layer in which our user not directly call persistence method so it will call this methods using this annotation. It will request @Repository as per user request

@Repository:This is Persistence layer(Data Access Layer) of application which used to get data from database. i.e. all the Database related operations are done by repository.

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

 

 

From Spring Documentation:

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.

Spring 2.5 introduces further stereotype annotations: @Component, @Service, and @Controller. @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.

Therefore, you can annotate your component classes with @Component, but by annotating them with @Repository, @Service, or @Controller instead, your classes are more properly suited for processing by tools or associating with aspects. For example, these stereotype annotations make ideal targets for pointcuts.

Thus, if you are choosing between using @Component or @Service for your service layer, @Service is clearly the better choice. Similarly, as stated above, @Repository is already supported as a marker for automatic exception translation in your persistence layer.

| 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)      |

 

 

Spring 2.5 introduces further stereotype annotations: @Component, @Service and @Controller. @Component serves as a generic stereotype for any Spring-managed component; whereas, @Repository, @Service, and @Controller serve as specializations of @Component for more specific use cases (e.g., in the persistence, service, and presentation layers, respectively). What this means is that you can annotate your component classes with @Component, but by annotating them with @Repository, @Service, or @Controller instead, your classes are more properly suited for processing by tools or associating with aspects. For example, these stereotype annotations make ideal targets for pointcuts. Of course, it is also possible that @Repository, @Service, and @Controller may carry additional semantics in future releases of the Spring Framework. Thus, if you are making a decision between using @Component or @Service for your service layer, @Service is clearly the better choice. Similarly, as stated above, @Repository is already supported as a marker for automatic exception translation in your persistence layer.

@Component – Indicates a auto scan component.
@Repository – Indicates DAO component in the persistence layer.
@Service – Indicates a Service component in the business layer.
@Controller – Indicates a controller component in the presentation layer.

 reference :- http://static.springsource.org/spring/docs/3.0.0.M3/reference/html/ch04s12.html

http://stackoverflow.com/questions/25633477/security-configuration-with-spring-boot?rq=1

 

时间: 2024-11-16 18:00:01

What&#39;s the difference between @Component, @Repository & @Service annotations in Spring?的相关文章

请教下关于SSH框架中@Repository/@Service/@Controller注入的问题!

问题描述 各位大大,请问个问题:我在持久层.业务层和控制层中分别使用@Repository/@Service/@Controller这样意义和直接全部使用@Component有什么区别呢? 问题补充:myali88 写道 解决方案 兄弟 那说明我刚才给你说的那个配置 应该是没问题的 这个问题应该是解决了的.解决方案二:我刚才之所以让你 前面加个resource这个东西 是因为你说是同目录,后来发觉肯定是你们头把这个目录和src目录都可以被映射成同一级 相对目录,所以让你改成这样.应该没问题了.解

spring学习笔记(3)Bean命名、定义与配置

基于xml的配置 基础配置 <bean id="id" name="name" class="full_name"> <property name="pname" value="pvalue" lazy-init="defalut/true/false" scope="singleton/prototype/request/session" />

context:component-scan base-package

Element : component-scan Scans the classpath for annotated components that will be auto-registered as Spring beans. By default, the Spring-provided @Component, @Repository, @Service, and @Controller stereotypes will be detected. Note: This tag implie

Spring Task Scheduler - No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined

1. Overview In this article, we are discussing the Springorg.springframework.beans.factory.NoSuchBeanDefinitionException – this is a common exception thrown by the BeanFactory when trying to resolve a bean that simply isn't defined in the Spring Cont

SpringMVC四大注解 Component&amp;#183;Controller&amp;#183;Service&amp;#183;Repository

如何使用Component\Controller\Service\Repository四大注解类: •@Component 是通用标注 •@Controller 标注 web 控制器 •@Service 标注 Servicec 层的服务 •@Respository 标注 DAO 层的数据访问 •这些注解都是类级别的,可以不带任何参数,也可以带一个参数,代表bean名字,在进行注入的时候就可以通过名字进行注入了. •在不清楚使用那个注解的时候,可以统统使用Component •为了代码逻辑清晰,还

《Spring Data实战》——2.3 定义Repository

2.3 定义Repository 到目前为止,我们看到了带有查询方法的Repository接口,这些查询有的是从方法名中衍生出来的,有的是手动声明的,这取决于Spring Data为实际存储类型所提供的使用方式.为了衍生出这些查询,我们必须扩展Spring Data的特定标识接口:Repository.除了查询以外,在你的Repository中还需要一些其他的功能:存储对象,删除对象,根据ID进行查找,返回所有存储的实体或按页对它们进行访问.通过Repository接口来暴露这些功能的最简单方式

Red Gate系列之二 SQL Source Control 3.0.13.4214 Edition 数据库版本控制器 完全破解+使用教程

原文:Red Gate系列之二 SQL Source Control 3.0.13.4214 Edition 数据库版本控制器 完全破解+使用教程 Red Gate系列之二 SQL Source Control 3.0.13.4214 Edition 数据库版本控制器 完全破解+使用教程 Red Gate系列文章: Red Gate系列之一 SQL Compare 10.2.0.1337 Edition 数据库比较工具 完全破解+使用教程 Red Gate系列之二 SQL Source Cont

Spring-AOP实践 - 统计访问时间--StopWatch

本文将介绍使用注解+AOP(面向切面编程)的方式实现其功能,一来可以快速使用StopWatch功能,二来让大家熟悉一下如何使用注解进行切面编程_ 1. 自定义一个注解 StopWatchTime @Retention(RetentionPolicy.RUNTIME) @Target({METHOD}) public @interface StopWatchTime { String value() default ""; } 本注解主要用在方法上,故target为METHOD,并设定一

SSM框架理解(转)

SSM框架理解 最近两星期一直在学JavaEE的MVC框架,因为之前学校开的JavaEE课程就一直学的吊儿郎当的,所以现在真正需要掌握就非常手忙脚乱,在此记录下这段时间学习的感悟,如有错误,希望大牛毫不客气地指出. SpringSpring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象.Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地`new`一个对象,而是让Spring框架帮你来完成这一切. SpringMVCSp