AnnotationConfigApplication学习

书中人 2019年08月27日 1,813次浏览

测试类入口:

AnnotationConfigApplicationContextTests test = new AnnotationConfigApplicationContextTests();
test.scanAndRefresh();

类图如下。

由类机构图可以知道AnnotationConfigApplicationContext同时也是一个Registry、因此同事也具有注册bean定义的功能

创建AnnotationConfigApplicationContext的同时由父类GenericApplicationContext负责创建内部refresh工厂类

private final DefaultListableBeanFactory beanFactory;

	@Nullable
	private ResourceLoader resourceLoader;
	private final DefaultListableBeanFactory beanFactory;
    public class GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry {
	/**
	 * Create a new GenericApplicationContext.
	 * @see #registerBeanDefinition
	 * @see #refresh
	 * 由此可见内部默认的bean工厂依然是DefaultListableBeanFactory类型
	 */
	public GenericApplicationContext() {
		this.beanFactory = new DefaultListableBeanFactory();
	}
	
}

1.从测试类开始。一步一步debug、带着问题逐渐深入。

private final AnnotatedBeanDefinitionReader reader;
private final ClassPathBeanDefinitionScanner scanner;
	/**
	 * Create a new AnnotationConfigApplicationContext that needs to be populated
	 * through {@link #register} calls and then manually {@linkplain #refresh refreshed}.
	 */
	public AnnotationConfigApplicationContext() {
		this.reader = new AnnotatedBeanDefinitionReader(this);
		this.scanner = new ClassPathBeanDefinitionScanner(this);
	}