1. 程式人生 > >看看Spring的原始碼(一)——Bean載入過程

看看Spring的原始碼(一)——Bean載入過程

最近幾天跟同事聊起Spring的一些問題,對一些地方有些疑問,趁這兩天有點空,看看Spring的原始碼,瞭解下具體的實現細節。本文基於Spring 4.0.5版本。

首先Web專案使用Spring是通過在web.xml裡面配置
org.springframework.web.context.ContextLoaderListener初始化IOC容器的。

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

那就以此為切入點順藤摸瓜。

public class ContextLoaderListener extends ContextLoader implements ServletContextListener 

ContextLoaderListener繼承了ContextLoader,並且實現ServletContextListener介面。當Server容器(一般指tomcat)啟動時,會收到事件初始化。

@Override
public void contextInitialized(ServletContextEvent event) {
    initWebApplicationContext(event.getServletContext());
}

initWebApplicationContext方法是在org.springframework.web.context.ContextLoader類裡面。方法太長,分段讀一下。

if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
    throw new IllegalStateException("Cannot initialize context because there is already a root application context present - " +"check whether you have multiple ContextLoader* definitions in your web.xml!");
}
Log logger = LogFactory.getLog(ContextLoader.class);
servletContext.log("Initializing Spring root WebApplicationContext");
if (logger.isInfoEnabled()) {
    logger.info("Root WebApplicationContext: initialization started");
}
long startTime = System.currentTimeMillis();

首先是判斷servletContext中是否已經註冊了WebApplicationContext,如果有則丟擲異常,避免重複註冊。然後就是啟用log,啟動計時。本方法的關鍵就在於try程式碼塊裡的內容

try {
    // Store context in local instance variable, to guarantee that
    // it is available on ServletContext shutdown.
    if (this.context == null) {
        this.context = createWebApplicationContext(servletContext);
    }
    if (this.context instanceof ConfigurableWebApplicationContext) {
        ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
        if (!cwac.isActive()) {
            // The context has not yet been refreshed -> provide services such as
            // setting the parent context, setting the application context id, etc
            if (cwac.getParent() == null) {
                // The context instance was injected without an explicit parent ->
                // determine parent for root web application context, if any.
                ApplicationContext parent = loadParentContext(servletContext);
                cwac.setParent(parent);
            }
            configureAndRefreshWebApplicationContext(cwac, servletContext);
        }
    }
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

    ClassLoader ccl = Thread.currentThread().getContextClassLoader();
    if (ccl == ContextLoader.class.getClassLoader()) {
        currentContext = this.context;
    }
    else if (ccl != null) {
        currentContextPerThread.put(ccl, this.context);
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
                WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
    }
    if (logger.isInfoEnabled()) {
        long elapsedTime = System.currentTimeMillis() - startTime;
        logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
    }

    return this.context;
}

這裡面有幾個關鍵的方法。首先看一下createWebApplicationContext()

protected WebApplicationContext createWebApplicationContext(ServletContext sc) {
    Class<?> contextClass = determineContextClass(sc);
    if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
        throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
                "] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
    }
    return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
}

首先determineContextClass()方法查明具體的Context類,他會讀取servletContext的初始化引數contextClass,此引數我們一半不配置,所以Spring就會讀取跟org.springframework.web.context.WebApplicationContext同一個包下面的ContextLoader.properties檔案讀取預設設定,反射出org.springframework.web.context.support.XmlWebApplicationContext類來。接下來就是在configureAndRefreshWebApplicationContext()方法裡將新建立的XmlWebApplicationContext進行初始化。首先會設定一個預設ID,即org.springframework.web.context.WebApplicationContext:+你專案的ContextPath

if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
    // The application context id is still set to its original default
    // value
    // -> assign a more useful id based on available information
    String idParam = sc.getInitParameter(CONTEXT_ID_PARAM);
    if (idParam != null) {
        wac.setId(idParam);
    } else {
        // Generate default id...
        wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(sc.getContextPath()));
    }
}

緊接著就是將ServletContext設定成XmlWebApplicationContext的屬性,這樣Spring就能在上下文裡輕鬆拿到ServletContext了。

wac.setServletContext(sc);

接下來就是讀取web.xml檔案中的contextConfigLocation引數。如果沒有配置就會去讀WEB-INF下的applicationContext.xml檔案。

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:beans.xml</param-value>
</context-param>

