注解产生原因

1、传统spring的做法:使用xml来对bean进行注入和或者是配置aop、事物配置文件体积庞大,造成了配置文件的可读性和可维护性很低Java文件和xml不断切换,造成思维不连贯开发效率降低

2、引入注解的做法:通过@xxx让注解与Java Bean紧密结合既大大减少了配置文件的体积,又增加了Java Bean的可读性与内聚性。

构造IOC容器的两种方式

1、使用配置文件

2、使用注解

用注解来向Spring容器注册Bean。需要在配置文件中注册<context:component-scan base-package=”pagkage1[,pagkage2,…,pagkageN]”/>,默认启用注解的支持
,<context:annotation-config /> 可省略。

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- <context:annotation-config /> -->
<!-- 扫描这个包的所有组件 -->
<context:component-scan base-package="ecut.aop.annotation" /> <!-- 为 注解 提供 自动产生 代理对象的 支持 -->
<aop:aspectj-autoproxy proxy-target-class="true" /> </beans>

base-package所指定的包及其子包会被扫描(多个包可用逗号分隔),只要类中带有特殊的注解将会被自动注入

常用的注解

1、标注组件的

  • @org.springframework.stereotype.Controller : 专门用来 标注 控制层的组件

    package ecut.aop.annotation;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Controller; /**
    * 控制层 ( Controller ) : 对 表示层 的用户操作 做出响应
    * 实现技术可以是: Servlet 、Struts 、Spring MVC
    * @Controller("sc") value ="sc" 相当于bean的名称是sc,默认名称是 studentController
    */
    @Controller
    public class StudentController { @Autowired
    @Qualifier( "studentService" )
    private StudentService studentService ; public String regist( Student s ){
    System.out.println( "StudentController # regist ( Student ) " );
    studentService.save( s );
    return "success" ;
    } public String logout() { throw new RuntimeException( "抛出异常" ); } public StudentService getStudentService() {
    return studentService;
    } public void setStudentService(StudentService studentService) {
    this.studentService = studentService;
    } }

    扫描到注解@Controller相当于在配置文件中<bean id="studentController" class="ecut.aop.annotation.StudentController" />增加这段配置。bean的id属性即名称若在注解中没有指定则默认是类名首字母小写后的名称例如StudentController类所默认的名称是studentController,若通过注解中的value指定了名称,则与此保持一致。

  • @org.springframework.stereotype.Service : 专门用来标注 业务逻辑层 的组件
    package ecut.aop.annotation;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service; /**
    * 业务逻辑层 ( Service ) 具体的业务逻辑
    */
    @Service
    public class StudentService { @Autowired
    private StudentDao studentDao ; public void save( Student s ) {
    System.out.println( "StudentService # save ( Student ) " );
    studentDao.persist( s );
    } public StudentDao getStudentDao() {
    return studentDao;
    } public void setStudentDao(StudentDao studentDao) {
    this.studentDao = studentDao;
    } }

    @Service相当于在配置文件中增加<bean id="studentService" class="ecut.aop.annotation.StudentService" />

  • @org.springframework.stereotype.Repository : 专门用来 标注 数据访问层 的 组件
    package ecut.aop.annotation;
    
    import org.springframework.stereotype.Repository;
    
    /**
    * 数据访问层 ( Repository ) 专门负责访问数据库
    * DAO :Data Access Object , 数据访问对象
    * 对象封装数据
    */
    @Repository
    public class StudentDao { public void persist( Student s ) {
    System.out.println( "StudentDao 的 persist 方法提示你: 保存 "+ s.getName() );
    } }

    @Repository相当于在配置文件中增加<bean id="studentDao" class="io.spring.aop.annotation.StudentDao" />

  • @org.springframework.stereotype.Component : 可以标注任意的组件
    package ecut.aop.annotation;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.Signature;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component; @Component
    @Aspect
    public class StudentAspect {
    .......
    }

    当不能确定到底是 Controller 、Service 、Repository 中的那种类型时,适用。

2、标注自动装备的

  • @org.springframework.beans.factory.annotation.Autowired : 默认按照类型对某个属性进行装配

    package ecut.aop.annotation;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service; /**
    * 业务逻辑层 ( Service ) 具体的业务逻辑
    */
    @Service
    public class StudentService { @Autowired
    private StudentDao studentDao ;
    ..............................
    }

    相当于 <bean id="studentService" class="ecut.aop.annotation.StudentService" autowire="byType"/>,当Spring发现@Autowired注解时,将自动在代码上下文中找到和其匹配(默认是类型匹配)的Bean,并自动注入到相应的地方去。

  • @org.springframework.beans.factory.annotation.Qualifier 与 Autowired 连用一起实现 按照名称进行自动装配
    package ecut.aop.annotation;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Controller; /**
    * 控制层 ( Controller ) : 对 表示层 的用户操作 做出响应
    * 实现技术可以是: Servlet 、Struts 、Spring MVC
    * @Controller("sc") value ="sc" 相当于bean的名称是sc,默认名称是 studentController
    */
    @Controller
    public class StudentController { @Autowired
    @Qualifier( "studentService" )
    private StudentService studentService ;
    ........
    }

    相当于 <bean id="studentController" class="ecut.aop.annotation.StudentController" autowire="byName"/>或者    <bean id="studentController" class="ecut.aop.annotation.StudentController" p:studentService-ref="studentService"/>

  • @javax.annotation.Resource:@Resource后面没有任何内容,默认通过name属性去匹配bean,找不到再按type去匹配指定了name或者type则根据指定的类型去匹配bean指定了name和type则根据指定的name和type去匹配bean,任何一个不匹配都将报错

