注解@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并由自己来管理.
注解@Component,@Controller,@Service,@Repository简单了解的更多相关文章
- spring自动扫描的注解@Component @Controller @Service @Repository
@Component @Controller @Service @Repository的作用 1.@controller 控制器(注入服务)2.@service 服务(注入dao)3.@reposit ...
- @Component @Controller @Service @Repository@Resourse
@Component @Controller @Service @Repository@Resourse这些全部是Spring提供的注解. 其中@Component用来表示把一个类纳入spring容器 ...
- SpringMVC常用注解@Controller,@Service,@repository,@Component
SpringMVC常用注解@Controller,@Service,@repository,@Component controller层使用@controller注解 @Controller 用于标记 ...
- 使用spring注解@Controller @Service @Repository简化配置
前言:在web项目中引入spring框架中的配置文件,我们给每一个java bean进行相关配置可以非常安全,便捷的管理我们的bean.那么,问题来了,如果一个项目中所涉及到的java bean十分庞 ...
- controller,service,repository,component注解的使用对比
项目中的controller层使用@controller注解 @Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象.分发处理器将会扫描使用了 ...
- SpringMVC常用注解@Controller,@Service,@repository,@Component,@Autowired,@Resource,@RequestMapping
1.controller层使用@Controller注解-用于呈现层,(spring-mvc) @Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controlle ...
- @Controller,@Service,@Repository,@Component详解
@Controller 用来表示一个web控制层bean,如SpringMvc中的控制器. @Service 用来表示一个业务层bean. @Repository 用来表示一个持久层bean,即数据访 ...
- Spring 学习——Spring常用注解——@Component、@Scope、@Repository、@Service、@Controller、@Required、@Autowired、@Qualifier、@Configuration、@ImportResource、@Value
Bean管理注解实现 Classpath扫描与组件管理 类的自动检测与注册Bean 类的注解@Component.@Service等作用是将这个实例自动装配到Bean容器中管理 而类似于@Autowi ...
- 关于Spring注解 @Service @Component @Controller @Repository 用法
@Component 相当于实例化类的对象,其他三个注解可以理解为@Component的子注解或细化. 在annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个spr ...
随机推荐
- Head First 设计模式笔记(模版方法模式)
1.定义: 在一个方法中定义一个算法骨架,而将一些步骤延迟到子类中.模版方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤. 2.类图: 3.说明: 模版方法可以理解为一个方法里面包 ...
- jemalloc报 Unsupported system page size错误
- Kudu的优点
不多说,直接上干货! Kudu目前具有以下优点 OLAP 工作的快速处理: 与 MapReduce,Spark 和其他 Hadoop 生态系统组件集成: 与 Apache Impala(incuba ...
- [转] Java集合类详解
集合类说明及区别Collection├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashMap└WeakHashMap ...
- REST与DDD
之前在为什么要使用MVC+REST+CQRS架构我曾经提出DDD是核心,REST是壳的观点,我想在这里详细谈谈我的思路. 今天正好看看到老外一篇博文Why REST is so important:按 ...
- pyhon虚拟环境的安装和使用
安装Python2.7: 1.Mac下使用Python2.7 2.Windows下安装Python2.7. *从python官网下载python2.7的版本 *双击python2.7,然后选择安装路径 ...
- springboot微信支付,支付二维码生成
https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1 微信扫码支付开发者文档,里面会有支付流程的教程 接口链接 URL地址:htt ...
- (12)JavaScript之[事件][事件监听]
事件 /** * 事件: * onload 和 onunload 事件在用户进入或离开页面时被触发 * * onchange事件常结合对输入字段的验证来使用 * onmouseover 和 onmou ...
- sharepoint2007就地升级2010系列(四)升级数据库
上一篇我们完成了系统的升级,今天我们来看一下SQL2005X64是如何升级到SQL2008X64的. 首先,我们先停掉所有sharepoint的服务 其实网上的文档并没有写到这一步,但是我个人觉得,要 ...
- Activity的Theme主题风格
在AndroidManifest.xml文件里面: <activity name="test" android:theme="@andr ...