注解产生原因

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. 初识Docker:BusyBox容器后台运行失败

    1.问题描述:我在进行Docker网络实验时,使用docker  run  -d  busybox命令时,busybox无法保持后台长期运行. ============================ ...

  2. Linux上安装nodejs

    https://github.com/nodejs/node-v0.x-archive/wiki/Installing-Node.js-via-package-manager#debian-and-u ...

  3. ASP学习笔记1

    一.变量 1.1 声明变量 dim name name="Donald Duck" response.write("My name is: " & na ...

  4. 自定义虚拟机MAC地址 | 它与 VMware 预留的 MAC 冲突 解法

    https://blog.csdn.net/wangrui1573/article/details/82056020 问题:我想给VMware ESXi上的一台虚拟机分配一个静态的MAC地址.然而当我 ...

  5. 主席树+二分 p4602

    题意:给出每一种果汁的美味度,价格,升数: m个询问,每个询问给出最高上限的钱g,以及给出最少的w 意思是,最多用g的钱去买最少l的果汁,问能得到的最大美味度: 美味度是取所有果汁中美味度的最小值: ...

  6. Sass&Scss入门 选择器 混合器 导入 条件判断 迭代

    Sass 基于ruby的一种将脚本解析成CSS的脚本语言.也可以说是一种预处理语言. Sass在css的语法基础上增加了变量.嵌套.混合.继承.导入等高级功能. 使用Sass与Sass样式库(如com ...

  7. git 初次push

    1.本地仓库与远程仓库第一次同步时,一直同步不上 最后 git status ,发现有两个文件没提交 提交后再push即可 2.如果不行,再看一下其他情况

  8. Docker - 命令 - docker image

    概述 docker 客户端操控 镜像 1. 分类 概述 1 简单对 命令 做一些分类 分类 查看 ls inspect history 与 dockerhub 交互 pull push 导出 & ...

  9. Linux - Shell - date

    概述 date 命令 准备 OS CentOS 7.6 基本功能 显示时间 格式化时间 翻译时间 转换时间格式 切换时区 设置时间 查看文件最后使用时间 1. 显示时间 概述 基本功能 命令 # 内容 ...

  10. Go非缓冲/缓冲/双向/单向通道

    1. 非缓冲和缓冲 package main import ( "fmt" "strconv" ) func main() { /* 非缓冲通道:make(ch ...