SpringMVC四大注解

  • Component

  通用标注,在不清楚使用哪个注解的时候,可以使用Component通用注解

  • Controller

  标注web请求控制器

  • Service

  标注Service层的服务

  • Repository

  标注DAO层的数据访问

四大注解都是类级别的,可以不带任何参数,也可以带一个参数,代表bean名字,在进行注入的时候就可以通过名字进行注入了。

bean的自动载入

在SpringMVC的配置文件中,通过context:component-scan使注解生效。这样就可以代替在web.xml中配置bean,自动实现bean的载入。例子:

<context:component-scan base-package="com.studySpringMVC.*">

  <context:include-filter type="annotation" expression="com.studySpringMVC.service"/>

  <context:exclude-filter type="annotation" expression=" com.studySpringMVC.service "/>

</context:component-scan>

context:include-filter定义需要扫描的包,context:exclude-filter定义不扫描的包,在配置时,两个标签的expression值不能设置成相同

type属性值有5个

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 Spring3新增自订Type,实作org.springframework.core.type.TypeFilter

使用@Resource或@Autowired注解实现注入

@Autowired默认按类型装配,默认情况下必须要求依赖对象必须存在,如果要允许null值,可以设置它的required属性为false(如@Autowired(required=false)),如果我们想使用名称装配可以结合@Qualifier注解进行使用

@Resource默认按名称进行装配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认按字段名进行名称查找,如果注解写在setter方法上,默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。推荐使用@Resource,这个注解属于J2EE,减少了与Spring的耦合

SpringMVC---四大注解的更多相关文章

  1. SpringMVC常用注解實例詳解3:@ResponseBody

    我的開發環境框架:        springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...

  2. SpringMVC常用注解實例詳解2:@ModelAttribute

    我的開發環境框架:        springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...

  3. springMVC的注解详解

    springmvc常用注解标签详解 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业 ...

  4. springmvc常用注解与类型转换

    springmvc常用注解与类型转换 一:前置 spring -servlet.xml 注入 <!-- 启用spring mvc 注解 --> <context:annotation ...

  5. springMvc的注解注入方式

    springMvc的注解注入方式 最近在看springMvc的源码,看到了该框架的注入注解的部分觉的有点吃力,可能还是对注解的方面的知识还认识的不够深刻,所以特意去学习注解方面的知识.由于本人也是抱着 ...

  6. SpringMVC全注解

    SpringMVC全注解不是你们那么玩的 前言:忙了段时间,忙得要死要活,累了一段时间,累得死去活来. 偶尔看到很多零注解配置SpringMVC,其实没有根本的零注解. 1)工程图一张: web.xm ...

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

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

  8. 一 : springmvc常用注解

    springmvc常用注解详解1.@Controller在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层 ...

  9. 04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s

     1. Spring-mvc介绍 1.1市面上流行的框架 Struts2(比较多) Springmvc(比较多而且属于上升的趋势) Struts1(即将被淘汰) 其他 1.2  spring-mv ...

  10. 转:springmvc常用注解标签详解

    Spring5:@Autowired注解.@Resource注解和@Service注解 - IT·达人 - 博客园--这篇顺序渐进,讲得超级好--此人博客很不错http://www.cnblogs.c ...

随机推荐

  1. Struts2_Struts标签

    1.property <s:property value="username" /> 一般访问,访问ValueStack中的第一个username <s:prop ...

  2. PHP switch分支语句中省略break后还会执行其他case的原因分析

    请分析以下PHP代码的输出结果: $a= 'dog'; switch($a) { case 'cat': echo "\$a is cat"; case 'dog': echo & ...

  3. intel Skylake平台安装WIN7

    目前针对IntelSkylake平台安装WIN7时USB接口失灵的问题,不少硬件厂商都推出了免费修改工具来集成XHCI USB控制器驱动,这其中技嘉提供了一款Windows USB Installat ...

  4. ssh配置解释

    http://vbird.dic.ksu.edu.tw/linux_server/0310telnetssh_2.php /etc/ssh/sshd_config Port 29922 #Addres ...

  5. WebRTC协议

    webrtc协议介绍 MDN webrtc协议 ICE 交互式连接建立Interactive Connectivity Establishment (ICE) 是一个允许你的浏览器和对端浏览器建立连接 ...

  6. frame、window和dialog区别

    属性 Window Frame Dialog 模式化 不是 不是 不是(可设置) 可调大小 不可 可 可 标题栏 无 有 有 边界 无 有 有 标题 无 有 有 菜单栏 无 有 无 焦点管理器 有 有 ...

  7. 字符串处理(POJ1782)

    题目链接:http://poj.org/problem?id=1782 解题报告: #include <iostream> #include <cstdio> #include ...

  8. python 下实现window 截图

    首先安装PIL库,因为PIL官网没有支持python3.6的PIL库我想在3.X中实现,因此使用pip安装pillow pip install pillow 安装 安装完成后,from PIL imp ...

  9. 121. Best Time to Buy and Sell Stock——Leetcode

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  10. AMD、CMD、CommonJs和ES6对比

    一.AMD(异步模块定义) AMD(异步模块定义)是RequireJS在推广过程中对模块定义的规范化产出.AMD是一个概念,RequireJs是对这个概念的实现.比如javascript语言是对ECM ...