1.建立beanFactory、BeanDefinition(一個bean的信息類)spring
(context)AbstractApplication.refresh():核心類型。初始化環境信息,加載配置路徑、實例化對象express
2.實例化對象:AbstractApplication.finishBeanFactoryInitialization()mybatis
注入:構造器注入、方法注入app
"模仿案例" Class<?> ob = Class.forName("com.fxl.spring.test.SayServiceImpl"); SayService say = (SayService) ob.newInstance(); Method[] methodList = ob.getMethods(); for (Method method : methodList) { System.out.println(method.getName()); if("setReflectNum".equals(method.getName())){ method.invoke(say, new Integer(2)); } } say.getMessage();
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:conf/Test.xml"); One one = (One) ctx.getBean("one"); System.out.println(one.getTwo().getName()); one.getDateTime();
通常,咱們開始學習spring的時候,都會從上面的使用模版開始。不管是誰都會告訴你第一行是生成一個beanFactroy的工廠,第二行就是從獲取須要的class對象。可是裏面發生了什麼,咱們並不瞭解。框架
public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory, MessageSource, ApplicationEventPublisher, ResourcePatternResolver
1.查看源碼,咱們發現是spring上下文的基本功能實現。查看說明eclipse
//ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:conf/Test.xml"); 1. ClassPathXmlApplicationContext:'記錄配置源' public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext { private Resource[] configResources; } 2. AbstractXmlApplicationContext:'Context封裝的xml實現' public abstract class AbstractXmlApplicationContext extends AbstractRefreshableConfigApplicationContext { private boolean validating = true; } 3.'AbstractRefreshableConfigApplicationContext':'記錄配置信息' public abstract class AbstractRefreshableConfigApplicationContext extends AbstractRefreshableApplicationContext implements BeanNameAware, InitializingBean { private String[] configLocations; } 4. 'AbstractRefreshableApplicationContext':'refresh上下文的適配類型,用來建立beanfactory' public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext { private Boolean allowBeanDefinitionOverriding; private Boolean allowCircularReferences; /** Bean factory for this context */ // 默認的beanfactory private DefaultListableBeanFactory beanFactory; /** Synchronization monitor for the internal BeanFactory */ private final Object beanFactoryMonitor = new Object(); } 5. 'AbstractApplicationContext':context的核心類、 // 實例化spring-core的對象 // refresh()刷新context public abstract class AbstractApplicationContext extends DefaultResourceLoader implements ConfigurableApplicationContext, DisposableBean { /** Logger used by this class. Available to subclasses. */ protected final Log logger = LogFactory.getLog(getClass()); /** Unique id for this context, if any */ private String id = ObjectUtils.identityToString(this); /** Display name */ private String displayName = ObjectUtils.identityToString(this); /** Parent context */ // 父類上下文 private ApplicationContext parent; /** BeanFactoryPostProcessors to apply on refresh */ private final List<BeanFactoryPostProcessor> beanFactoryPostProcessors = new ArrayList<BeanFactoryPostProcessor>(); /** System time in milliseconds when this context started */ private long startupDate; /** Flag that indicates whether this context is currently active */ private boolean active = false; /** Flag that indicates whether this context has been closed already */ private boolean closed = false; /** Synchronization monitor for the "active" flag */ // active鎖 private final Object activeMonitor = new Object(); /** Synchronization monitor for the "refresh" and "destroy" */ private final Object startupShutdownMonitor = new Object(); /** Reference to the JVM shutdown hook, if registered */ private Thread shutdownHook; /** ResourcePatternResolver used by this context */ // 資源加載匹配 private ResourcePatternResolver resourcePatternResolver; /** LifecycleProcessor for managing the lifecycle of beans within this context */ // 生命週期管理 private LifecycleProcessor lifecycleProcessor; /** MessageSource we delegate our implementation of this interface to */ //消息實現類 private MessageSource messageSource; /** Helper class used in event publishing */ //事件封裝類型 private ApplicationEventMulticaster applicationEventMulticaster; /** Statically specified listeners */ private Set<ApplicationListener<?>> applicationListeners = new LinkedHashSet<ApplicationListener<?>>(); /** Environment used by this context; initialized by {@link #createEnvironment()} */ // 環境配置:JVM環境 private ConfigurableEnvironment environment; } 6. DefaultResourceLoader: 類加載器 public class DefaultResourceLoader implements ResourceLoader { private ClassLoader classLoader; }
EnvironmentCapable:All Spring application contexts are EnvironmentCapable, and the interface is used primarily for performing instanceof checks in framework methods that accept BeanFactory instances that may or may not actually be ApplicationContext instances in order to interact with the environment if indeed it is available.ide
ListableBeanFactory:Extension of the BeanFactory interface to be implemented by bean factories that can enumerate all their bean instances, rather than attempting bean lookup by name one by one as requested by clients. BeanFactory implementations that preload all their bean definitions (such as XML-based factories) may implement this interface. (beanfactory的一個擴展接口)工具
HierarchicalBeanFactory:Sub-interface implemented by bean factories that can be part of a hierarchy.post
MessageSource:Strategy interface for resolving messages, with support for the parameterization and internationalization of such messages.學習
ApplicationEventPublisher:Interface that encapsulates event publication functionality .(事件封裝)
ResourcePatternResolver:Strategy interface for resolving a location pattern (for example, an Ant-style path pattern) into Resource objects. (資源處理)
ClassPathXmlApplicationContext
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException { super(parent); //加載applicationContext setConfigLocations(configLocations); //設置配置文件地址 if (refresh) { refresh(); } }
debug進classPathXmlApplication裏面,發現最重要的是上上面這段代碼。用eclipse的crtl+T,咱們能夠看見繼承關係.主要是獲取一下applicationContext,而後將xml文件的路徑從新設置。最後經過AbstractApplicationContext 將配置從新加載到內存中。
1.spring上下文的實現抽象類。用於application的合併,beanFactory的從新實現等等
public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { // Prepare this context for refreshing. (刷新前) // 獲取JVM環境Environment,檢測啓動傳入的參數 'prepareRefresh()''; // Tell the subclass to refresh the internal bean factory. // 建立一個 beanfactory,將配置信息加載進去 'ConfigurableListableBeanFactory' 'beanFactory' = 'obtainFreshBeanFactory()'; // Prepare the bean factory for use in this context.(準備刷新的beanFactory) // 配置工廠的標準上下文特徵,例如上下文的類加載器和後處理器。 prepareBeanFactory(beanFactory); try { // Allows post-processing of the bean factory in context subclasses.(容許子類中正在執行中的工廠) // 實例化並調用全部已註冊的BeanPostProcessor bean(事件bean),按照給定順序。處理一些示例化過程當中的業務邏輯,就像servlet的filter postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in the context.(執行工廠做爲一個bean註冊到context中) invokeBeanFactoryPostProcessors(beanFactory); // Register bean processors that intercept bean creation.(註冊bean執行者攔截建立) registerBeanPostProcessors(beanFactory); // Initialize message source for this context.(初始化) initMessageSource(); // Initialize event multicaster for this context.(初始化事件) initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses.(初始化特別的的子類bean) onRefresh(); // Check for listener beans and register them.(檢測監聽的bean,建立它們) registerListeners(); // Instantiate all remaining (non-lazy-init) singletons.(實例化全部重名了的單例) // 完成這個上下文的bean工廠的初始化,初始化全部剩餘的單例bean。 '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.(摧毀bean) 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(); } } }
prepareRefresh:獲取JVM環境Environment,檢測啓動傳入的參數
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory():加載並解析配置信息
prepareBeanFactory(beanFactory):將JVM環境所有傳入所有實例化放在 beanfactory中
postProcessBeanFactory(beanFactory):在它的標準初始化以後修改應用程序上下文的內部bean工廠。全部的bean定義都已經加載了,可是尚未實例化bean。這容許在特定的應用程序環境實現中註冊特殊的beanpost處理器等。(空方法)
invokeBeanFactoryPostProcessors(beanFactory):執行BeanPostProcessor,用於處理一些bean的示例
registerBeanPostProcessors(beanFactory):將BeanPostProcessor放入beanfactory中
initMessageSource():初始化message信息
initApplicationEventMulticaster():初始化applicationEventMulticaster,用來處理event事件。由哪一個listener來處理
onRefresh()
registerListeners():消息事件監聽器
finishBeanFactoryInitialization(beanFactory):將剩餘的bean實例化,存放在beanfactory的singletonObjects中。通常來講剩餘的都是配置文件中的信息
finishRefresh()
destroyBeans()
cancelRefresh(ex)
resetCommonCaches()