【惊】Spring源码的秘密|一起看看Spring启动时究竟做了什么惊天动地的事情?

10 篇文章 2 订阅
10 篇文章 1 订阅

初识Spring

入口代码

在启动Spring的时候,我们是编写如下的代码;本文我们分析的是创建AnnotationConfigApplicationContext()

public class SpringstudyApplication {

    public static void main(String[] args) {
        // 本文分析这一行
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        // 将类转化为bd并注入bean工厂
        applicationContext.register(SpringstudyApplication.class);
        applicationContext.refresh();
    }
}
大致流程

在这里插入图片描述

代码分析
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
[1]AnnotationConfigApplicationContext剖析

如下图是该类的继承关系图,他就是整个sping的上下文,他可以进行类的解析,扫描,转换bd等等。那么我们启动spring的时候,就由他来初始化扫描器,解析器,类加载器,环境,过滤链等一系列初始化操作;

在这里插入图片描述

public class AnnotationConfigApplicationContext extends GenericApplicationContext implements AnnotationConfigRegistry {
	public AnnotationConfigApplicationContext() {
        // 创建一个类注解解析器,将上下文转换成了一个bean工厂(DefaultListableBeanFactory),
        // bean工厂设置bean优先级排序器以及延迟加载等
        // bean工厂注入进spring内置的8个基本bean
		this.reader = new AnnotatedBeanDefinitionReader(this);
        // 创建一个类的扫描器,可以扫描@Compoment等注解修饰的类
		this.scanner = new ClassPathBeanDefinitionScanner(this);
	}
}
[2]AnnotatedBeanDefinitionReader 剖析

这里呢是经历了多个构造方法才到了这里,他主要是定义了注册器(即spring上下文),还有系统环境对象,以及调用AnnotationConfigUtils中的方法,对bean工厂进行定义,以及扫描注解时进行初始化设置;

在这里插入图片描述

参数:

  • registry:就是spring的上下文annotationConfigApplicationContext,因为他继承了Registry;
  • environment:是new了一个StandardEnvironment,他可以获取系统属性值,环境变量等;
public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment) {
		Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
		Assert.notNull(environment, "Environment must not be null");
		this.registry = registry;
		this.conditionEvaluator = new ConditionEvaluator(registry, environment, null);
    	// 这块就是取出Bean工厂等步骤
		AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
	}
registerAnnotationConfigProcessors 方法
  1. 这个方法属于AnnotationConfigUtils工具类中的,首先从上下文中获取bean工厂,由于AnnotationConfigApplicationContext是继承了GenericApplicationContext了,所以直接返回GenericApplicationContext.beanFactory;
private static DefaultListableBeanFactory unwrapDefaultListableBeanFactory(BeanDefinitionRegistry registry) {
		if (registry instanceof DefaultListableBeanFactory) {
			return (DefaultListableBeanFactory) registry;
		}
        // 走这里
		else if (registry instanceof GenericApplicationContext) {
			return ((GenericApplicationContext) registry).getDefaultListableBeanFactory();
		}
		else {
			return null;
		}
	}

// 然后由于在GenericApplicationContext构造方法中,bean工厂被初始化
public GenericApplicationContext() {
		this.beanFactory = new DefaultListableBeanFactory();
}
  1. bean工厂设置对@Order和@Priority进行扫描排序以及设置对bean的延迟加载与自动注入等

  2. 注册初始的6个后置处理器(其中一个已经被废弃了),将其变成bd注册进工厂

    • 将class变成bd的过程,可以交给RootBeanDefinition实现

      RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
      
