欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

spring启动过程之源码跟踪(续beanfactory)--spring Debug

发布时间:2025/4/5 编程问答 29 豆豆
生活随笔 收集整理的这篇文章主要介绍了 spring启动过程之源码跟踪(续beanfactory)--spring Debug 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1.初始化过程

1 Resource res = new ClassPathResource("/applicationContext.xml"); 2 XmlBeanFactory factory = new XmlBeanFactory(res);

2.入门

1 /** 2 * Create a new XmlBeanFactory with the given input stream, 3 * which must be parsable using DOM. 4 * @param resource XML resource to load bean definitions from 5 * @param parentBeanFactory parent bean factory 6 * @throws BeansException in case of loading or parsing errors 7 */ 8 public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException { 9 super(parentBeanFactory); 10 this.reader.loadBeanDefinitions(resource); 11 }

3.读取配置文件XmlBeanDefinitionReader.java

1 public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException { 2 Assert.notNull(encodedResource, "EncodedResource must not be null"); 3 if (logger.isInfoEnabled()) { 4 logger.info("Loading XML bean definitions from " + encodedResource.getResource()); 5 } 6 7 Set currentResources = (Set) this.resourcesCurrentlyBeingLoaded.get(); 8 if (currentResources == null) { 9 currentResources = new HashSet(4); 10 this.resourcesCurrentlyBeingLoaded.set(currentResources); 11 } 12 if (!currentResources.add(encodedResource)) { 13 throw new BeanDefinitionStoreException( 14 "Detected recursive loading of " + encodedResource + " - check your import definitions!"); 15 } 16 try { 17 InputStream inputStream = encodedResource.getResource().getInputStream(); 18 try { 19 InputSource inputSource = new InputSource(inputStream); 20 if (encodedResource.getEncoding() != null) { 21 inputSource.setEncoding(encodedResource.getEncoding()); 22 } 23 return doLoadBeanDefinitions(inputSource, encodedResource.getResource()); 24 } 25 finally { 26 inputStream.close(); 27 } 28 } 29 catch (IOException ex) { 30 throw new BeanDefinitionStoreException( 31 "IOException parsing XML document from " + encodedResource.getResource(), ex); 32 } 33 finally { 34 currentResources.remove(encodedResource); 35 if (currentResources.isEmpty()) { 36 this.resourcesCurrentlyBeingLoaded.set(null); 37 } 38 } 39 }

4.读取方法DefaultBeanDefinitionDocumentReader.java

1 /** 2 * Parses bean definitions according to the "spring-beans" DTD. 3 * <p>Opens a DOM Document; then initializes the default settings 4 * specified at <code>&lt;beans&gt;</code> level; then parses 5 * the contained bean definitions. 6 */ 7 public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) { 8 this.readerContext = readerContext; 9 10 logger.debug("Loading bean definitions"); 11 Element root = doc.getDocumentElement(); 12 13 BeanDefinitionParserDelegate delegate = createHelper(readerContext, root); 14 15 preProcessXml(root); 16 parseBeanDefinitions(root, delegate); 17 postProcessXml(root); 18 }

 

 

 

转载于:https://www.cnblogs.com/davidwang456/archive/2013/03/12/2956187.html

总结

以上是生活随笔为你收集整理的spring启动过程之源码跟踪(续beanfactory)--spring Debug的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。