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简单了解的更多相关文章

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

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

  2. @Component @Controller @Service @Repository@Resourse

    @Component @Controller @Service @Repository@Resourse这些全部是Spring提供的注解. 其中@Component用来表示把一个类纳入spring容器 ...

  3. SpringMVC常用注解@Controller,@Service,@repository,@Component

    SpringMVC常用注解@Controller,@Service,@repository,@Component controller层使用@controller注解 @Controller 用于标记 ...

  4. 使用spring注解@Controller @Service @Repository简化配置

    前言:在web项目中引入spring框架中的配置文件,我们给每一个java bean进行相关配置可以非常安全,便捷的管理我们的bean.那么,问题来了,如果一个项目中所涉及到的java bean十分庞 ...

  5. controller,service,repository,component注解的使用对比

    项目中的controller层使用@controller注解 @Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象.分发处理器将会扫描使用了 ...

  6. SpringMVC常用注解@Controller,@Service,@repository,@Component,@Autowired,@Resource,@RequestMapping

    1.controller层使用@Controller注解-用于呈现层,(spring-mvc) @Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controlle ...

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

    @Controller 用来表示一个web控制层bean,如SpringMvc中的控制器. @Service 用来表示一个业务层bean. @Repository 用来表示一个持久层bean,即数据访 ...

  8. Spring 学习——Spring常用注解——@Component、@Scope、@Repository、@Service、@Controller、@Required、@Autowired、@Qualifier、@Configuration、@ImportResource、@Value

    Bean管理注解实现 Classpath扫描与组件管理 类的自动检测与注册Bean 类的注解@Component.@Service等作用是将这个实例自动装配到Bean容器中管理 而类似于@Autowi ...

  9. 关于Spring注解 @Service @Component @Controller @Repository 用法

    @Component 相当于实例化类的对象,其他三个注解可以理解为@Component的子注解或细化. 在annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个spr ...

随机推荐

  1. java——数组队列 ArrayQueue

    队列: Array: package Date_pacage; public class Array<E> { //叫它静态数组 //private int[] data; private ...

  2. Invalid prop: type check failed for prop "XXX". Expected String, got Object.

    项目是Vue的,基于elementUI的后台管理系统. Invalid prop: type check failed for prop "total". Expected Str ...

  3. form表单序列化数据之后,追加额外数据

    form表单序列化数据之后追加额外数据多使用在js中,下面是追加额外数据的代码: <span style="font-size:18px;">$.param({'inv ...

  4. mysql忘记密码时,重新修改密码

    错误描述 1045 access denied for user 'root'@'localhost' using password yes 1045 access denied for user ' ...

  5. Murano Weekly Meeting 2015.11.11

    Meeting time: 2015.November.11th 1:00~2:00 Chairperson:  Serg Melikyan, PTL from Mirantis Meeting su ...

  6. 细讲递归(recursion)

    首先先对递归进行入门. 递归是以自相似的方式重复项目的过程.在编程语言中,如果程序允许您在同一函数内调用函数,则称其为函数的递归调用. 简而言之,递归就是函数的自身调用.可以看看下面的递归使用: vo ...

  7. SQL命令行操作

    命令行操作(mysql.exe)    0.登录  :       mysql -u root -p    1.显示数据库列表:    show databases;     2.选择数据库:     ...

  8. 定时备份oracle数据库脚本文件

    @echo off REM ########################################################### REM # Windows Server 2003下 ...

  9. #include stdio.h(5)

    #include <stdio.h> int main() { //1.数组的排序-冒泡排序 /* 1.规则:相邻的两个数据进行比较 2.如果有N个数据,需要选择N-1次参照物来比较, 因 ...

  10. ComponentOne、Spread、ActiveReports 5折起 加入惊喜惠

    慧都十周年,GrapeCity也来共襄盛举,旗下三大产品产品线齐齐参与.界面控件套包ComponentOne.Excel表格控件Spread与报表开发工具ActiveReports,指定授权5折起加入 ...