3、@Resource和@Autowired注解的区别

  • 默认匹配的方式不同:@Autowired默认按照byType方式进行bean匹配,@Resource默认按照byName方式进行bean匹配。
  • 所属架构不同:@Autowired是Spring的注解,@Resource是J2EE的注解(Spring属于第三方的,J2EE是Java自己的东西,因此,建议使用@Resource注解,以减少代码和Spring之间的耦合)

转载请于明显处标明出处:

https://www.cnblogs.com/AmyZheng/p/9277957.html

Spring学习(七)的更多相关文章

  1. Spring学习七----------Bean的配置之自动装配

    © 版权声明:本文为博主原创文章,转载请注明出处 Bean的自动装配(Autowiring) no:不启用自动装配,此时需要手动注入.参考:Spring学习三----------注入方式 defaul ...

  2. spring学习七 spring和dynamic project进行整合

    spring和web项目进行整合,其实就是在项目启动时,就创建spring容器,然后在servlet中使用spring容器进行开. 注意:为了页面可以访问到servlet,因此servlet必须放进t ...

  3. Spring学习(七)-----Spring Bean的5种作用域

    在Spring中,bean作用域用于确定哪种类型的 bean 实例应该从Spring容器中返回给调用者.bean支持的5种范围域: 单例(singleton) - 每个Spring IoC 容器返回一 ...

  4. Spring学习(七)--Spring MVC的高级技术

    一.Spring MVC配置的替代方案 我们已经了解如何通过AbstractAnnotationConfigDispatcherServlet- Initializer快速搭建了Spring MVC环 ...

  5. Spring学习七:ComponentScan注解

    今天主要从以下几个方面来介绍一下@ComponentScan注解: @ComponentScan注解是什么 @ComponentScan注解的详细使用 1.ComponentScan注解是什么 其实很 ...

  6. spring学习七

    一: web.xml中常用配置元素? <servlet></servlet>: 在向servlet或JSP页面制定初始化参数或定制URL时,首先命名servlet或JSP页面. ...

  7. MyBatis学习七:spring和MyBatis整合

    <\mybatis\day02\16mybatis和spring整合-sqlSessionFactory配置.avi;> MyBatis学习七:spring和MyBatis整合.逆向工程 ...

  8. Spring学习笔记(七)模拟实际开发过程的调用过程XML版-Setter方式注入

    模拟实际开发过程的调用过程XML版-Setter方式注入 源码获取github [TOC] 1.项目结构 2.jar包跟上个一样 3.重写set方法 UserServiceImpl.java 1234 ...

  9. spring学习笔记(七)HttpMessageConverter

    spring学习笔记(七)HttpMessageConverter 1. HttpMessageConverter的加载 2. 从StringMessageConverter探究消息转换器的原理 1. ...

随机推荐

  1. Hive文件与记录格式

    1. Hive文件与记录格式 Create table 有多种用法,例如STORED AS SEQUENCEFILE, ROW FORMAT DELIMITED, SERDE, INPUTFORMAT ...

  2. 并发之ATOMIC原子操作--CAS乐观锁原理(二)

    1.乐观锁介绍 程序完成并发操作时,访问数据时每次不加锁,假设没有冲突去完成某项操作,如果因为冲突失败就重试,直到成功为止.就是当去做某个修改或其他操作的时候它认为不会有其他线程来做同样的操作(竞争) ...

  3. webpack4.41.0配置二(加载器_url-loader/babel-loader/sass-loader)

    loader是webpack用来预处理源文件的,比如typesrcipt形式的文件最终都得转成浏览器可以执行的js文件 (注:以下的配置代码不一定与下方一摸一样,具体与官网上https://webpa ...

  4. centos 安装mindoc 二进制安装

    自建 文档管理系统或者说 wiki系统 mindoc官网: https://www.iminho.me/#%E6%BC%94%E7%A4%BA mindoc github页面:https://gith ...

  5. centos useradd 命令详解

    useradd 命令 Usage: useradd [options] LOGIN useradd -D useradd -D [options] Options: -b, --base-dir BA ...

  6. HTML学习(11)表格

    HTML表格由<table>标签定义,下面是一个2行3列的表格: <table> <tr> <td>11</td> <td>12 ...

  7. Spark学习笔记1

    趁着工作业余时间,趁着内心对技术追求的热情,还是对Spark这个大数据内存计算框架动手了,毕竟人与人之间的差距都是在工作业余时间拉开的…… Spark官网:http://spark.apache.or ...

  8. GitHub项目简介

    为了存放代码新建了一个GitHub账号,存放了一些比较常用的代码块,上面的模块大部分都能找到 index.html 文件直接在浏览器打开. 地址:https://github.com/liuzhou1 ...

  9. 每天进步一点点------Alpha半透明图形叠加算法Matlab+Verilog实现

    Alpha图形叠加算法Matlab+Verilog实现 1.1. Alpha算法的研究 Alpha通道是一个8位的灰度通道,该通道用256级灰度来记录图像中的透明度信息,定义透明.不透明和半透明区域, ...

  10. jmeter的BeanShell Sampler使用--导入第三方jar包

    实现目的 测试接口的过程中,可能有时需要用到第三方jar包来生成一些测试数据,此时我们就可以通过BeanShell来调用自己编写的工具类,来对jmeter的功能进行扩展,以满足测试需要. 脚本实现 在 ...