时间: 2019-12-16 19:15:13
标签:
java
@Component, @Repository, @Service的区别
问题
在spring集成的框架中,注解在类上的@Component
,@Repository
,@Service
等注解能否被互换?或者说这些注解有什么区别?
回答1
引用spring的官方文档中的一段描述:
在Spring2.0之前的版本中,@Repository
注解可以标记在任何的类上,用来表明该类是用来执行与数据库相关的操作(即dao对象),并支持自动处理数据库操作产生的异常
在Spring2.5版本中,引入了更多的Spring类注解:@Component
,@Service
,@Controller
。Component
是一个通用的Spring容器管理的单例bean组件。而@Repository
, @Service
, @Controller
就是针对不同的使用场景所采取的特定功能化的注解组件。
因此,当你的一个类被@Component
所注解,那么就意味着同样可以用@Repository
, @Service
, @Controller
来替代它,同时这些注解会具备有更多的功能,而且功能各异。
最后,如果你不知道要在项目的业务层采用@Service
还是@Component
注解。那么,@Service
是一个更好的选择。
就如上文所说的,@Repository
早已被支持了在你的持久层作为一个标记可以去自动处理数据库操作产生的异常(译者注:因为原生的java操作数据库所产生的异常只定义了几种,但是产生数据库异常的原因却有很多种,这样对于数据库操作的报错排查造成了一定的影响;而Spring拓展了原生的持久层异常,针对不同的产生原因有了更多的异常进行描述。所以,在注解了@Repository
的类上如果数据库操作中抛出了异常,就能对其进行处理,转而抛出的是翻译后的spring专属数据库异常,方便我们对异常进行排查处理)。
注解 |
含义 |
@Component |
最普通的组件,可以被注入到spring容器进行管理 |
@Repository |
作用于持久层 |
@Service |
作用于业务逻辑层 |
@Controller |
作用于表现层(spring-mvc的注解) |
回答2
这几个注解几乎可以说是一样的:因为被这些注解修饰的类就会被Spring扫描到并注入到Spring的bean容器中。
这里,有两个注解是不能被其他注解所互换的:
@Controller
注解的bean会被spring-mvc框架所使用。
@Repository
会被作为持久层操作(数据库)的bean来使用
如果想使用自定义的组件注解,那么只要在你定义的新注解中加上@Component
即可:
@Component
@Scope("prototype")
public @interface ScheduleJob {...}
这样,所有被@ScheduleJob
注解的类就都可以注入到spring容器来进行管理。我们所需要做的,就是写一些新的代码来处理这个自定义注解(译者注:可以用反射的方法),进而执行我们想要执行的工作。
回答3
@Component
就是跟<bean>
一样,可以托管到Spring容器进行管理。
@Service, @Controller , @Repository = [email protected] + 一些特定的功能}。这个就意味着这些注解在部分功能上是一样的。
当然,下面三个注解被用于为我们的应用进行分层:
@Controller
注解类进行前端请求的处理,转发,重定向。包括调用Service层的方法
@Service
注解类处理业务逻辑
@Repository
注解类作为DAO对象(数据访问对象,Data Access Objects),这些类可以直接对数据库进行操作
有这些分层操作的话,代码之间就实现了松耦合,代码之间的调用也清晰明朗,便于项目的管理;假想一下,如果只用@Controller
注解,那么所有的请求转发,业务处理,数据库操作代码都糅合在一个地方,那这样的代码该有多难拓展和维护。
总结
@Component
, @Service
, @Controller
, @Repository
是spring注解,注解后可以被spring框架所扫描并注入到spring容器来进行管理
@Component
是通用注解,其他三个注解是这个注解的拓展,并且具有了特定的功能
@Repository
注解在持久层中,具有将数据库操作抛出的原生异常翻译转化为spring的持久层异常的功能。
@Controller
层是spring-mvc的注解,具有将请求进行转发,重定向的功能。
@Service
层是业务逻辑层注解,这个注解只是标注该类处于业务逻辑层。
- 用这些注解对应用进行分层之后,就能将请求处理,义务逻辑处理,数据库操作处理分离出来,为代码解耦,也方便了以后项目的维护和开发。
Stackoverflow链接:
http://stackoverflow.com/questions/6827752/whats-the-difference-between-component-repository-service-annotations-in
拓展
- [email protected]@[email protected]@Controller区别
- [email protected]@Resource区别
相关问答
- Vue开发错误 Failed to execute 'intertBefore' on 'Node'
- Testing InitializrTest & GitHub
- Example on how to implement Searchkick, Using Elasticsearch from AWS in Ruby on Rails
- SilverStripe GridField Components set for bulk upload and bulk record edit, unlink & delete
- http benchmark web app based on vegeta & boom
- Framework to solve Divide and Conquer problems in an easy way, you just need to define the specific Problem characteristics and the Framework will solve it for you with a D&C approach
- An online Knight's tour visualizer using divide and conquer algorithm
- Recursive & Divide and Conquer algorithms
- Mutual benefit & assistance
- Runs RequireJS in Node and Rhino, and used to run the RequireJS optimizer
- pkg download & installation directly on Vita
- Wrappers, tools and additional API's for using ROS with Gazebo
- The extension method for Foundation & UIKit Class
- Openbox on Ubuntu with x11vnc & NoVNC
- Webster's English Dictionary in JSON format, and related Swift parsing utility
- Reverse Shell Using JavaScript & XSS
- Bootstrap Script for migrating systems to Foreman & Katello
- Gradle plugin to generate the jOOQ metamodel from Flyway & Liquibase migrations using a dockerized database
- An Otter Git Raft and also instructions for how to use these resources in Otter
- Builders and buildpacks designed to run on Google Cloud's container platforms
- Starter Code for Auth&Auth course
- A high school portal that can further be modified or expanded to suit any institutions type built with django
- Quick & easy Discord webhook runner for Lua
- Application to calculate summary in Irish pub O'Hooligans
- Sending HTTP Post & Get requests in Java
- Robot Framework's library to test REST interfaces utilizing Apache HttpClient
- Chronicle Bytes has a similar purpose to Java NIO's ByteBuffer with many extensions
- jQuery plugin allows you to easy show & manage business hours
- Scheduled job to clean out Laravel's soft deleted records at configured interval
- Using the Newton's Method to find the Learning Rate
- This repo will help to send push notification to any device if you have fcm device token using php
- FCM Notification with PHP & MySQL for Android
- Visual Docker Container Management on Mac & Windows
- JHipster monitoring & alerting console, based on ELK
- Kibana Alert & Report App for Elasticsearch
- Easy & Flexible Alerting With ElasticSearch
- Reliable PostgreSQL Backup & Restore
- Lanyrd's MySQL to PostgreSQL conversion script
- Use Jupyter Notebooks to demonstrate how to build a Recommender with Apache Spark & Elasticsearch
- Generic Data Ingestion & Dispersal Library for Hadoop
- DateTimePicker & DateTimeField for ExtJS
- A development tool to help migrate away from APIs and features that have been or will be removed from jQuery core
- jQuery contextMenu plugin & polyfill
- A Swagger Codegen for typescript, nodejs & angularjs
- All the source code for the AngularJS Up & Running Book for O'Reilly
- Lazy load modules & components in AngularJS
- C++ Eye Iris Automatic Detection based on entropy & Iris color score using openCV
- The fastest way to deploy a restful api's with Gin Framework with a structured project that defaults to PostgreSQL database and JWT authentication middleware stored in Redis
- A recipe for using Buffalo & Goth
- Vince Buffalo's devnotes — ½ TIL, ½ notebook
- Beego's RBAC & ABAC Authorization middleware based on Casbin
- Simple Add, Edit, Delete & Display in PHP & MySQL
- Passbolt CE Backend, a JSON API written with Cakephp
- Generate PDF's in CodeIgniter using this easy to use library based on domPDF
- Light, concurrent RPC framework for PHP & C
- Firefly's gevent version, with better perfromance than its previous twisted version
- Dave's An Introduction to Asynchronous Programming and Twisted in Chinese
- Build a Job Board with Python & Flask
- A great starting point to build your SaaS in Flask & Python, with Stripe subscription billing
- A lil' bootstrap app to create a Tornado project, ready to deploy to Heroku!
- A library to connect to Amazon's MWS web services in an object oriented manner
- Brew & manage PHP versions in pure PHP at HOME
- Thinking In PHP Internals, An open book on PHP Internals
- Which @NotNull Java annotation should I use
- php windows系统下的tp5运行Workerman显示 @@@no worker inited@@@ 并自动退出
- 在vue 插槽中设置一个el-input, 然后通过this.$refs能获取到这个子组件,但是设置focus无效
- TS2339: Property 'includes' does not exist on type 'number[]'
- PySide导入错误Mac OS X El Capitan,库未加载:@ rpath / libpyside.cpython-34m.1.2.dylib
- python-如何重用自定义@ contextlib.contextmanager?
- python-HDF5 min_itemsize错误:ValueError:试图在[y]列中存储一个长度为len [##]的字符串,但是此列的限制为[##]!
- 我为什么要在`@ asyncio.coroutine`上使用`async def`?
- python-无法将关键字“ i”解析为字段.选项包括:id,joind_on,user,user_id
- python-如何调用使用指针返回numba @jit中的值的ctypes函数
- numba @jit会降低纯Python的速度吗?