@Configuration底层原理

 
AnnotationConfigApplicationContext act = new 
                                     AnnotationConfigApplicationContext(MyConfigure.class);
 Student stduent = (Student) act.getBean(Student.class);
 studnt.say();

进入 AnnotationConfigApplicationContext 的构造方法中

public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
		this();
		register(annotatedClasses);
		refresh();
	}

  其中  this() 方法调用了无参构造方法,由于 AnnotationConfigApplicationContext 继承 GenericApplicationContext ,

GenericApplicationContext  包含成员 变量  beanFactory (私有,但是子类可以通过 getBeanFactory()方法访问 )

 

那么问题来了,@ComponentScan标注的对象何时加入 beanDefinitionMap  中的呢?

其实很简单,我们可以通过 debug 模式,查看 beanDefinitionMap 的size大小

 

通过上图可以看到,在执行到 register()方法时,只有默认的6个(AnnotatedBeanDefinitionReader时spring默认创建的)。

此时  MyConfigure 类也没生成 beanDefinition ,猜测  register(annotatedClasses) 方法会将 MyConfigure生成 beanDefinition,F8执行,发现猜测时正确的。

 

通过此方法,我们就能看到,@ComponentScan标注的对象何时加入 beanDefinitionMap  中的,由于篇幅问题,就不再贴图了,直接上步骤。大家可以自己照这个方式进行debug查看。

 

 

未完,待续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值