1、设置configLocation并调用refresh开始刷新上下文
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)throws BeansException {
super(parent);
setConfigLocations(configLocations);
if (refresh) {
refresh();
}
}
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
**finishBeanFactoryInitialization(beanFactory);**
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
}
}
2、解析配置文件得到Bean名称集合和定义集合
public class DefaultListableBeanFactory .....{
......
// Bean定义集合
private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>(64);
// Bean名称集合
private final List<String> beanDefinitionNames = new ArrayList<String>();
..........
}
3、遍历Factory中的Bean
public void preInstantiateSingletons() throws BeansException {
List beanNames;
synchronized (this.beanDefinitionMap) {
beanNames = new ArrayList(this.beanDefinitionNames);
}
for (String beanName : beanNames) {
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
if (isFactoryBean(beanName)) {
final FactoryBean> factory = (FactoryBean>) getBean(FACTORY_BEAN_PREFIX + beanName);
boolean isEagerInit;
if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
isEagerInit = AccessController.doPrivileged(new PrivilegedAction() {
@Override
public Boolean run() {
return ((SmartFactoryBean>) factory).isEagerInit();
}
}, getAccessControlContext());
}
else {
isEagerInit = (factory instanceof SmartFactoryBean &&
((SmartFactoryBean>) factory).isEagerInit());
}
if (isEagerInit) {
getBean(beanName);
}
}
else {
getBean(beanName);
}
}
}
}
4、判断Bean类型针对性处理
protected T doGetBean(final String name, final Class requiredType, final Object[] args, boolean typeCheckOnly)throws BeansException {
final String beanName = transformedBeanName(name);
Object bean;
// Eagerly check singleton cache for manually registered singletons.
// (2)先从单例注册表(缓存)中查找
Object sharedInstance = getSingleton(beanName);
.......
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
} else {
.........
try {
// (3)如果缓存中没有,则获取Bean的定义,为创建Bean做准备
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
checkMergedBeanDefinition(mbd, beanName, args);
.........
// Create bean instance.
// (4)如果Bean被配置为单例,则走单例创建方法,创建完成要注册到单例缓存中
if (mbd.isSingleton()) {
sharedInstance = getSingleton(beanName, new ObjectFactory
5、Bean实际通过反射技术创建而不是new出来的
public static T instantiateClass(Constructor<T> ctor, Object... args) throws BeanInstantiationException {
Assert.notNull(ctor, "Constructor must not be null");
try {
ReflectionUtils.makeAccessible(ctor); // 强制构造函数可用
return ctor.newInstance(args); // 创建实例对象,同Class.newInstance作用一样
}
........
}
6、调用BeanPostProcessors进行后续处理