在Spring@Component vs @Repository vs @Service
介绍:
借助Spring的自动扫描功能,它可以自动检测我们的应用程序中定义的各种bean。 我们通常使用可用的Spring注释之一来注释我们的bean-@ Component,@ Repository,@ Service,@ Controller 。
在检测到bean时,Spring只需将其注册到ApplicationContext中 。
在本快速教程中,我们将研究@ Component,@ Repository和@Service Spring批注之间的区别。
我们可以使用@Component注释将bean标记为Spring管理的组件。 换句话说, 它是任何Spring托管组件的通用构造型 。
我们可以使用<context:component-scan>标记启用自动扫描。 在自动扫描过程中,Spring将扫描并注册所有标有@Component注释的bean:
@Component
public class Employee {...}
@Repository批注是@Component批注的特化:
@Component
public @interface Repository {
}
由于@Repository是@Component的一种 ,因此Spring也会自动扫描并注册它们。
@Repository是持久层的构造型。 它的工作是捕获所有与持久性相关的异常,并将其作为Spring DataAccessException重新抛出。
为此,我们应该在应用程序上下文中配置PersistenceExceptionTranslationPostProcessor :
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
该bean后处理器将顾问程序添加到所有标有@Repository的 bean。 顾问的职责是将特定于平台的异常转换为Spring统一的未经检查的异常。
就像@Repository一样,@Service是@Component的另一个特化:
@Component
public @interface Service {
}
就像@Repository一样,@Service也是@Component的一种 。 这意味着Spring也将自动检测此类bean。
@Service批注表示我们的bean拥有一些业务逻辑。 到目前为止,它没有提供@Component以外的任何特定行为。
尽管如此, 我们仍应使用@Service注释对服务层bean进行注释,以使意图清晰明了。 此外,我们永远不知道Spring是否会选择为其添加某些特定功能。
NutShell的差异:
让我们快速总结一下@ Component,@ Repository和@Service之间的区别:
- @Component是最通用的构造型,并将bean标记为Spring托管的组件
- @Service和@Repository批注都是@Component批注的特化
- @Repository是用于持久层的构造型。 它将所有与持久性相关的异常转换为Spring的DataAccessException
- @Service用于服务层的bean。 目前,它不提供@Component以外的任何其他功能
- 在适用的情况下,总是最好使用@Repository和@Service批注而不是@Component 。 它更清楚地传达了豆子的意图
结论:
在本微型教程中,我们了解了流行的Spring批注之间的区别– @ Component,@ Service和@Repository。
总而言之,我们应该始终倾向于根据其特定的图层约定使用注释。
翻译自: https://www.javacodegeeks.com/2019/05/component-repository-service-spring.html
在Spring@Component vs @Repository vs @Service相关推荐
- Spring注解@Component、@Repository、@Service、@Controller,@Autowired、@Resource用法
一.Spring定义bean,@Component.@Repository.@Service 和 @Controller Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥 ...
- Spring中的@ Component,@ Repository和@Service批注有什么区别?
@Repository @Component , @Repository和@Service批注可以在Spring中互换使用吗,或者除了充当注解设备外,它们还提供任何特定功能吗? 换句话说,如果我有一个 ...
- Spring @Autowired、@Resource、@Required、@Component、@Repository、@Service、@Controller注解的用法和作用...
Spring @Autowired,@Resource,@Required注解的用法和作用 Spring中 @Autowired标签与 @Resource标签 的区别 Spring注解@Compone ...
- Spring注解的使用步骤,@Component注解创建的对象名称,常用注解Component、Repository、Service以及Controller的区别
1. 注解的使用 1.1 pom.xml文件加入spring-context依赖,间接加入了spring-aop依赖,有aop依赖才能使用注解 1.2 在spring配置文件中加入组件扫描器标签< ...
- Spring学习使用标签来标记资源(@Component、@Repository、 @Service和@Controller)和用法(包括如何jsp正在使用)...
首先,在xml其中新增部分标有下划线的文件,容器初始化的时候需要扫描包 注意: a. 包款扫描(下划线部分)一定要加,默认是不扫描整个包.与每一包之间','开.如过具有同样的父包,那么我们能够 ...
- spring中stereotype注解Component,Repository,Service,Controller
[README] 本文介绍了 spring4.0 下 org.springframework.stereotype 的注解类型,俗称刻板型注解(一成不变型): 包括 @Component, @Repo ...
- Spring中用@Component、@Repository、@Service和 @Controller等标注的默认Bean名称会是小写开头的非限定类名
今天用调度平台去调用bean中的方法时,提示找不到bean.经查,发现是由于如果在标注上没有提供name属性值,则默认的bean名称是小写开头的,而不是大写开头的. 下面是其他文档参阅: 使用过滤器自 ...
- SSM框架---Spring注解@Component、@Repository、@Service、@Controller区别
原文链接 很长时间没做web项目都把以前学的那点框架知识忘光了,今天把以前做的一个项目翻出来看一下发现用·@Component标记一个组件,而网上有的用@Service标记组件,我晕就查了一下资料: ...
- @Component、@Repository、@Service、@Controller区别
@Component.@Repository.@Service.@Controller区别 很长时间没写代码了,把以前学的那点框架知识忘光了,最近在写Java开源商城HapiShop,发现用@Comp ...
最新文章
- 多些时间能少写些代码(转自酷壳 – CoolShell.cn)
- ISA2006实战系列之三:防火墙策略部署深度分析(附图)
- [CLR via C#]25. 线程基础
- Simulink之负载换流式无源逆变电路
- android hdmi开关,Android – 禁用HDMI
- DOM(四)——事件、事件模型(冒泡)与事件对象的功能
- epoll哪些触发模式_epoll的内部实现 百万级别句柄监听 lt和et模式非常好的解释...
- java 对象快速赋值_JavaWeb学习笔记:简单JavaBean对象的快速赋值与获取
- 腹肌锻炼视频(01):四种方法打造完美腹肌
- Zabbix 报告缺少可用的交换空间主机 “Lack of free swap space”问题解决
- ABOV(12) UART
- python连通域提取 原理_连通域的原理与Python实现
- 秋叶收藏集, 动态规划 leetcode LCP 19
- Unity 卡在 Rload Script Assemblies
- 【Linux】一张图让你读懂Linux内核运行原理
- 算法训练 瓷砖铺放 【递归】java
- 从数据孪生发展到元宇宙
- myCPUZ (未完待续)
- (绝对防御勒索病毒)装机员 ghost win7 Sp1 64位纯净6月版
- 原生JavaScript的评分星星
热门文章
- jzoj3846-七天使的通讯【二分图判定】
- 51nod-猴猴吃香蕉【dp】
- jzoj3337-[NOI2013模拟]wyl8899的TLE【字符串hash,二分】
- P3951,jzoj5473-小凯的疑惑【数论】(NOIP2017提高组)
- ssl1776-游乐场【图论,深搜】
- 14、ORACLE下的基本SQL操作
- SpringBoot2.1.9 分布式锁ShedLock不执行坑
- java反射的field.get(null)
- Selector 实现原理
- 一张图弄懂java线程的状态和生命周期