並將值設定(就是我們的Spring配置檔案的路徑)進XmlWebApplicationContext中。然後就會在指定的路徑載入配置檔案。

String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);
if (configLocationParam != null) {
    wac.setConfigLocation(configLocationParam);
}

接下來就是customizeContext(sc, wac)方法,此方法會根據使用者配置的globalInitializerClasses引數來初始化一些使用者自定義的屬性,一般我們不配置,所以這裡什麼也不做。

最後登場的就是最核心的方法了,

wac.refresh();

在這個方法裡,會完成資原始檔的載入、配置檔案解析、Bean定義的註冊、元件的初始化等核心工作,我們一探究竟。

@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;
        }
    }
}

次方法是同步的,避免重複重新整理,每個步驟都放在單獨的方法內,流程清晰,是值得學習的地方。這裡面有個重要的方法是finishBeanFactoryInitialization(beanFactory);,裡面的內容是Spring如何例項化bean,並注入依賴的,這個內容下一節講,本節只說明Spring是如何載入class檔案的。

首先就是prepareRefresh()方法。

protected void prepareRefresh() {
    this.startupDate = System.currentTimeMillis();

    synchronized (this.activeMonitor) {
        this.active = true;
    }

    if (logger.isInfoEnabled()) {
        logger.info("Refreshing " + this);
    }

    // Initialize any placeholder property sources in the context environment
    initPropertySources();

    // Validate that all properties marked as required are resolvable
    // see ConfigurablePropertyResolver#setRequiredProperties
    getEnvironment().validateRequiredProperties();
}

此方法做一些準備工作,如記錄開始時間,輸出日誌,initPropertySources();getEnvironment().validateRequiredProperties();一般沒幹什麼事。

接下來就是初始化BeanFactory,是整個refresh()方法的核心,其中完成了配置檔案的載入、解析、註冊

ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

看看它裡面都做了些什麼?

protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
    refreshBeanFactory();
    ConfigurableListableBeanFactory beanFactory = getBeanFactory();
    if (logger.isDebugEnabled()) {
        logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
    }
    return beanFactory;
}

首先refreshBeanFactory()

protected final void refreshBeanFactory() throws BeansException {
    if (hasBeanFactory()) {
        destroyBeans();
        closeBeanFactory();
    }
    try {
        DefaultListableBeanFactory beanFactory = createBeanFactory();
        beanFactory.setSerializationId(getId());
        customizeBeanFactory(beanFactory);
        loadBeanDefinitions(beanFactory);
        synchronized (this.beanFactoryMonitor) {
            this.beanFactory = beanFactory;
        }
    }
    catch (IOException ex) {
        throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
    }
}

我們看到會建立一個DefaultListableBeanFactory例項

DefaultListableBeanFactory beanFactory = createBeanFactory();

再設定一個ID

beanFactory.setSerializationId(getId());

然後設定一些自定義引數:

customizeBeanFactory(beanFactory);

這裡面最重要的就是loadBeanDefinitions(beanFactory);方法了。

@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
    // Create a new XmlBeanDefinitionReader for the given BeanFactory.
    XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

    // Configure the bean definition reader with this context's
    // resource loading environment.
    beanDefinitionReader.setEnvironment(this.getEnvironment());
    beanDefinitionReader.setResourceLoader(this);
    beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

    // Allow a subclass to provide custom initialization of the reader,
    // then proceed with actually loading the bean definitions.
    initBeanDefinitionReader(beanDefinitionReader);
    loadBeanDefinitions(beanDefinitionReader);
}

此方法會通過XmlBeanDefinitionReader載入bean定義。具體的實現方法是在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions方法中定義的。這裡設計了層層呼叫,有好多過載方法,主要就是載入Spring所有的配置檔案(可能會有多個),以備後面解析,註冊之用。我一路追蹤到org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(Element root)

protected void doRegisterBeanDefinitions(Element root) {
    String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);
    if (StringUtils.hasText(profileSpec)) {
        Assert.state(this.environment != null, "Environment must be set for evaluating profiles");
        String[] specifiedProfiles = StringUtils.tokenizeToStringArray(
                profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS);
        if (!this.environment.acceptsProfiles(specifiedProfiles)) {
            return;
        }
    }
    BeanDefinitionParserDelegate parent = this.delegate;
    this.delegate = createDelegate(this.readerContext, root, parent);
    preProcessXml(root);
    parseBeanDefinitions(root, this.delegate);
    postProcessXml(root);
    this.delegate = parent;
}

