在Spring的入口函数refresh()之中进行的。
- AbstractApplicationContext
- ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
- protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
- refreshBeanFactory();
- ConfigurableListableBeanFactory beanFactory = getBeanFactory();
- if (logger.isDebugEnabled()) {
- logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
- }
- return beanFactory;
- }
- AbstractRefreshableApplicationContext
- 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;
- }
- }
- }
- XmlWebApplicationContext
- protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
- XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
- beanDefinitionReader.setEnvironment(this.getEnvironment());
- beanDefinitionReader.setResourceLoader(this);
- beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
- initBeanDefinitionReader(beanDefinitionReader);
- loadBeanDefinitions(beanDefinitionReader);
- }
- protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {
- String[] configLocations = getConfigLocations();
- if (configLocations != null) {
- for (String configLocation : configLocations) {
- reader.loadBeanDefinitions(configLocation);
- }
- }
- }
- 中间省略
- XmlBeanDefinitionReader
- public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
- BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
- documentReader.setEnvironment(this.getEnvironment());
- int countBefore = getRegistry().getBeanDefinitionCount();
- documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
- return getRegistry().getBeanDefinitionCount() - countBefore;
- }
- protected XmlReaderContext createReaderContext(Resource resource) {
- if (this.namespaceHandlerResolver == null) {
- this.namespaceHandlerResolver = createDefaultNamespaceHandlerResolver();
- }
- return new XmlReaderContext(resource, this.problemReporter, this.eventListener,
- this.sourceExtractor, this, this.namespaceHandlerResolver);
- }
- protected NamespaceHandlerResolver createDefaultNamespaceHandlerResolver() {
- return new DefaultNamespaceHandlerResolver(getResourceLoader().getClassLoader());
- }
- public DefaultNamespaceHandlerResolver(ClassLoader classLoader) {
- this(classLoader, DEFAULT_HANDLER_MAPPINGS_LOCATION);
- }
- 接着往下走注册BeanDefinitioni的过程
- DefaultBeanDefinitionDocumentReader
- public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {
- this.readerContext = readerContext;
- logger.debug("Loading bean definitions");
- Element root = doc.getDocumentElement();
- doRegisterBeanDefinitions(root);
- }
- protected void doRegisterBeanDefinitions(Element root) {
- this.delegate = createDelegate(this.readerContext, root, parent);
- preProcessXml(root);
- parseBeanDefinitions(root, this.delegate);
- postProcessXml(root);
- this.delegate = parent;
- }
- 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);
- }
- }
- public BeanDefinition parseCustomElement(Element ele, BeanDefinition containingBd) {
- String namespaceUri = getNamespaceURI(ele);
- NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
- return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));
- }
- DefaultNamespaceHandlerResolver
- public NamespaceHandler resolve(String namespaceUri) {
- Map<String, Object> handlerMappings = getHandlerMappings();
- Object handlerOrClassName = handlerMappings.get(namespaceUri);
- if (handlerOrClassName == null) {
- return null;
- }
- else if (handlerOrClassName instanceof NamespaceHandler) {
- return (NamespaceHandler) handlerOrClassName;
- }
- else {
- String className = (String) handlerOrClassName;
- try {
- Class<?> handlerClass = ClassUtils.forName(className, this.classLoader);
- if (!NamespaceHandler.class.isAssignableFrom(handlerClass)) {
- throw new FatalBeanException("Class [" + className + "] for namespace [" + namespaceUri +
- "] does not implement the [" + NamespaceHandler.class.getName() + "] interface");
- }
- NamespaceHandler namespaceHandler = (NamespaceHandler) BeanUtils.instantiateClass(handlerClass);
- namespaceHandler.init();
- handlerMappings.put(namespaceUri, namespaceHandler);
- return namespaceHandler;
- }
- }
- }
转载于:https://www.cnblogs.com/wwjldm/p/10338418.html