Spring笔记思维导图

Spring

几个重要概念

  • Spring容器(ApplicationContext)
  • 依赖注入/控制反转

Bean的作用域

  • singleton
  • prototype
  • request
  • session
  • global session

创建Bean的3种方式

  • 构造器
  • 静态工厂
    • factory-method
    • constructor-args
    • property
  • 动态工厂
    • factory-bean
    • factory-method
    • constructor-args
    • property

Bean的生命周期

    • image-20220630141243852
  • @InitMethod 和 @destoryMethod

    • @InitMethod指定Bean实例化后的一些操作。

    • @destoryMethod指定Bean销毁前的一些操作。

    • Web应用随着Web应用的关闭而关闭容器, 且回收Bean

    • 非Web引用调用ctx.registerShutdownHook方法来标明JVM关闭前回收Spring容器和Bean。

  • 两种bean

    • singleton Bean
      • 由Spring容器管理Bean的创建,销毁
    • prototype Bean
      • 每次用的时候创建,JVM垃圾回收销毁

工厂bean

  • PropertyPathFactoryBean
    • util:propery-path
  • FieldRetriecingFactoryBean
    • util:constant
  • MehodInvokingFactoryBean

Bean工厂和工厂Bean

常用的注解

  • Java配置类
    • @Configuration
    • @Bean
    • @Value
    • @ImportResource
  • Spring Bean类
    • @Component
      • 普通Spring Bean类
    • @Controller
      • 控制器类
    • @Service
      • 业务逻辑类
    • @Repository
      • Dao类
    • <context: component-scan>配置bean包

ApplicationContext

  • Spring容器
  • 启动的时候创建所有的singleton Bean
  • 国际化支持
    • 继承了MessageSource
    • ctx.getMessage()
  • 实例化的ApplicationContext的时候可以指定容器的多个配置文件
  • 事件源 ctx.publishEvent(Eventbean)
    • ApplicationEvent 事件Bean
    • ApplicationListener 监听器
  • 与BeanFactory的区别
    • BeanFactory是ApplicationContext的父接口
    • BeanFactory不会再创建的时候初始化singleton Bean
  • 常用方法
    • getBean
    • getMessage(获取国际化资源)
    • pushEvent(发出一个ApplicationEvent Bean事件)
    • registerShutdowenHook(非Web项目,配置JVM关闭前回收Spring容器和Bean)

简化配置

  • p: p命名空间,修饰实例变量
  • c: c命名空间, 修饰构造参数
  • util:
    • constant
    • property-path
    • lilst
    • set
    • map
    • properties

访问资源

  • Resource接口
    • 实现类
      • UrlResource
      • ClassPathResource
      • FileSystemResource
      • ServletContextResource
      • InputStreamResource
      • ByteArrayResource
  • ResourceLoader

AOP

  • 基本概念

    • 切面 Aspect
      • 修饰Aop的描述类
    • 连接点 JoinPoint
      • 具体的方法点
    • 通知 Advice
      • 对方法的增强处理
    • 切入点 Pointcut
      • 对那些方法进行织入操作
    • 织入过程 Weaving
      • aop动态代理这个动作叫做织入
  • 常用注解

    • @Aspect
    • @Pointcut
    • @After
    • @Before
    • @AfterReturning
    • @AfterThrowing
    • @Around
  • demo

    • package com.linjie.aop;
      
      import java.util.Arrays;
      
      import org.aopalliance.intercept.Joinpoint;
      import org.aspectj.lang.JoinPoint;
      import org.aspectj.lang.ProceedingJoinPoint;
      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;
      
      /**
       * @author LinJie
       * log功能,不影响核心业务
       */
      @Component("logginAspectJ")
      @Aspect
      public class LogginAspectJ {
          /*
           *定义一个方法,用于声明切点表达式,该方法一般没有方法体
           *@Pointcut用来声明切点表达式
           *通知直接使用定义的方法名即可引入当前的切点表达式 
           */
          @Pointcut("execution(* com.linjie.aop.Arithmetic.*(..))")
          public void PointcutDeclaration() {}
      
          //前置通知,方法执行之前执行
          @Before("PointcutDeclaration()")
          public void BeforeMethod(JoinPoint jp) {    
              String methodName = jp.getSignature().getName();
              Object[] args = jp.getArgs();
              System.out.println("BeforeMethod  The method   "+ methodName +"   parameter is  "+ Arrays.asList(args));
              System.out.println("add before");
              System.out.println();
          }
      
          //后置通知,方法执行之后执行(不管是否发生异常)
          @After("PointcutDeclaration()")
          public void AfterMethod(JoinPoint jp) {
              String methodName = jp.getSignature().getName();
              Object[] args = jp.getArgs();
              System.out.println("AfterMethod  The method    "+ methodName +"   parameter is  "+Arrays.asList(args));
              System.out.println();
          }
      
          //返回通知,方法正常执行完毕之后执行
          @AfterReturning(value="PointcutDeclaration()",returning="result")
          public void AfterReturningMethod(JoinPoint jp,Object result) {
              String methodName = jp.getSignature().getName();
              Object[] args = jp.getArgs();
              System.out.println("AfterReturningMethod  The method   "+ methodName +"   parameter is  "+Arrays.asList(args)+" "+result);
              System.out.println();
          } 
      
          //异常通知,在方法抛出异常之后执行
          @AfterThrowing(value="PointcutDeclaration()",throwing="e")
          public void AfterThrowingMethod(JoinPoint jp,Exception e) {
              String methodName = jp.getSignature().getName();
              System.out.println("AfterThrowingMethod  The method   "+ methodName +"exception :"+e);
          }
      }
      --------------------- 
      版权声明:本文为CSDN博主「浅然_」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
      原文链接:https://blog.csdn.net/w_linux/article/details/80230222
      
    • 应用场景

      • 记录日志

      • 一致性逻辑处理

      • AOP声明式事务

        • transaction.comit
          
          .......方法
          
          trasaction.rollback
          

事务

  • @Transactional

    • 修饰类
      • 对类中的所有方法有效
    • 修饰方法
      • 对这个修饰的类有效
    • 属性
      • isolation
        • 事务隔离级别, 默认为数据源隔离级别
      • noRollbackFor
        • 对于某个指定异常事务不回滚
      • noRollbackForClassName
        • 对于多个指定异常,事务不回滚
      • propagation
        • 事务传播行为
      • rollbackFor
        • 对于某个指定的异常, 强制回滚
      • rollbackForClassName
        • 对于多个指定的异常, 强制回滚
      • readOnly
        • 只读.一般对读取数据源的方法都可以加
      • timeout
        • 事务超时时长,如果指定时长还没有执行完该事务, 强制回滚.
  • 全局事务

    • 对所有的数据源有效(如JTA)
  • 局部事务

    • 在单个数据源里面配置, 对单个数据源有效

缓存

  • 内置缓存(不推荐使用)
  • 第三方缓存(推荐使用EhCache),通过配置加载
    • 参数组合为缓存key
    • 第一次进缓存,第二次从缓存中取
    • @Cacheable
      • 修饰类
        • 调用该类的任意方法, 只要传入的参数相同,就是同一个缓存
      • 修饰方法
        • 只要传入的参数相同,就是同一个缓存
      • 注意如何缓存和修饰的方法名和类名无关,只和参数有关, 因为是参数组合形成唯一的KEY
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BinBin_Bang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值