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     }

 

 

 

posted on 2013-03-12 17:58  一天不进步,就是退步  阅读(745)  评论(0编辑  收藏  举报