Spring的注解是在Spring2.5的版本中引入的,目的简化XML配置。在企业开发过程中使用注解的频率非常高,但是学习注解的前提是大家一定要对Spring基于XML配置要熟悉,这是我个人建议,因为在Spring2.0的版本时候是没有出现注解的使用

1. Spring常用注解如下

  • @Component
  • @Autowired
  • @Qualifier
  • @Scope
  • @Controller
  • @Service
  • @Repository

2. 使用Spring注解的时候一定关注Spring框架需要加入的包【很重要】,我们这里以spring4.0.3版本为例来介绍

  • common-logging.jar
  • spring-core-4.0.3.jar
  • spring-context-4.0.3.jar
  • spring-beans-4.0.3.jar
  • spring-expression-4.0.3.jar
  • spring-aop-4.0.3.jar,【此jar包在使用<context:component-scan/>需要导入此包】

3. @Component注解

  • @Component主要用于将一个Java类注入到Spring框架中,它相当于XML配置文件中的<bean id=”xxx” class=”xxx”/>
  • 当使用了Spring注解后,我们需要在配置文件中添加<context:component-scan/>来扫描添加了注解的类,这样子声明注解的类才能起作用
  • Bean实例的名称默认是Bean类的首字母小写,其他部分不变。
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd">
  10.  
  11. <context:component-scan base-package="com.gxa.spring.day02"></context:component-scan>
  12.  
  13. </beans>
  1. package com.gxa.spring.day02;
  2.  
  3. import org.springframework.stereotype.Component;
  4.  
  5. @Component
  6. public class StudentAnnotation {
  7. }
  1. package com.gxa.spring.test;
  2.  
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6.  
  7. import com.gxa.spring.day02.StudentAnnotation;
  8.  
  9. public class Test02 {
  10.  
  11. @Test
  12. public void m06() {
  13. ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
  14. StudentAnnotation studentAnnotation = context.getBean("studentAnnotation", StudentAnnotation.class);
  15. System.out.println(studentAnnotation.hashCode());
  16. }
  17.  
  18. }

4. 除了@Component注解,Spring容器提供了3个功能和@Component注解等同。它们分别是用于对Dao,Service及Web层的Controller进行注解

  • @Repository:用于对Dao实现类注解
  • @Service:用于对Service实现类注解
  • @Controller:用于对Controller实现类注解

5. @Autowired注解

  • Spring2.5引入了@Autowired注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作(DI依赖注入工作)
  • Spring通过@Autowired注解实现Bean之间的依赖关系
  1. package com.gxa.spring.day02;
  2.  
  3. import org.springframework.stereotype.Component;
  4.  
  5. @Component
  6. public class TeacherAnnotation {
  7. public void getTeacherName() {
  8. System.out.println("===刘老师好===");
  9. }
  10. }
  1. package com.gxa.spring.day02;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Component;
  5.  
  6. @Component
  7. public class StudentAnnotation {
  8.  
  9. /**
  10. * 通过Autowired来自动装备Student所依赖的对象
  11. * 无需创建setter方法
  12. * 无需编写XML配置文件中<property/>
  13. */
  14. @Autowired
  15. private TeacherAnnotation teacherAnnotation;
  16.  
  17. public void showTeacher() {
  18. teacherAnnotation.getTeacherName();
  19. }
  20. }
  • @Autowired(required = false),告诉 Spring:在找不到匹配 Bean 时也不报错
  1. package com.gxa.spring.day02;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Component;
  5.  
  6. @Component
  7. public class StudentAnnotation {
  8.  
  9. /**
  10. * 注意TeamAnnotation没有加入@Component
  11. * 当StudentAnnotation依赖TeamAnnotation会报错
  12. * 但是如果加入@Autowired(required=false)就不会报错
  13. */
  14. @Autowired(required=false)
  15. private TeamAnnotation teamAnnotation;
  16.  
  17. public void showTeacher() {
  18. teacherAnnotation.getTeacherName();
  19. }
  20. }

6. @Qualifier指定注入Bean的名称

  • 如果容器中有一个以上匹配的Bean时,则可以通过@Qualifier注解限定注入的Bean名称
  1. package com.gxaedu.bean;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Qualifier;
  5. import org.springframework.stereotype.Component;
  6.  
  7. @Component
  8. public class Computer {
  9. @Autowired
  10. @Qualifier("storage01")
  11. private Storage storage;
  12. public void m01() {
  13. storage.show();
  14. }
  15. }
  1. package com.gxaedu.bean;
  2.  
  3. import org.springframework.stereotype.Component;
  4.  
  5. @Component("storage01")
  6. public class Storage { //此类是在com.gxaedu.bean下的Storage
  7. public void show() {
  8. System.out.println("东芝硬盘");
  9. }
  10. }
  1. package com.gxaedu.test;
  2.  
  3. import org.springframework.stereotype.Component;
  4.  
  5. @Component
  6. public class Storage { //此类是在com.gxaedu.test下的Storage
  7.  
  8. }

7. @Qualifier为方法指定注入Bean名称

  1. package com.gxa.spring.day02;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Qualifier;
  5. import org.springframework.stereotype.Component;
  6.  
  7. @Component
  8. public class StudentAnnotation {
  9.  
  10. private CourseAnnotation courseAnnotation;
  11.  
  12. /**
  13. * @Autowired来自动装备构造方法里面的对象
  14. * @Qualifier来指定依赖对象的bean
  15. * @paramcourseAnnotation
  16. */
  17. @Autowired
  18. public StudentAnnotation(@Qualifier("course") CourseAnnotation courseAnnotation) {
  19. this.courseAnnotation = courseAnnotation;
  20. }
  21.  
  22. public void showCourse() {
  23. System.out.println(courseAnnotation.hashCode());
  24. }
  25. }