這裡建立了一個BeanDefinitionParserDelegate示例,解析XML的過程就是委託它完成的,我們不關心它是怎樣解析XML的,我們只關心是怎麼載入類的,所以就要看parseBeanDefinitions(root, this.delegate)方法了。

protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
    if (delegate.isDefaultNamespace(root)) {
        NodeList nl = root.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            if (node instanceof Element) {
                Element ele = (Element) node;
                if (delegate.isDefaultNamespace(ele)) {
                    parseDefaultElement(ele, delegate);
                }
                else {
                    delegate.parseCustomElement(ele);
                }
            }
        }
    }
    else {
        delegate.parseCustomElement(root);
    }
}

我們看到最終解析XML元素的是delegate.parseCustomElement(ele)方法,最終會走到一下方法.

public BeanDefinition parseCustomElement(Element ele, BeanDefinition containingBd) {
    String namespaceUri = getNamespaceURI(ele);
    NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
    if (handler == null) {
        error("Unable to locate Spring NamespaceHandler for XML schema namespace [" + namespaceUri + "]", ele);
        return null;
    }
    return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));
}

這裡會根據不同的XML節點,會委託NamespaceHandlerSupport找出合適的BeanDefinitionParser,如果我們配置了

<context:component-scan
    base-package="com.geeekr.service,com.geeekr.dao" />

那麼對應BeanDefinitionParser就是org.springframework.context.annotation.ComponentScanBeanDefinitionParser,來看看它的parse方法。

@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
    String[] basePackages = StringUtils.tokenizeToStringArray(element.getAttribute(BASE_PACKAGE_ATTRIBUTE),
            ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);

    // Actually scan for bean definitions and register them.
    ClassPathBeanDefinitionScanner scanner = configureScanner(parserContext, element);
    Set<BeanDefinitionHolder> beanDefinitions = scanner.doScan(basePackages);
    registerComponents(parserContext.getReaderContext(), beanDefinitions, element);

    return null;
}

不難看出這裡定義了一個ClassPathBeanDefinitionScanner,通過它去掃描包中的類檔案,注意:這裡是類檔案而不是類,因為現在這些類還沒有被載入,只是ClassLoader能找到這些class的路徑而已。到目前為止,感覺真想距離我們越來越近了。順著繼續往下摸。進入doSacn方法裡,映入眼簾的又是一大坨程式碼,但是我們只關心觀點的部分。

protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
    Assert.notEmpty(basePackages, "At least one base package must be specified");
    Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<BeanDefinitionHolder>();
    for (String basePackage : basePackages) {
        Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
        for (BeanDefinition candidate : candidates) {
            ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
            candidate.setScope(scopeMetadata.getScopeName());
            String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);
            if (candidate instanceof AbstractBeanDefinition) {
                postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
            }
            if (candidate instanceof AnnotatedBeanDefinition) {
                AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
            }
            if (checkCandidate(beanName, candidate)) {
                BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
                definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
                beanDefinitions.add(definitionHolder);
                registerBeanDefinition(definitionHolder, this.registry);
            }
        }
    }
    return beanDefinitions;
}

一眼就能看出是通過

Set<BeanDefinition> candidates = findCandidateComponents(basePackage);

有時候不得不佩服這些外國人起名字的功力,把掃描出來的類叫做candidates(候選人);真是不服不行啊,這種名字真的很容易理解有不有?哈哈,貌似扯遠了。繼續往下看。這裡只列出方法的主題部分。

