@Controller

用来表示一个web控制层bean,如SpringMvc中的控制器。

@Service

用来表示一个业务层bean。

@Repository

用来表示一个持久层bean,即数据访问层DAO组件。

@Component

用来表示一个平常的普通组件,当一个类不合适用以上的注解定义时用这个组件修饰。

需要注意的是@Controller,@Service,@Repository都有带@Component父注解,说明它们除了基本组件的属性外还有其他的的场景应用,即如果不用SpringMVC其实它们就是一个普通的组件,但普通组件建议最好还是用@Component修饰。

为了让Spring自动扫描注册这些组件,需要在配置文件中加上扫描的配置,如扫描com.test包下的注解。

<context:component-scan base-package="com.test" />

些扫描配置默认use-default-filters="true",默认扫描@Component注解及子注解,可以配置过滤只扫描哪些注解不扫描哪些注解。

要过滤扫描注解,需要相应的带上下面的子标签,可以有多个。

<context:include-filter>

<context:exclude-filter>

如只扫描com.test包下的@Controller和@Service注解的组件。

<context:component-scan base-package="com.test" use-default-filters="false">

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>

</context:component-scan>

关于type的定义

Filter Type Examples Expression Description
annotation org.example.SomeAnnotation 符合SomeAnnoation的target class
assignable org.example.SomeClass 指定class或interface的全名
aspectj org.example..*Service+ AspectJ语法
regex org\.example\.Default.* Regelar Expression
custom org.example.MyTypeFilter 自定义Type,实现接口org.springframework.core.type.TypeFilter

另外,<context:component-scan>配置可以有多个。

@Controller,@Service,@Repository,@Component详解的更多相关文章

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

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

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

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

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

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

  4. @Component @Controller @Service @Repository@Resourse

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

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

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

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

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

  7. Android中Service的使用详解和注意点(LocalService)

    Android中Service的使用详解和注意点(LocalService) 原文地址 开始,先稍稍讲一点android中Service的概念和用途吧~ Service分为本地服务(LocalServ ...

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

    Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的开发.@Repository注解便属于最先引入的一批,它用于将数据访问层 (DAO 层 ) 的类标识为 Spring B ...

  9. vue从入门到进阶:组件Component详解(六)

    一.什么是组件? 组件 (Component) 是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它添加特殊功 ...

随机推荐

  1. Scala入门系列(六):面向对象之object

    object object相当于class的单个实例,类似于Java中的static,通常在里面放一些静态的field和method.   第一次调用object中的方法时,会执行object的con ...

  2. Thinking in Java & 内部类

    静态内部类可以直接创建对象new B.C(); 如果内部类不是静态的,那就得这样 B b = new B(); B.C c = b.new C(); //要想直接创建内部类的对象,不能按照想象的方式, ...

  3. Mac下不能安装第三方下载软件

    1.安装成功后,启动时如提示“某某文件已损坏,打不开”,请将电脑的“系统偏好设置--安全性与隐私--通用”的允许从以下位置下载的应用程序设置为“任何来源”. 2.如果您的电脑上没有“任何来源”的选项, ...

  4. 在CANopen网络中通过LSS服务设置节点地址和网络波特率

    CANopen专有个子协议用来描述怎样去通过网络设置节点地址和波特率,就是CiA DSP-305,大伙都叫LSS协议,是Layer Setting Services的缩写,不太好翻译,也许可以翻译成底 ...

  5. 操作系统 cmd mini OS

    #include <stdio.h>#include <stdlib.h>#include <string.h> void word(char *a){ if(st ...

  6. VMWare之——宿主机与虚拟机互相ping通,宿主机ping通另一台机器的虚拟机

    版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处:http://blog.csdn.NET/l1028386804/article/details/52267554 今天给大家带来 ...

  7. [转帖]从HTTP/0.9到HTTP/2:一文读懂HTTP协议的历史演变和设计思路

    从HTTP/0.9到HTTP/2:一文读懂HTTP协议的历史演变和设计思路   http://www.52im.net/thread-1709-1-2.html     本文原作者阮一峰,作者博客:r ...

  8. java 数据结构与算法---二叉树

    原理来自百度百科     推荐数据演示网址 :https://www.cs.usfca.edu/~galles/visualization/BST.html 一.什么是二叉树 二叉树的每个结点至多只有 ...

  9. iOS BCD码、数据流、字节和MD5计算

    一.各个之间的相互转换 1.字符串转数据流NSData NSString *str = @"abc123"; NSData *dd = [str dataUsingEncoding ...

  10. MT【121】耐克数列的估计

    已知$\{a_n\}$满足$a_1=1,a_2=2,\dfrac{a_{n+2}}{a_n}=\dfrac{a_{n+1}^2+1}{a_n^2+1}$, 求$[a_{2017}]$_____ 解:容 ...