8. @Scope注解来显式指定Bean作用范围

  1. @Scope("prototype")
  2. @Component
  3. public class Car {
  4. }

[刘阳Java]_Spring常用注解介绍_第6讲的更多相关文章

  1. [刘阳Java]_Spring相关配置介绍_第5讲

    这一节我们介绍一下Spring框架的相关常用配置 Spring依赖注入的两种方式(构造方法注入和setter方式注入) p-namespace方式配置 properties属性文件配置方式 集合对象配 ...

  2. [刘阳Java]_Spring AOP注解详细介绍_第8讲

    这节内容非常关键,我们会比较详细地介绍Spring AOP注解的使用 1. 要使用Spring AOP注解,必须满足如下的事项 导入Aspectj的jar.Spring3.0-AOP.jar.aopa ...

  3. [刘阳Java]_MyBatis_实体关系映射_第8讲

    MyBatis既然是一个ORM框架,则它也有像Hibernate那样的一对多,多对多,多对一的实体关系映射功能.下面我们就来介绍一下如何使用MyBatis的实体关系映射 1.MyBatis实体关系映射 ...

  4. [刘阳Java]_SpringMVC访问静态资源_第9讲

    有些时候我们在使用SpringMVC的时候造成无法访问静态资源文件(如:html,js,css,image等等).其主要的原因出在web.xml文件我们设置SpringMVC前端控制器的映射路径 &l ...

  5. [刘阳Java]_BeanNameViewResolver视图解析器_第8讲

    BeanNameViewResolver:这个视图解析器跟XmlViewResolver有点类似,也是通过把返回的逻辑视图名称去匹配定义好的视图bean对象.它要求视图bean对象都定义在Spring ...

  6. [刘阳Java]_ResourceBundleViewResolver视图解析器_第7讲

    ResourceBundleViewResolver是根据proterties文件来找对应的视图来解析"逻辑视图".该properties文件默认是放在classpath路径下的v ...

  7. [刘阳Java]_InternalResourceViewResolver视图解析器_第6讲

    SpringMVC在处理器方法中通常返回的是逻辑视图,如何定位到真正的页面,就需要通过视图解析器 InternalResourceViewResolver是SpringMVC中比较常用视图解析器. 网 ...

  8. [刘阳Java]_Spring AOP基于XML配置介绍_第9讲

    基于注解配置的Spring AOP固然简单,但是这节我们会给大家介绍基于XML配置的AOP是如何应用的.为什么这么说了,因为后面我们还会介绍到Spring对Dao操作的事务管理(基于AOP的XML文件 ...

  9. [刘阳Java]_Spring AOP入门_第7讲

    AOP技术个人认为是能够完善(改善)面向对象编程OOP.为什么这么说,我们得先从AOP的概念说起,然后通过一段简单的例子加以佐证.这样子大家就可以慢慢地了解AOP 1. AOP概念 AOP为Aspec ...

随机推荐

  1. TVM性能评估分析(三)

    TVM性能评估分析(三) Figure 1. TVM's WebGPU backend close to native GPU performance when deploying models to ...

  2. Pass Infrastructure基础架构(下)

    Pass Infrastructure基础架构(下) pass注册  PassRegistration该类在示例中简要显示了各种pass类型的定义 .该机制允许注册pass类,以便可以在文本pass管 ...

  3. 认真总结Vue3中ref与reactive区别和isRef与isReactive 类型判断

    1.什么是ref? 1.ref和reactive-样 也是用来实现响应式数据的方法 由于reactive必须传递一个对象, 所以导致在企业开发中如果我们只想让某个变量实现响应式的时候会非常麻烦 所以V ...

  4. Jenkins 进阶篇 - 数据备份

    随着我们的长期使用,Jenkins 系统中的内容会越来越多,特别是一些配置相关的东西,不能有任何丢失.这个时候我们就需要定期备份我们的 Jenkins 系统,避免一些误操作不小心删除了某些重要文件,J ...

  5. jQuery基础-选择器,样式操作

    入口函数:ready() 当 DOM(文档对象模型) 已经加载,并且页面(包括图像)已经完全呈现时,会发生 ready 事件. 由于该事件在文档就绪后发生,因此把所有其他的 jQuery 事件和函数置 ...

  6. SSM框架集成各配置文件

    SSM框架集成各配置文件 Spring Spring MVC Mybatis 的整合SpringMVC相当于Spring的一个组件 本来就是一个家族的不存在整合的问题,所以主要就是Spring于Myb ...

  7. PRVF-0002: could not retrieve local nodename报错解决

     

  8. C程序从编译到运行

    第一篇文章 一.前言 最近在看CSAPP(深入理解计算机系统)然后以前也学过C语言,但是从来没有深究写好的C代码是怎么编译再到执行的. 所以现在自己学习,然后记录下来. 以最常用的hello worl ...

  9. 我对SpringMVC的浅见

    之前在学校没接触框架这东西之前只接触过MVC的model1和model2,而真正接触SpringMVC的时候是在一年前,在学习过程中,我这才意识到SpringMVC大大简化了以前的开发工程,到了社会上 ...

  10. 玩转STM32MP157- 在应用层中使用 fbtft

    fbtft使用的是framebuffer框架,这种框架将显示设备抽象为帧缓冲区,对framebuffer设备(/dev/fbx(0.1.2..))进行相关操作可以反应到LCD上. 现在尝试下在用户空间 ...