Spring-3-IoC容器初始化

概述

  • 步骤
    • Resource定位
    • Document载入
    • BeanDefinition注册

XmlBeanFactory

  • Deprecated
ClassPathResource resource = new ClassPathResource("spring-test.xml");
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(resource);
System.out.println(factory.getBean("bean1"));

ClassPathXmlApplicationContext

入口

  • ClassPathXmlApplicationContext构造方法
    • super(parent)
      • CPXAC.resourcePatternResolver.resourceLoader = CPXAC
      • CPXAC.parentApplicationContext = parent
    • CPXAC.setConfigLocations(configLocations)
    • CPXAC.refresh()

Resource定位

ClassPathXmlApplicationContext context1 = new ClassPathXmlApplicationContext("classpath:spring-test.xml");
ClassPathResource("spring-test.xml", Launcher$AppClassLoader)

ClassPathXmlApplicationContext context2 = new ClassPathXmlApplicationContext("spring-test.xml");
ClassPathContextResource("spring-test.xml", Launcher$AppClassLoader)

FileSystemXmlApplicationContext context3 = new FileSystemXmlApplicationContext("spring-test.xml");
FileSystemResource("spring-test.xml") -> FileNotFoundException
需要"D:/spring-test.xml"
或者"classpath:spring-test.xml" -> ClassPathResource("spring-test.xml", Launcher$AppClassLoader)

ClassPathXmlApplicationContext context1 = new ClassPathXmlApplicationContext("classpath*:spring-test.xml");
UrlResource("file:/D:/work/code/jvtest/target/classes/spring-test.xml")
UrlResource("file:/D:/work/code/jvtest/target/test-classes/spring-test.xml")
多个

Document载入

InputStream inputStream = encodedResource.getResource().getInputStream()
InputSource inputSource = new InputSource(inputStream)
doLoadBeanDefinitions(inputSource, encodedResource.getResource());
// 载入 - 得到Document对象,用的org.w3c.dom
Document doc = doLoadDocument(inputSource, resource)

BeanDefinition注册

registerBeanDefinitions(doc, resource)

preProcessXml(root); // 自定义,默认空实现
parseBeanDefinitions(root, this.delegate)
postProcessXml(root); // 自定义,默认空实现
Spring-3-IoC容器初始化_第1张图片
spring-003.jpg

parseBeanDefinitions(root, this.delegate)

  • xml举例



    

    

    

    
    
    

    


  • beans
    • namespaceURI="http://www.springframework.org/schema/beans"
    • name="beans"
    • localName="beans"
  • context:property-placeholder
    • namespaceURI="http://www.springframework.org/schema/context"
    • name="context:property-placeholder"
    • localName="property-placeholder"
beans:root
data:"\n\n    "
context:property-placeholder
data:"\n\n    "
data:
data:"\n\n    "
context:component-scan
...

parseDefaultElement - beans

  • bean
    • 得到BeanDefinitionHolder
      • GenericBeanDefinition beanDefinition
        • beanClass
      • String beanName:id(不存在时用aliases首元素)
      • String[] aliases:name
    • 塞入DLBF.Map
    • 塞入DLBF.Map
  • beans
    • 递归调用doRegisterBeanDefinitions
  • import
    • 递归加载xml
  • alias
    • 塞入DLBF.Map

parseCustomElement

  • 拿到节点的名空间,比如context
    • String namespaceUri = getNamespaceURI(ele)
  • 找hander,怎么找呢
    • 加载所有META-INF/spring.handlers形成一个map
    • DefaultNamespaceHandlerResolver.resolve(namespaceUri)
  • META-INF/spring.handlers
http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler
http\://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandler
http\://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandler
http\://www.springframework.org/schema/task=org.springframework.scheduling.config.TaskNamespaceHandler
http\://www.springframework.org/schema/cache=org.springframework.cache.config.CacheNamespaceHandler
  • ContextNamespaceHandler.init()
  • 找到parser
registerBeanDefinitionParser("property-placeholder", new PropertyPlaceholderBeanDefinitionParser());
registerBeanDefinitionParser("property-override", new PropertyOverrideBeanDefinitionParser());
registerBeanDefinitionParser("annotation-config", new AnnotationConfigBeanDefinitionParser());
registerBeanDefinitionParser("component-scan", new ComponentScanBeanDefinitionParser());
registerBeanDefinitionParser("load-time-weaver", new LoadTimeWeaverBeanDefinitionParser());
registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser());
registerBeanDefinitionParser("mbean-export", new MBeanExportBeanDefinitionParser());
registerBeanDefinitionParser("mbean-server", new MBeanServerBeanDefinitionParser());

AnnotationConfigBeanDefinitionParser

  • 注册6个BeanPostProcessor,功能被component-scan包含

ComponentScanBeanDefinitionParser

  • doScan(basePackages)得到Set
  • 默认属性:lazyInit,initMethod,destroyMethod
  • 注解属性:Lazy,Primary,DependsOn,Role,Description
  • 塞到DLBF的map
  • BeanPostProcessor的role=2
  • 注册6个BeanPostProcessor

问答

classpath*:与classpath:区别

  • resource1.jar的'com.test.rs'包有一个'jarAppContext.xml'文件

  • resource2.jar的 'com.test.rs'包也有一个'jarAppcontext.xml'文件

// 可以把两个xml都加载进来
ApplicationContext context = new ClassPathXmlApplicationContext( "classpath*:com/test/rs/jarAppcontext.xml");
// 只会加载第一个xml
ApplicationContext context = new ClassPathXmlApplicationContext( "classpath:com/test/rs/jarAppcontext.xml");

base-package属性中的包在多个jar中出现

  • 根据类加载器规则,同名类只会加载第一次

对于Bean依赖链,有没有做优化?

  • 粗看下,并没有,不会特意优先加载没有需注入属性的bean

你可能感兴趣的:(Spring-3-IoC容器初始化)