Spring基于注解开发的注解(spring底层基础的注解例如@Configuration@Autowired@Required等等注解的实现支持)

AnnotationConfigApplicationContext,基于注解的分析

AnnotationConfigApplicationContext初始化

属性

private final AnnotatedBeanDefinitionReader reader;

构造函数

public AnnotationConfigApplicationContext() {
	this.reader = new AnnotatedBeanDefinitionReader(this);//初始化加载,注解Bean定义读取器
	this.scanner = new ClassPathBeanDefinitionScanner(this);
}

起父类完成了beanFactory的初始化【后面要用,这里先分析到】

GenericApplicationContext
//如果后面子类或者方法要获取beanFactory实例的时候,直接获取即可
public GenericApplicationContext() {
	this.beanFactory = new DefaultListableBeanFactory();
}

属性分析

先看类图结构,这里实现了BeanDefinitionRegistry接口


AnnotatedBeanDefinitionReader详解


public AnnotationConfigApplicationContext() {
    //构造方法初始化的时候,完成了对AnnotatedBeanDefinitionReader的初始化,并且把spring上下文给传了进去;通过上面类图分析,这个this--AnnotationConfigApplicationContext implements BeanDefinitionRegistry接口【后面要用==这里先记录好】
	this.reader = new AnnotatedBeanDefinitionReader(this);//初始化加载,注解Bean定义读取器
}
实例化
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);
        //通过AnnotationConfigUtils工具类,注册注解配置处理器
		AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
}

--registerAnnotationConfigProcessors
public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
			BeanDefinitionRegistry registry, @Nullable Object source) {
    //获取beanFactory,里面很简单就是判断当前提供的registry是DefaultListableBeanFactory还是GenericApplicationContext,这里是GenericApplicationContext,要获取
	//beanFactory需要获取AnnotationConfigApplicationContext的getBeanFactory方法,因为beanFactory是AnnotationConfigApplicationContext的属性(其父类GenericApplicationContext持有)
	DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
	if (beanFactory != null) {
		if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) {
			beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);//加入AnnotationAware排序比较器
		}
		if (!(beanFactory.getAutowireCandidateResolver() instanceof ContextAnnotationAutowireCandidateResolver)) {
			beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
		}
	}
    //【以下部分很重要】
	//也就是说,我们用到的所有注解都是通过初始化的方式先把这些beanDefinition设置到beanFactory中
	Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<>(8);//BeanDefinitionHolder持有BeanDefinitional和beanName
	//CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME=org.springframework.context.annotation.internalConfigurationAnnotationProcessor BeanNameGenerator
	//AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME=org.springframework.context.annotation.internalAutowiredAnnotationProcessor @Autowired
	//REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME=org.springframework.context.annotation.internalRequiredAnnotationProcessor @Required支持
	//COMMON_ANNOTATION_PROCESSOR_BEAN_NAME=org.springframework.context.annotation.internalCommonAnnotationProcessor JSR-250支持
	//PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME=org.springframework.context.annotation.internalPersistenceAnnotationProcessor  JPA支持
	//PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME=org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor 
	//EVENT_LISTENER_PROCESSOR_BEAN_NAME=org.springframework.context.event.internalEventListenerProcessor @EventListener
	//EVENT_LISTENER_FACTORY_BEAN_NAME=org.springframework.context.event.internalEventListenerFactory EventListenerFactory
	//看看beanFactory是否存在beanName定义名称为org.springframework.context.annotation.internalConfigurationAnnotationProcessor的
	if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
		//如果不存在,将org.springframework.context.annotation.ConfigurationClassPostProcessor注册到beanDefinition中【@Configuration】注解实现
		RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
		def.setSource(source);
		beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
	}

	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));
	}

	// 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));
	}

	// 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));
	}

	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));
	}

	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;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值