前言
IOC容器最核心的功能,加载Bean
Demo代码
java部分
package com.shenge.spring.ioc;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*IOC容器初始化
* 简单的获取Bean
* */
public class SpringIOC1 {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring_ioc_1.xml");
Object testBeean = context.getBean("testBeean");
System.out.println(testBeean.getClass().getName());
}
}
xml
流程图
-
ClassPathXmlApplicationContext构造函数的时候,做了什么
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring_ioc_1.xml");
上述流程图描述大概的流程,内容其实太多了,只关注本节核心部分
-
加载BeanDefination总结
- BeanDefination 顾名思义就是Bean定义的抽象,用于描述Bean的生命周期,className等属性
- 通过AbstractApplicationConext的核心方法refresh,加载新的BeanFactory
- refresh是个模版方法,实际调用AbstractRefreshableApplicationContext的refreshBeanFactory加载新的BeanFactory
- 加载新的BeanFactory的时候,最终调用XmlBeanDefinationReader解析xml成document
- 再由DefaultBeanDefinitionDocumentReader读取Document,解析成BeanDefination
- 最后再由BeanFactory将解析后的BeanDefination注册到BeanFactory内部的一个map当中,(Map
beanDefinitionMap)
-
实例话Bean总结
- 通过AbstractApplicationConext的核心方法refresh
- refresh是个模版方法,实际调用AbstractApplicationConext的finishBeanFactoryInitialization完成单例Bean的实例化
- AbstractApplicationConext 调用内部BeanFactory的preInstantiateSingletons实例化Bean
接口设计
每一个接口都代表一个最核心的功能模块,现在我们想看的是IOC容器怎么加载Bean的,那最核心的一定是BeanFactory
BeanFactory -------> ListableBeanFactory,HierarchicalBeanFactory
BeanFactory
根据Bean名称或类名或名称+构造参数等获取Bean实例
根据Bean名称判断容器内是否含有此Bean
根据Bean名称判断Bean是否单例 或者 多例
获取Bean的类型
判断Bean的类型
获取特殊Bean提供者(暂时不知道)
-
获取别名
ListableBeanFactory
根据Bean的名称获取Bean的元数据(BeanDefinition)
获取所有元数据的数量
根据Bean的类型获取Bean的name集合
根据Bean的类型取Bean的实例集合
根据注解获取Bean的name集合
根据注解获取Bean和注解的Map
获取Bean的注解
HierarchicalBeanFactory
得到父容器
根据Bean的额名称在本地容器查找是否存在
在Demo代码中我们用的ApplicationContext,即上下文
ApplicationContext ---------->ConfigurableApplicationContext ------->AbstractApplicationContext -------------> AbstractRefreshableApplictionContext------------->AbstractRefreshableConfigApplictionContext------------->AbstractXmlApplicationContext------------->ClassPathXmlApplicationContext
ApplicationContext
获取ApplicationContext的唯一ID
获取ApplicationContext的名称
获取ApplicationContext的friendly名称
获取ApplicationContext 启动时间
获取ApplicationContext 的父ApplicationContext
获取具有自动装配能力的容器(BeanFactory)
ConfigurableApplicationContext
设置context的唯一ID
设置context 的父ApplicationContext
设置conetext的environment
设置context的BeanFactoryPostProcessor
设置context的Application监听者
设置类加载器
设置协议解析器
刷新容器
注册关闭钩子
关闭容器
判断是否context激活
获取一个ConfigurableListableBeanFactory容器实例
抽象类
AbstractContextApplication
ApplicationConetxt接口实现部分
//---------------------------------------------------------------------
// Implementation of ApplicationContext interface
//---------------------------------------------------------------------
@Override
public String getId() {
return this.id;
}
@Override
public String getApplicationName() {
return "";
}
@Override
public String getDisplayName() {
return this.displayName;
}
@Override
@Nullable
public ApplicationContext getParent() {
return this.parent;
}
@Override
public ConfigurableEnvironment getEnvironment() {
if (this.environment == null) {
this.environment = createEnvironment();
}
return this.environment;
}
@Override
public AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException {
return getBeanFactory();
}
@Override
public long getStartupDate() {
return this.startupDate;
}
@Override
public void publishEvent(ApplicationEvent event) {
publishEvent(event, null);
}
@Override
public void publishEvent(Object event) {
publishEvent(event, null);
}
ConfigurableApplicationContext接口实现部分(getBeanFactory, setClassLoader,addProtocalResolver没有实现 )
@Override
public void setId(String id) {
this.id = id;
}
@Override
public void addApplicationListener(ApplicationListener> listener) {
Assert.notNull(listener, "ApplicationListener must not be null");
if (this.applicationEventMulticaster != null) {
this.applicationEventMulticaster.addApplicationListener(listener);
}
this.applicationListeners.add(listener);
}
@Override
public void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor) {
Assert.notNull(postProcessor, "BeanFactoryPostProcessor must not be null");
this.beanFactoryPostProcessors.add(postProcessor);
}
@Override
public void close() {
synchronized (this.startupShutdownMonitor) {
doClose();
// If we registered a JVM shutdown hook, we don't need it anymore now:
// We've already explicitly closed the context.
if (this.shutdownHook != null) {
try {
Runtime.getRuntime().removeShutdownHook(this.shutdownHook);
}
catch (IllegalStateException ex) {
// ignore - VM is already shutting down
}
}
}
}
@Override
public ConfigurableEnvironment getEnvironment() {
if (this.environment == null) {
this.environment = createEnvironment();
}
return this.environment;
}
@Override
public boolean isActive() {
return this.active.get();
}
@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) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
@Override
public void registerShutdownHook() {
if (this.shutdownHook == null) {
// No shutdown hook registered yet.
this.shutdownHook = new Thread(SHUTDOWN_HOOK_THREAD_NAME) {
@Override
public void run() {
synchronized (startupShutdownMonitor) {
doClose();
}
}
};
Runtime.getRuntime().addShutdownHook(this.shutdownHook);
}
}
@Override
public void setEnvironment(ConfigurableEnvironment environment) {
this.environment = environment;
}
@Override
public void setId(String id) {
this.id = id;
}
@Override
public void setParent(@Nullable ApplicationContext parent) {
this.parent = parent;
if (parent != null) {
Environment parentEnvironment = parent.getEnvironment();
if (parentEnvironment instanceof ConfigurableEnvironment) {
getEnvironment().merge((ConfigurableEnvironment) parentEnvironment);
}
}
}
BeanFactory接口实现部分
//---------------------------------------------------------------------
// Implementation of BeanFactory interface
//---------------------------------------------------------------------
@Override
public Object getBean(String name) throws BeansException {
assertBeanFactoryActive();
return getBeanFactory().getBean(name);
}
@Override
public T getBean(String name, Class requiredType) throws BeansException {
assertBeanFactoryActive();
return getBeanFactory().getBean(name, requiredType);
}
@Override
public Object getBean(String name, Object... args) throws BeansException {
assertBeanFactoryActive();
return getBeanFactory().getBean(name, args);
}
@Override
public T getBean(Class requiredType) throws BeansException {
assertBeanFactoryActive();
return getBeanFactory().getBean(requiredType);
}
@Override
public T getBean(Class requiredType, Object... args) throws BeansException {
assertBeanFactoryActive();
return getBeanFactory().getBean(requiredType, args);
}
@Override
public ObjectProvider getBeanProvider(Class requiredType) {
assertBeanFactoryActive();
return getBeanFactory().getBeanProvider(requiredType);
}
@Override
public ObjectProvider getBeanProvider(ResolvableType requiredType) {
assertBeanFactoryActive();
return getBeanFactory().getBeanProvider(requiredType);
}
@Override
public boolean containsBean(String name) {
return getBeanFactory().containsBean(name);
}
@Override
public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
assertBeanFactoryActive();
return getBeanFactory().isSingleton(name);
}
@Override
public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
assertBeanFactoryActive();
return getBeanFactory().isPrototype(name);
}
@Override
public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException {
assertBeanFactoryActive();
return getBeanFactory().isTypeMatch(name, typeToMatch);
}
@Override
public boolean isTypeMatch(String name, Class> typeToMatch) throws NoSuchBeanDefinitionException {
assertBeanFactoryActive();
return getBeanFactory().isTypeMatch(name, typeToMatch);
}
@Override
@Nullable
public Class> getType(String name) throws NoSuchBeanDefinitionException {
assertBeanFactoryActive();
return getBeanFactory().getType(name);
}
@Override
@Nullable
public Class> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {
assertBeanFactoryActive();
return getBeanFactory().getType(name, allowFactoryBeanInit);
}
@Override
public String[] getAliases(String name) {
return getBeanFactory().getAliases(name);
}
ListableBeanFactory的接口实现部分
@Override
public boolean containsBeanDefinition(String beanName) {
return getBeanFactory().containsBeanDefinition(beanName);
}
@Override
public int getBeanDefinitionCount() {
return getBeanFactory().getBeanDefinitionCount();
}
@Override
public String[] getBeanDefinitionNames() {
return getBeanFactory().getBeanDefinitionNames();
}
@Override
public String[] getBeanNamesForType(ResolvableType type) {
assertBeanFactoryActive();
return getBeanFactory().getBeanNamesForType(type);
}
@Override
public String[] getBeanNamesForType(ResolvableType type, boolean includeNonSingletons, boolean allowEagerInit) {
assertBeanFactoryActive();
return getBeanFactory().getBeanNamesForType(type, includeNonSingletons, allowEagerInit);
}
@Override
public String[] getBeanNamesForType(@Nullable Class> type) {
assertBeanFactoryActive();
return getBeanFactory().getBeanNamesForType(type);
}
@Override
public String[] getBeanNamesForType(@Nullable Class> type, boolean includeNonSingletons, boolean allowEagerInit) {
assertBeanFactoryActive();
return getBeanFactory().getBeanNamesForType(type, includeNonSingletons, allowEagerInit);
}
@Override
public Map getBeansOfType(@Nullable Class type) throws BeansException {
assertBeanFactoryActive();
return getBeanFactory().getBeansOfType(type);
}
@Override
public Map getBeansOfType(@Nullable Class type, boolean includeNonSingletons, boolean allowEagerInit)
throws BeansException {
assertBeanFactoryActive();
return getBeanFactory().getBeansOfType(type, includeNonSingletons, allowEagerInit);
}
@Override
public String[] getBeanNamesForAnnotation(Class extends Annotation> annotationType) {
assertBeanFactoryActive();
return getBeanFactory().getBeanNamesForAnnotation(annotationType);
}
@Override
public Map getBeansWithAnnotation(Class extends Annotation> annotationType)
throws BeansException {
assertBeanFactoryActive();
return getBeanFactory().getBeansWithAnnotation(annotationType);
}
@Override
@Nullable
public A findAnnotationOnBean(String beanName, Class annotationType)
throws NoSuchBeanDefinitionException {
assertBeanFactoryActive();
return getBeanFactory().findAnnotationOnBean(beanName, annotationType);
}
HierarchicalBeanFactory接口实现部分
@Override
@Nullable
public BeanFactory getParentBeanFactory() {
return getParent();
}
@Override
public boolean containsLocalBean(String name) {
return getBeanFactory().containsLocalBean(name);
}
三个抽象方法
protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException;
protected abstract void closeBeanFactory();
@Override
public abstract ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;
总结
- 最底层的ApplictionContext抽象类
- 类内置的一个BeanFacorty成员变量,它实现了BeanFactory,HierarchicalBeanFactory,ListableBeanFactory的所有接口。应该是采用了策略模式
- 细品ConfigurableApplicationContext,Configurable,可配置的,它继承于ApplicationContext,它有很多公共set方法,它允许其他对象修改其内部属性
- getBeanFactory继承于ConfigurableApplicationContext没有被实现,也就是说AbstractAppicationContext的实现类必须实现这个方法。不难发现,这其实是一个解耦的一个思想,AbstractAppicationContext作为最基础的抽象类,它需要把最灵活,最多变的东西抽离出去。这个东西就是BeanFactory,ConfigurableApplicationContext从这个接口已经开始提前做准备了。通过策略模式将IOC的实现BeanFactory剥离出去。将方法getBeanFactory交由子类实现,源码里有两个实现了此方法的实现类AbstractRefreshableApplicationContext和GenericApplicationContext
- DefaultListableBeanFactory是子类getBeanFactory返回的实际BeanFactory
- 核心方法refresh,模版方法,描述了整个ApplictionContext加载Bean的全过程,设置监听器,加载内部BeanFactory,实例化Bean,事件通知等等。其中最灵活,最多变的refreshBeanFactory被抽离了出去,交给子类实现
ClassPathXmlApplicationContext
- 10个构造方法
- 获取资源文件列表