View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.xbean.spring.context;
18  
19  import java.io.IOException;
20  import java.lang.reflect.Constructor;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.apache.xbean.spring.context.impl.XBeanHelper;
25  import org.springframework.beans.BeansException;
26  import org.springframework.beans.factory.support.BeanDefinitionRegistry;
27  import org.springframework.beans.factory.support.DefaultListableBeanFactory;
28  import org.springframework.beans.factory.xml.ResourceEntityResolver;
29  import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
30  import org.springframework.context.ApplicationContext;
31  import org.springframework.core.SpringVersion;
32  
33  /**
34   * An XBean version of the regular Spring class to provide improved XML handling.
35   * 
36   * @author James Strachan
37   * @author Dain Sundstrom
38   * @version $Id$
39   * @since 2.0
40   */
41  public class ClassPathXmlApplicationContext extends org.springframework.context.support.ClassPathXmlApplicationContext implements SpringApplicationContext {
42      private final List xmlPreprocessors;
43  
44      /**
45       * Creates a ClassPathXmlApplicationContext which loads the configuration at the specified location on the class
46       * path.
47       * @param configLocation the location of the configuration file on the class path
48       * @throws BeansException if a problem occurs while reading the configuration
49       */
50      public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
51          this(new String[] {configLocation}, true, null, Collections.EMPTY_LIST);
52      }
53  
54      /**
55       * Creates a ClassPathXmlApplicationContext which loads the configuration at the specified locations on the class
56       * path.
57       * @param configLocations the locations of the configuration files on the class path
58       * @throws BeansException if a problem occurs while reading the configuration
59       */
60      public ClassPathXmlApplicationContext(String[] configLocations) throws BeansException {
61          this(configLocations, true, null, Collections.EMPTY_LIST);
62      }
63  
64      /**
65       * Creates a ClassPathXmlApplicationContext which loads the configuration at the specified locations on the class
66       * path.
67       * @param configLocations the locations of the configuration files on the class path
68       * @param refresh if true the configurations are immedately loaded; otherwise the configurations are not loaded
69       * until refresh() is called
70       * @throws BeansException if a problem occurs while reading the configuration
71       */
72      public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh) throws BeansException {
73          this(configLocations, refresh, null, Collections.EMPTY_LIST);
74      }
75  
76      /**
77       * Creates a ClassPathXmlApplicationContext which loads the configuration at the specified locations on the class
78       * path.
79       * @param configLocations the locations of the configuration files on the class path
80       * @param parent the parent of this application context
81       * @throws BeansException if a problem occurs while reading the configuration
82       */
83      public ClassPathXmlApplicationContext(String[] configLocations, ApplicationContext parent) throws BeansException {
84          this(configLocations, true, parent, Collections.EMPTY_LIST);
85      }
86  
87      /**
88       * Creates a ClassPathXmlApplicationContext which loads the configuration at the specified locations on the class
89       * path.
90       * @param configLocations the locations of the configuration files on the class path
91       * @param refresh if true the configurations are immedately loaded; otherwise the configurations are not loaded
92       * until refresh() is called
93       * @param parent the parent of this application context
94       * @throws BeansException if a problem occurs while reading the configuration
95       */
96      public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException {
97          this(configLocations, refresh, parent, Collections.EMPTY_LIST);
98      }
99  
100     /**
101      * Creates a ClassPathXmlApplicationContext which loads the configuration at the specified location on the class
102      * path.
103      * @param configLocation the location of the configuration file on the classpath
104      * @param xmlPreprocessors the SpringXmlPreprocessors to apply before passing the xml to Spring for processing
105      * @throws BeansException if a problem occurs while reading the configuration
106      */
107     public ClassPathXmlApplicationContext(String configLocation, List xmlPreprocessors) throws BeansException {
108         this(new String[] {configLocation}, true, null, xmlPreprocessors);
109     }
110 
111     /**
112      * Creates a ClassPathXmlApplicationContext which loads the configuration at the specified locations on the class
113      * path.
114      * @param configLocations the locations of the configuration files on the class path
115      * @param xmlPreprocessors the SpringXmlPreprocessors to apply before passing the xml to Spring for processing
116      * @throws BeansException if a problem occurs while reading the configuration
117      */
118     public ClassPathXmlApplicationContext(String[] configLocations, List xmlPreprocessors) throws BeansException {
119         this(configLocations, true, null, xmlPreprocessors);
120     }
121 
122     /**
123      * Creates a ClassPathXmlApplicationContext which loads the configuration at the specified locations on the class
124      * path.
125      * @param configLocations the locations of the configuration files on the class path
126      * @param refresh if true the configurations are immedately loaded; otherwise the configurations are not loaded
127      * until refresh() is called
128      * @param xmlPreprocessors the SpringXmlPreprocessors to apply before passing the xml to Spring for processing
129      * @throws BeansException if a problem occurs while reading the configuration
130      */
131     public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, List xmlPreprocessors) throws BeansException {
132         this(configLocations, refresh, null, xmlPreprocessors);
133     }
134 
135     /**
136      * Creates a ClassPathXmlApplicationContext which loads the configuration at the specified locations on the class
137      * path.
138      * @param configLocations the locations of the configuration files on the class path
139      * @param parent the parent of this application context
140      * @param xmlPreprocessors the SpringXmlPreprocessors to apply before passing the xml to Spring for processing
141      * @throws BeansException if a problem occurs while reading the configuration
142      */
143     public ClassPathXmlApplicationContext(String[] configLocations, ApplicationContext parent, List xmlPreprocessors) throws BeansException {
144         this(configLocations, true, parent, xmlPreprocessors);
145     }
146 
147     /**
148      * Creates a ClassPathXmlApplicationContext which loads the configuration at the specified locations on the class
149      * path.
150      * @param configLocations the locations of the configuration files on the class path
151      * @param refresh if true the configurations are immedately loaded; otherwise the configurations are not loaded
152      * until refresh() is called
153      * @param parent the parent of this application context
154      * @param xmlPreprocessors the SpringXmlPreprocessors to apply before passing the xml to Spring for processing
155      * @throws BeansException if a problem occurs while reading the configuration
156      */
157     public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent, List xmlPreprocessors) throws BeansException {
158         super(configLocations, false, parent);
159         this.xmlPreprocessors = xmlPreprocessors;
160         if (refresh) {
161             refresh();
162         }
163     }
164 
165     /**
166      * {@inheritDoc}
167      */
168     protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException {
169         // Create a new XmlBeanDefinitionReader for the given BeanFactory.
170         XmlBeanDefinitionReader beanDefinitionReader = XBeanHelper.createBeanDefinitionReader(this, beanFactory, xmlPreprocessors);
171 
172         // Configure the bean definition reader with this context's
173         // resource loading environment.
174         beanDefinitionReader.setResourceLoader(this);
175         beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
176 
177         // Allow a subclass to provide custom initialization of the reader,
178         // then proceed with actually loading the bean definitions.
179         initBeanDefinitionReader(beanDefinitionReader);
180         loadBeanDefinitions(beanDefinitionReader);
181     }
182     
183 }