前言
本文不讲解源码,仅记录加载过程中的一部分。看本文需要先知道spring对BeanDefinition的处理,对bean的实例化。
单元测试和配置
public class Config {
}
public class ContextLoadTest {
private ApplicationContext ctx = null;
@Before
public void setUp() {
ctx = new AnnotationConfigApplicationContext(Config.class);
}
@Test
public void testContextLoads() {
}
}
BeanDefinition的加载堆栈记载
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner at localhost:58478
Thread [main] (Suspended (breakpoint at line 846 in DefaultListableBeanFactory))
owns: ConcurrentHashMap<K,V> (id=111)
owns: Object (id=96)
DefaultListableBeanFactory.registerBeanDefinition(String, BeanDefinition) line: 846
ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(BeanMethod) line: 266
ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClass, ConfigurationClassBeanDefinitionReader$TrackedConditionEvaluator) line: 140
ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(Set<ConfigurationClass>) line: 116
ConfigurationClassPostProcessor.processConfigBeanDefinitions(BeanDefinitionRegistry) line: 320
ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(BeanDefinitionRegistry) line: 228
PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(Collection<BeanDefinitionRegistryPostProcessor>, BeanDefinitionRegistry) line: 272
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory, List<BeanFactoryPostProcessor>) line: 92
AnnotationConfigApplicationContext(AbstractApplicationContext).invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory) line: 687
AnnotationConfigApplicationContext(AbstractApplicationContext).refresh() line: 525
AnnotationConfigApplicationContext.<init>(Class<?>...) line: 84
BeanOverrideTest.setUp() line: 16
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]
NativeMethodAccessorImpl.invoke(Object, Object[]) line: 62
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43
Method.invoke(Object, Object...) line: 498
FrameworkMethod$1.runReflectiveCall() line: 50
FrameworkMethod$1(ReflectiveCallable).run() line: 12
FrameworkMethod.invokeExplosively(Object, Object...) line: 47
RunBefores.evaluate() line: 24
BlockJUnit4ClassRunner(ParentRunner<T>).runLeaf(Statement, Description, RunNotifier) line: 325
BlockJUnit4ClassRunner.runChild(FrameworkMethod, RunNotifier) line: 78
BlockJUnit4ClassRunner.runChild(Object, RunNotifier) line: 57
ParentRunner$3.run() line: 290
ParentRunner$1.schedule(Runnable) line: 71
BlockJUnit4ClassRunner(ParentRunner<T>).runChildren(RunNotifier) line: 288
ParentRunner<T>.access$000(ParentRunner, RunNotifier) line: 58
ParentRunner$2.evaluate() line: 268
BlockJUnit4ClassRunner(ParentRunner<T>).run(RunNotifier) line: 363
JUnit4TestReference.run(TestExecution) line: 86
TestExecution.run(ITestReference[]) line: 38
RemoteTestRunner.runTests(String[], String, TestExecution) line: 538
RemoteTestRunner.runTests(TestExecution) line: 760
RemoteTestRunner.run() line: 460
RemoteTestRunner.main(String[]) line: 206
Thread [ReaderThread] (Running)
值得留意的是:
ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(Set<ConfigurationClass>) line: 116
这里就是把BeanDefinition从configuration class加载进来的过程,类似解析xml。
BeanDefinition的注册:
org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(String, BeanDefinition)