public Set<BeanDefinition> findCandidateComponents(String basePackage) {
    Set<BeanDefinition> candidates = new LinkedHashSet<BeanDefinition>();
    try {
        String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
                resolveBasePackage(basePackage) + "/" + this.resourcePattern;
        Resource[] resources = this.resourcePatternResolver.getResources(packageSearchPath);
        boolean traceEnabled = logger.isTraceEnabled();
        boolean debugEnabled = logger.isDebugEnabled();
        for (Resource resource : resources) {
            if (traceEnabled) {
                logger.trace("Scanning " + resource);
            }
            if (resource.isReadable()) {
                try {
                    MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(resource);
                    if (isCandidateComponent(metadataReader)) {
                        ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);
                        sbd.setResource(resource);
                        sbd.setSource(resource);

先看這兩句:

String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + resolveBasePackage(basePackage) + "/" + this.resourcePattern;

假設我們配置的需要掃描的包名為com.geeekr.service,那麼packageSearchPath的值就是classpath*:com.geeekr.service/**/*.class,意思就是com.geeekr.service包(包括子包)下所有class檔案;如果配置的是*,那麼packageSearchPath的值就是classpath*:*/**/*.class。這裡的表示式是Spring自己定義的。Spring會根據這種表示式找出相關的class檔案。

Resource[] resources = this.resourcePatternResolver.getResources(packageSearchPath);

這一句就把相關class檔案加載出來了,那我們就要看看,Spring究竟是如何把class檔案找到的了。首先看看resourcePatternResolver的定義:

private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();

進入getResources方法

@Override
public Resource[] getResources(String locationPattern) throws IOException {
    Assert.notNull(locationPattern, "Location pattern must not be null");
    if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) {
        // a class path resource (multiple resources for same name possible)
        if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) {
            // a class path resource pattern
            return findPathMatchingResources(locationPattern);
        }
        else {
            // all class path resources with the given name
            return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));
        }
    }
    else {
        // Only look for a pattern after a prefix here
        // (to not get fooled by a pattern symbol in a strange prefix).
        int prefixEnd = locationPattern.indexOf(":") + 1;
        if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) {
            // a file pattern
            return findPathMatchingResources(locationPattern);
        }
        else {
            // a single resource with the given name
            return new Resource[] {getResourceLoader().getResource(locationPattern)};
        }
    }
}

