[转]what’s the difference between @Component ,@Repository & @Service annotations in Spring

原文地址:https://www.cnblogs.com/softidea/p/6070314.html

@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



总的来说就是 @Service, @Controller , @Repository = {@Component + 特殊的功能} ,文章提到我们应该结合Spring中到的切面编程思想(AOP), 我们的controller 承担着分发请求的任务后,又要处理业务逻辑,同时还要与数据库持久层作交互,这样的代码是糟糕的,所以文档中主张是使用这几个注解类,更好地区分开各自的功能

  • @Component : 将自动扫描组件
  • @Repository :  指示为在持久层的Dao层的组件(它的好处是捕抓到持久层交互中出现的异常,并把异常友好地表示出来,假如没有这个注释,数据库抛出的异常常常难以理解)
  • @Service : 业务逻辑
  • @Controller : 分发请求

原文地址:https://www.cnblogs.com/Benjious/p/9307736.html

时间: 2024-08-14 01:11:38

[转]what’s the difference between @Component ,@Repository & @Service annotations in Spring的相关文章

What&#39;s the difference between @Component, @Repository &amp; @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&qu

SpringAnnotation注解之@Component,@Repository,@Service,@Controller

@Component:组件,表示此写上了此注解的bean,作为一个组件存在于容器中.这样的话别的地方就可以使用@Resource这个注解来把这个组件作为一个资源来使用了.初始化bean的名字为类名首字母小写 与@Component注解功能相同的注解有:@Repository,@Service,@Controller,@Component ,默认情况下Spring认为这4个注解会被认为是一个组件. @Repository:数据层,一般放在Dao接口的实现类上 @Service:服务层,一般放在se

@Component @Repository @Service @Controller

Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层.业务层和控制层(Web 层)相对应.虽然目前这3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能.所以,如果 Web 应用

Spring注解 Component Repository Service Controller区别

Spring 4.1 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层.业务层和控制层(Web 层)相对应.虽然目前这 3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能.所以,如果 Web 应

@Component, @Repository, @Service的区别

注解 含义 @Component 最普通的组件,可以被注入到spring容器进行管理 @Repository 作用于持久层 @Service 作用于业务逻辑层 @Controller 作用于表现层(spring-mvc的注解) 原文地址:https://www.cnblogs.com/shihuibei/p/9639633.html

@Component、@Service、@Constroller

@Component.@Service.@Constroller,@Repository,它们分别用于软件系统的不同层次: @Component 是一个泛化的概念,仅仅表示一个组件 (Bean) ,可以作用在任何层次. @Service 通常作用在业务层,但是目前该功能与 @Component 相同. @Constroller 通常作用在控制层,但是目前该功能与 @Component 相同 @Repository通常用于将数据访问层 (DAO 层 ) 的类标识为 Spring Bean 为了让

@Repository @Service 和@Autowired 的使用

解释: @Controller 声明Action组件 @Service   声明Service组件    @Service("myMovieLister") @Repository 声明Dao组件 @Autowired 用于注入 配置方位: @Repository ->(XXDaoImpl) , @Service("UserService")->(XXServiceImpl) ,  @Autowired ->(private UserDao;) 注

@Repository注解会导致spring boot生成一个动态代理

用了一个@Repository注解,本意是避免了idea开发工具提示接口 unused,但是导致了一个后果,spring boot自动把该接口包装成了动态代理类,里面才是mybatis生成的动态代理类.项目框架内用到了hdl = Proxy.getInvocationHandler(this.baseMapper); 来获取真实的DAO层接口,导致了获取不到,取到的是Jdk的动态代理类 下面是basemapper的代理实现类. [email protected], 它的InvocationHan

Difference between boot ip. service ip and persistent ip in hacmp

- boot IP is the original address on a network interface even when the cluster is down - service IP is a movable IP that will be added to a network interface when a resource group becomes online. Clients normally should connect to service IP. If a re