注解@Component,@Controller,@Service,@Repository简单了解

Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的开发。@Repository注解便属于最先引入的一批,它用于将数据访问层 (DAO 层 ) 的类标识为 Spring Bean。具体只需将该注解标注在 DAO类上即可。同时,为了让 Spring 能够扫描类路径中的类并识别出 @Repository 注解,需要在 XML 配置文件中启用Bean 的自动扫描功能,这可以通过<context:component-scan/>实现;

Spring 2.5 在 @Repository的基础上增加了功能类似的额外三个注解:@Component、@Service、@Constroller

它们分别用于软件系统的不同层次:

@Component 是一个泛化的概念,仅仅表示一个组件 (Bean) ,可以作用在任何层次。

@Service 通常作用在业务层,但是目前该功能与 @Component 相同。

@Constroller 通常作用在控制层,但是目前该功能与 @Component 相同

。通过在类上使用 @Repository、@Component、@Service 和 @Constroller 注解,Spring会自动创建相应的 BeanDefinition 对象,并注册到 ApplicationContext 中。这些类就成了 Spring受管组件。这三个注解除了作用于不同软件层次的类,其使用方式与 @Repository 是完全相同的。

@Component 是通用标注

•@Controller 标注 web 控制器

•@Service 标注 Servicec 层的服务

•@Respository 标注 DAO 层的数据访问

@Controller 用来定义控制(dao)层的组件

@Service 用来定义业务层(service)的组件

@Respository 用来定义持久层(domain)的组件

@Component 用来定义不在上述范围内的一般性组件

上面组件的名称默认是类名的首字母小写,如果要重命名,则这样@controller("beanName")

当在spring中配置了<context:annotation-config/> 和<context:component-scan base-package="*">时,上述四种注解的组件都会由spring容器来创建为bean并由自己来管理.

posted @ 2018-10-09 10:38 韦邦杠 阅读(...) 评论(...) 编辑 收藏

注解@Component,@Controller,@Service,@Repository简单了解相关推荐

  1. spring自动扫描的注解@Component @Controller @Service @Repository

    @Component @Controller @Service @Repository的作用 1.@controller 控制器(注入服务) 2.@service 服务(注入dao) 3.@repos ...

  2. 解释@Component @Controller @Service @Repository

    2019独角兽企业重金招聘Python工程师标准>>> Spring 配置管理 Bean XML .net 对Spring的注解标签刚刚接触,所以就找了几个常用的,记录下,感觉注解用 ...

  3. @Controller,@Service,@Repository,@Component详解

    转载自 @Controller,@Service,@Repository,@Component详解 @Controller 用来表示一个web控制层bean,如SpringMvc中的控制器. @Ser ...

  4. 面试官:注解@Component,@Service是如何被解析的?

    点击上方 好好学java ,选择 星标 公众号重磅资讯,干货,第一时间送达 今日推荐:推荐19个github超牛逼项目!个人原创100W +访问量博客:点击前往,查看更多 来源: https://my ...

  5. spring中stereotype注解Component,Repository,Service,Controller

    [README] 本文介绍了 spring4.0 下 org.springframework.stereotype 的注解类型,俗称刻板型注解(一成不变型): 包括 @Component, @Repo ...

  6. r中rep_Spring中@ Component,@ Service,@ Controller和@Repository之间的区别

    r中rep 在了解Spring框架中@Repository @Component , @Service @Controller , @Repository @Controller和@Repositor ...

  7. Spring中@ Component,@ Service,@ Controller和@Repository之间的区别

    在了解Spring框架中@Repository @Component , @Service @Controller , @Repository @Controller和@Repository批注之间的 ...

  8. @service注解_Spring 中 @Component、@Service 等注解如何被解析的

    作者:温安适原文:https://juejin.im/post/6844904199688306702#heading-5 前言 @Component和@Service都是工作中常用的注解,Sprin ...

  9. Spring 中 @Component、@Service 等注解如何被解析?

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 来源 | http://8rr.co/EjqL 前言 @ ...

  10. @service注解_Spring 中 @Component、@Service 等注解如何被解析?

    前言 @Component和@Service都是工作中常用的注解,Spring如何解析? 1.@Component解析流程 找入口 Spring Framework2.0开始,引入可扩展的XML编程机 ...

最新文章

  1. 2015年度互联网安全报告发布 移动支付成重灾区
  2. 关于 jsp java servlet 中文汉字乱码的解决方法
  3. [转]关于WM_NCHITTEST消息
  4. Promise 的四种常用方法。
  5. 哈工大讯飞联合实验室发布少数民族多语言预训练模型CINO
  6. 如果你到了20岁,还没到25岁
  7. 软考程序员2017下半年真题含答案解析
  8. 也谈谈内卷化、996和程序员的发展
  9. 云计算机怎么分盘,电脑硬盘怎么分区
  10. 【166】VS2022调试通过海康人脸抓拍SDK的C++代码
  11. vue3.0需要学习的技术栈
  12. MySQL修改数据库名字
  13. 什么是JAVA?JAVA能做什么?
  14. 校园表白墙告白墙表白网站程序源码搭建教程
  15. 多段曲线控温“核函数“算法分析(完整梯形图源代码)
  16. 结构化经验教训总结单
  17. 获得android手机root权限,安卓设备什么是Root? 获得手机Root后可进行哪些高权限的操作?...
  18. 软件版本中的Alpha,Beta,Gamma,RC等
  19. 苹果乔布斯辞世享年56岁 世界因他而变得美好
  20. 室内设计优美语句_形容室内的唯美句子

热门文章

  1. Python爬取王者荣耀手游中的游戏英雄角色
  2. Python绘制简单漂亮好玩的散点图
  3. 中文分词项目(开源/API接口)总结
  4. C3927 “->“: 非函数声明符后不允许尾随返回类型等错误
  5. html怎么让背景颜色百分比,jquery – CSS设置背景颜色只是表行宽度的一个百分比...
  6. FISCO BCOS 确定性多合约并行(DMC)
  7. 证明3-SAT问题是NP-complete
  8. 如何证明NP-Hard Problems
  9. FISCO BCOS 控制台 部署合约 调用 查看已部署合约的地址
  10. pycharm git使用_通过Pycharm中的版本控制工具VCS使用Git