概述
- 步骤
- Resource定位
- Document载入
- BeanDefinition注册
XmlBeanFactory
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); // 自定义,默认空实现
parseBeanDefinitions(root, this.delegate)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:envDev/config-dev.properties"/>
<!--<context:annotation-config/>-->
<context:component-scan base-package="com.fjh"/>
<bean id="bean1" class="com.fjh.bean.Bean1" name="bean11" />
<alias name="bean11" alias="bean111" />
<import resource="classpath*:ds.xml"/>
</beans>
- beans
- context:property-placeholder
beans:root
data:"\n\n "
context:property-placeholder
data:"\n\n "
data:<context:annotation-config/>
data:"\n\n "
context:component-scan
...
parseDefaultElement - beans
- bean
- 得到BeanDefinitionHolder
- GenericBeanDefinition beanDefinition
- String beanName:id(不存在时用aliases首元素)
- String[] aliases:name
- 塞入DLBF.Map<beanName, BeanDefinition>
- 塞入DLBF.Map<alias, beanName>
- beans
- 递归调用doRegisterBeanDefinitions
- import
- alias
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<ScannedGenericBeanDefinition>
- 默认属性:lazyInit,initMethod,destroyMethod
- 注解属性:Lazy,Primary,DependsOn,Role,Description
- 塞到DLBF的map
- BeanPostProcessor的role=2
- 注册6个BeanPostProcessor
问答
classpath*:与classpath:区别
- resource1.jar的'com.test.rs'包有一个'jarAppContext.xml'文件
<bean id="processorImplA" class="com.test.spring.di.ProcessorImplA" />
- resource2.jar的 'com.test.rs'包也有一个'jarAppcontext.xml'文件
<bean id="processorImplB" class="com.test.spring.di.ProcessorImplB" />
// 可以把两个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