這裡會先判斷表示式是否以classpath*:開頭。前面我們看到Spring已經給我們添加了這個頭,這裡當然符合條件了。接著會進入findPathMatchingResources方法。在這裡又把**/*.class去掉了,然後在呼叫getResources方法,然後在進入findAllClassPathResources方法。這裡的引數只剩下包名了例如com/geeekr/service/

protected Resource[] findAllClassPathResources(String location) throws IOException {
    String path = location;
    if (path.startsWith("/")) {
        path = path.substring(1);
    }
    ClassLoader cl = getClassLoader();
    Enumeration<URL> resourceUrls = (cl != null ? cl.getResources(path) : ClassLoader.getSystemResources(path));
    Set<Resource> result = new LinkedHashSet<Resource>(16);
    while (resourceUrls.hasMoreElements()) {
        URL url = resourceUrls.nextElement();
        result.add(convertClassLoaderURL(url));
    }
    return result.toArray(new Resource[result.size()]);
}

真相大白了,Spring也是用的ClassLoader載入的class檔案。一路追蹤,原始的ClassLoader是Thread.currentThread().getContextClassLoader();。到此為止,就拿到class檔案了。
Spring會將class資訊封裝成BeanDefinition,然後再放進DefaultListableBeanFactorybeanDefinitionMap中。

拿到了class檔案後,就要看看Spring是如何裝配bean的了,下一節,繼續看。

相關推薦

看看Spring原始碼()——Bean載入過程

最近幾天跟同事聊起Spring的一些問題,對一些地方有些疑問,趁這兩天有點空,看看Spring的原始碼,瞭解下具體的實現細節。本文基於Spring 4.0.5版本。 首先Web專案使用Spring是通過在web.xml裡面配置org.springframework.web.context.ContextLo

看看Spring原始碼(二)——bean例項化

首先來看一段程式碼,看過上一節的朋友肯定對這段程式碼並不陌生。這一段程式碼詮釋了Spring載入bean的完整過程,包括讀取配置檔案,掃描包,載入類,例項化bean,注入bean屬性依賴。 public void refresh() throws BeansExcepti

Spring原始碼閱讀——bean載入過程解析

前言 在上一節中,我們已經瞭解過Spring對bean從XML中提取,並且以BeanDefinition型別解析註冊到Spring的DefaultListableBeanFactory中取了,那麼,接下來就應該來看下我們在Spring中獲取一個例項過程中,bean是如何載入成為我們需

Spring原始碼中容器載入Bean過程

使用XmlBeanFactory容器來載入容器中Bean的過程 在Spring原始碼中XmlBeanFactory.java 可以有上面的XmlBeanFactory,java載入資原始檔可以看出,初始化XmlBeanFactory類的關鍵是this.reader.loadBeanDefi

Spring原始碼閱讀——Bean載入和獲取過程

我們經常使用Spring,並且也都瞭解其大概原理。我想我們一定會對Spring原始碼的解讀有迫切的渴望。 我也如此。所以,我打算閱讀一下Spring的原始碼。再此之前,我也為此準備了很多。包括,去複習熟練java反射,理解常用的設計模式。當然,這些複習筆記也會在今後的複習中

SpringBean載入過程

在載入Bean時需要經歷11個步驟 instantiate bean 物件例項化 屬性注入 如果Bean 實現了BeanNameAware 執行setBeanName 方法 如果Bean實現了BeanFactoryAware 或者ApplicationConte

spring bean 載入過程spring

  以classpathXmlApplication為例 入口方法包含3個部分, public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext par

Spring原始碼Bean初始化過程

主程式碼 bean完成屬性注入之後,接著要以bean進行初始化,初始化過程在AbstractAutowireCapableBeanFactory抽象類中,核心程式碼如下: protected Object initializeBean(fina

spring bean載入過程

需要的方法引數如下: 1.name 你要得到bean物件的名稱 不能為空 2.requiredType 這個bean物件的Class型別,可以為null 3.args 可以為null,如果有引數,則代表在找到這個bean定義後,通過構造方法或工廠方法或其他方法傳入args引數來改變這個bean例項。 spri

spring容器及bean載入機制原始碼解讀

前言:這是本人第一個部落格,早就想記錄些總結和理解,可一直不知道從哪開始,最近正好在解決一個spring的問題,正好這個問題涉及到了spring的一些相關基礎,整理一下就從這部分開始了。 歡迎所有閱讀者和愛好者批評從各個方面(特別是文件和技術方面)批評,指正。

死磕spring,大致還原springbean載入過程

總的來說: 對傳入的name做轉化,去除&開頭獲得真實beanname,通過aliasMap獲取是否有別名 檢查是否有bean的完整的引用快取;如果沒有,看singletonsCurrentlyInCreation,是不是這個bean正在建立中,看ea

Spring原始碼系列 — Bean生命週期

前言 上篇文章中介紹了Spring容器的擴充套件點,這個是在Bean的建立過程之前執行的邏輯。承接擴充套件點之後,就是Spring容器的另一個核心:Bean的生命週期過程。這個生命週期過程大致經歷了一下的幾個階段 在本節中重點介紹例項化、填充裝配、喚醒Aware方法、BeanPostProce

Spring FrameWork的bean載入機制

其實原始碼是個好東西,好到讓你理解作者的初心和想法是如何一步步實現的,例如spring,我們更習慣於拿來當應用去使用,但適當的去研究下原始碼的實現就會掌握很多幹貨。 千萬言不抵一書,梳理下spring的

Spring 學習 bean 注入方式

Bean 的注入方式 介紹向 IOC 容器注入 bean 的方式 XML 配置的方式 public class Person { private String name; private Integer age; } xml 檔案的配置

Spring知識(bean配置)

IOC與DI概念 IOC(Inversion of Control):其思想是反轉資源獲取的方向. 傳統的資源查詢方式要求元件向容器發起請求查 找資源. 作為迴應, 容器適時的返回資源. 而應用了 IOC 之後, 則是容器主動地將資源推送給它所管理的元件,

Spring原始碼bean建立(四)屬性注入

一、populateBean 在建立了bean的例項後,Spring的下一步工作就是為bean注入其依賴的屬性: protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable

使用Spring @DependsOn控制bean載入順序

  spring容器載入bean順序是不確定的,spring框架沒有約定特定順序邏輯規範。但spring保證如果A依賴B(如beanA中有@Autowired B的變數),那麼B將先於A被載入。但如果beanA不直接依賴B,我們如何讓B仍先載入呢? 控制bean初始化順序 可能有些

深入理解Spring原始碼bean的生命週期控制器BeanPostProcessor

 spring是藉助ioc容器進行bean的初始化的,ioc的概念如下: bean的生命週期:            bean建立---初始化----銷燬的過程   容器管理bean的生命週期;  我們可以自定義初始化和銷燬方法;容器在bean進行到當前生命週期

spring mvc 次請求過程跟蹤()

spring mvc 一次請求過程跟蹤 專案背景 Spring Boot 主方法 import org.springframework.boot.SpringApplication; import org.springframework.

spring 原始碼研究---bean包--BeanWapper TypeCoverter PropertyEditorRegistrySupport BeanInfo PropertyValue

#####################   PropertyValue  ############################ AttributeAccessor定義了訪問屬性訪問器 AttributeAccessorSupport使用hash表來管理屬性訪問器 BeanMetadataElement