beanNamebeanClass目的备注
org.springframework .context .annotation .internalConfigurationAnnotationProcessorConfigurationClassPostProcessor.class后置处理器 扫描Configuration或者是加了@Component、@ComponentScan、@Import、@ImportResource或者是方法上加了@Bean的注解
org.springframework. context.annotation. internalAutowiredAnnotationProcessorAutowiredAnnotationBeanPostProcessor.class后置处理器 属性注入
org.springframework.context. annotation.internalRequiredAnnotationProcessorRequiredAnnotationBeanPostProcessor.class后置处理器 校验属性上是否有@Required并且是否注入值V5.1后取消了
org.springframework.context. annotation.internalCommonAnnotationProcessorCommonAnnotationBeanPostProcessor.class后置处理器 解析@Resource、@WebServiceRef、@EJB三个注解
org.springframework.context. annotation.internalPersistenceAnnotationProcessorClassUtils.forName(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME)后置处理器 对JPA进行支持 解析@PersistenceUnit和@PersistenceContext若不引入jpa则不注入进bean工厂
org.springframework.context. event.internalEventListenerProcessorEventListenerMethodProcessor.class后置处理器 解析@EventListener 设置一个默认的监听器工厂
org.springframework.context. event.internalEventListenerFactoryDefaultEventListenerFactory.class后置处理器 也是处理@EventListener
public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
			BeanDefinitionRegistry registry, @Nullable Object source) {
		// 根据registry获取registry中的bean工厂
		DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
		if (beanFactory != null) {
			// bean工厂可以对@Order和@Priority进行扫描排序
			if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) {
				beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
			}
			// bean工厂可以设置对bean的延迟加载与自动注入等
			if (!(beanFactory.getAutowireCandidateResolver() instanceof ContextAnnotationAutowireCandidateResolver)) {
				beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
			}
		}

		Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<>(8);

		// 【bean工厂】后置处理器 扫描Configuration或者是加了@Component、@ComponentScan、@Import、@ImportResource或者是方法上加了@Bean的注解
		if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
		}
		// 【bean】后置处理器 属性注入
		if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
		}
		// 【bean】后置处理器 校验属性上是否有@Required并且是否注入值[V5.1后取消了]
		if (!registry.containsBeanDefinition(REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(RequiredAnnotationBeanPostProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
		}

		// 【bean】后置处理器 解析@Resource、@WebServiceRef、@EJB三个注解
		// Check for JSR-250 support, and if present add the CommonAnnotationBeanPostProcessor.
		if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
		}

		// 后置处理器 对JPA进行支持 解析@PersistenceUnit和@PersistenceContext 若不引入jpa则不注入进bean工厂
		// Check for JPA support, and if present add the PersistenceAnnotationBeanPostProcessor.
		if (jpaPresent && !registry.containsBeanDefinition(PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition();
			try {
				def.setBeanClass(ClassUtils.forName(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME,
						AnnotationConfigUtils.class.getClassLoader()));
			}
			catch (ClassNotFoundException ex) {
				throw new IllegalStateException(
						"Cannot load optional framework class: " + PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME, ex);
			}
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));
		}

		// 后置处理器  解析@EventListener 设置一个默认的监听器工厂
		if (!registry.containsBeanDefinition(EVENT_LISTENER_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(EventListenerMethodProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME));
		}

		// 后置处理器 也是处理@EventListener
		if (!registry.containsBeanDefinition(EVENT_LISTENER_FACTORY_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(DefaultEventListenerFactory.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME));
		}

		return beanDefs;
	}
[3]ClassPathBeanDefinitionScanner剖析

上面创建完bean注解解析器后,这里创建BeanDefinition扫描器,我们可以用他来扫描包或者类,然后转换为bd

this.scanner = new ClassPathBeanDefinitionScanner(this);
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
			Environment environment, @Nullable ResourceLoader resourceLoader) {

		Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
		this.registry = registry;
		// 初始时进入这里为true
		if (useDefaultFilters) {
			// 加了指定注解的类会被扫描到 带有@Component,@Repository,@Service,@Controller,@ManagedBean,@Named
			registerDefaultFilters();
		}
		// 设置环境(可获取系统变量,环境变量等)
		setEnvironment(environment);
		// 设置类加载器(上下文实现了ResourceLoader接口,这里塞进去的就是上下文)
		setResourceLoader(resourceLoader);
	}
registerDefaultFilters剖析

这个方法表示:加了指定注解的类会被扫描到 ,比如带有@Component,@Repository,@Service,@Controller,@ManagedBean,@Named

protected void registerDefaultFilters() {
		// @Repository @Controller @Service 注解上面都加了@Component注解
		this.includeFilters.add(new AnnotationTypeFilter(Component.class));
		ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
		try {
			this.includeFilters.add(new AnnotationTypeFilter(
					((Class<? extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false));
			logger.debug("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
		}
		catch (ClassNotFoundException ex) {
			// JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.
		}
		try {
			this.includeFilters.add(new AnnotationTypeFilter(
					((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Named", cl)), false));
			logger.debug("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
		}
		catch (ClassNotFoundException ex) {
			// JSR-330 API not available - simply skip.
		}
	}
  • 13
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 23
    评论
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

BugGuys

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

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

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

打赏作者

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

抵扣说明:

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

余额充值