Spring

IOC

Spring Bean作用域

  • singleton
    单例注册表的单例模式实现,每个Bean在SpringIOC容器中只存在一个实例,会缓存Bean实例。
    默认启动容器就创建了对象,如果设置lazy-init=true,则在调用getBean方法的时候创建。

  • prototype
    每次getBean的时候都返回一个全新的Bean,不缓存Bean实例

  • request
    每个Http请求创建一个Bean实例

  • session
    每个Session创建一个Bean实例

  • 自定义作用域

    1. 实现org.springframework.beans.factory.config.Scope接口
    2. 向Spring容器注册新的作用域
      • ConfigurableBeanFactory.registerScope(String scopeName, Scope scope)

Spring Bean的生命周期

  1. BeanFactoryPostProcessor#postProcessBeanFactory
  2. InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation
  3. 执行Bean的构造函数
  4. InstantiationAwareBeanPostProcessor#postProcessAfterInstantiation
  5. InstantiationAwareBeanPostProcessor#postProcessPropertyValues
  6. Bean属性注入
  7. org.springframework.beans.factory.BeanNameAware#setBeanName
  8. org.springframework.beans.factory.BeanClassLoaderAware#setBeanClassLoader
  9. org.springframework.beans.factory.BeanFactoryAware#setBeanFactory
  10. org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization
    1. ApplicationContextAwareProcessor#postProcessBeforeInitialization
      1. org.springframework.context.EnvironmentAware#setEnvironment
      2. org.springframework.context.EmbeddedValueResolverAware#setEmbeddedValueResolver
      3. org.springframework.context.ResourceLoaderAware#setResourceLoader
      4. org.springframework.context.ApplicationEventPublisherAware#setApplicationEventPublisher
      5. org.springframework.context.MessageSourceAware#setMessageSource
      6. org.springframework.context.ApplicationContextAware#setApplicationContext
    2. ConfigurationClassPostProcessor.ImportAwareBeanPostProcessor#postProcessBeforeInitialization
      1. org.springframework.context.annotation.ImportAware#setImportMetadata
  11. org.springframework.beans.factory.InitializingBean#afterPropertiesSet
  12. 调用bean的init-method方法
  13. org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization
  14. 初始化完成
  15. 调用DisposableBean的destory方法
  16. 调用bean的destory-method方法

Spring Bean注入方式

  • 注解注入

  • 构造方法注入

  • setter注入

BeanFactory和FactoryBean、ApplicationContext 的区别

  • BeanFactory

    IOC容器的最基本形式;对象工厂;获取spring中注入的bean都需要继承此接口;XmlWebApplicationContext实现了此接口

  • FactoryBean

    IOC容器中的一个Bean

    框架中的实现类

    • Mybatis

      • SqlSessionFactoryBean
      • MapperFactoryBean
    • Dubbo

      • ReferenceBean
    • Spring Cloud OpenFeign

      • FeignClientFactoryBean
  • ApplicationContext

    ApplicationContext接口对BeanFactory进行了扩展

    • 支持信息源,可以实现国际化(实现 MessageSource 接口)
    • 访问资源(实现 ResourcePatternResolver 接口)
    • 支持应用事件(实现 ApplicationEventPublisher 接口)

@Autowired和@Resource的区别

  • @Autowired和@Inject

    默认按照类型装配,如果想按照名称来装配,则需要结合@Qualifier才能使用名称

  • @Resource

    默认按照名称装配,如果找不到与名称匹配的bean才会按照类型来装配;

Bean循环注入

出现循环注入的方式

  • 原型模式(prototype)的setter方式注入
  • 构造函数参数循环依赖

解决循环注入的方式

  • setter单例模式

AOP

动态代理

JDK动态代理

基于接口的动态代理

CGLIB动态代理

基于类的动态代理,采用的继承的方式,覆盖父类方法,所以不能对final类生成代理

相关术语

  • 连接点(Joinpoint)被拦截到的点,具体方法
  • 切点(Pointcut)指定对哪些Joinpoint进行拦截,可以同时指定多个函数,一般使用正则表达式
  • 通知(Advice)
    • 前置通知(Before)
    • 后置通知(AfterReturning)
    • 异常通知(AfterRThrowing)
    • 最终通知(After)
    • 环绕通知(Around)
  • 引介(Introduction)
  • 织入(Weaving)
    • 编译期织入(AspectJ采用的方式)需要特殊的Java编译期(例如AspectJ的ajc)
    • 装载期织入(AspectJ采用的方式)要求使用特殊的类加载器,在装载类的时候对类进行增强
    • 运行时织入(Spring采用的方式)在运行时为目标类生成代理实现增强
  • 切面(Aspect)切入点和通知的结合

事务管理

传播机制

  • propagation_required

    (默认)支持当前事务,如果没有就新建一个

  • propagation_supports

    支持当前事务,如果当前没有事务,就以非事务执行;

  • propagation_mandatory

    支持当前事务,如果当前没有事务,就抛出异常;

  • propagation_not_supported

    以非事务方式执行操作,如果当前存在事务,就把当前事务挂起;

  • propagation_never

    以非事务方式执行,如果当前存在事务,则抛出异常;

  • propagation_requires_new

    新建事务,如果当前存在,把当前事务挂起;外层事务失败回滚,不能回滚内层事务;内层事务失败抛出异常,外层事务可以不处理回滚操作;

  • propagation_nested

    如果一个活动的事务存在,则运行在一个嵌套的事务中;如果没有活动事务,则按required执行;内部事务回滚不会对外部事务造成影响;

隔离级别

  • 读未提交(ISOLATION_READ_UNCOMMITTED)

    这种隔离级别会产生脏读, 不可重复读和幻读

  • 读已提交(ISOLATION_READ_COMMITTED)

    这种隔离级别会产生不可重复读和幻读

  • 可重复读(ISOLATION_REPEATABLE_READ)

    这种隔离级别会产生幻读

  • 串行化读(ISOLATION_SERIALIZABLE)

设计模式

  • 单例模式(IOC)
  • 代理模式(AOP)
  • 工厂方法模式
    • 工厂方法模式
  • 简单工厂模式
    • BeanFactory
  • 策略模式
    • DefaultAopProxyFactory
      • JdkDynamicAopProxy
      • ObjenesisCglibAopProxy
  • 模板方法模式
    • JdbcTemplate
    • RestTemplate

Spring 5 新特性

  • 支持Java9,Spring5框架基于Java8开发

  • 支持响应式编程

  • 函数式Web框架

  • 支持Kotlin