Spring 概览

2021-06-23  本文已影响0人  美美的苹果核

Spring概览

核心类

生命周期

为什么要用三级缓存,一是并发可能获得半成品的实例,二是AOP需要生成代理对象

AbstractAutowireCapableBeanFactory.doCreateBean
  1. 创建对象A,并放入缓存
  2. 发现A依赖B且B不存在
  3. 创建对象B
  4. 发现B依赖A
  5. 从一级缓存取A,取不到就去二级缓存,还取不到就去三级缓存
  6. 取到A后完成B的创建
  7. 继续A的初始化,完成对象创建
  8. B引用的A已经是完整对象了

AOP

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AopAspect {

    //定义切点
    @Pointcut("execution(* example.aop.*.*(..))")
    public void aopPointcut() {
    }

    //前置通知
    @Before("aopPointcut()")
    public void before() {
        System.out.println("前置通知");
    }

    //后置通知
    @After("aopPointcut()")
    public void after() {
        System.out.println("后置通知");
    }

    //环绕通知
    @Around("aopPointcut()")
    public void around(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("环绕通知:环绕前");
        pjp.proceed();//执行方法
        System.out.println("环绕通知:环绕后");
    }
}

Spring 事务

参考地址

Spring MVC

整体流程

Spring 扩展点

异常处理

@ExceptionHandler + @ControllerAdvice

/**
 * 统一错误处理入口
 */
@ControllerAdvice(basePackages = "com.finup.coffee.controller")
public class ErrorAdvice {
    private static final Logger LOGGER = LoggerFactory.getLogger(ErrorAdvice.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseVo<String> handleControllerException(Exception ex) {
        LOGGER.error("接口错误统一处理:", ex);
        String errorCode = "";
        String errorMessage = "";
        if (ex instanceof BusinessException) {
            errorCode = ((BusinessException) ex).getErrorCode();
            errorMessage = ex.getMessage();
        } else if (ex instanceof IllegalArgumentException) {
            errorCode = ResponseCode.PARAMETER_ERROR.getValue();
            errorMessage = ex.getMessage();
        } else {
            errorCode = ResponseCode.SERVER_ERROR.getValue();
            errorMessage = ResponseCode.SERVER_ERROR.getMessage();
        }
        return ResponseVo.ofError(errorCode, errorMessage);
    }
}

过滤器 Filter

Filter前处理 --> Interceptor前处理 --> controller--> Interceptor后处理 --> Filter后处理

拦截器 HandlerInterceptor

AOP切面

xxxAware接口

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

//Bean层面执行
public class LifeCircleService implements BeanNameAware, ApplicationContextAware, InitializingBean, DisposableBean {
    @Override
    public void setBeanName(String name) {
        System.out.println("BeanNameAware.setBeanName");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("DisposableBean.destroy");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean.afterPropertiesSet");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("ApplicationContextAware.setApplicationContext");
    }
}

xxxPostProcessor

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;

//容器层面执行
public class CustomInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        return null;
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        return false;
    }

    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        return null;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return null;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return null;
    }
}

导入实例 @import

Spring Boot

运行器 Runner

SpringApplication.run(ApplicationBoot.class,args)通过构造方法初始化,加载resource(main方法类),判断当前是否是web环境并初始化 ConfigurableApplicationContext,并在refresh的时候启动tomcat

上一篇 下一篇

猜你喜欢

热点阅读