1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.beans.factory.xml;
  17. import java.io.InputStream;
  18. import org.springframework.beans.BeansException;
  19. import org.springframework.beans.factory.BeanFactory;
  20. import org.springframework.beans.factory.support.DefaultListableBeanFactory;
  21. import org.springframework.core.io.InputStreamResource;
  22. import org.springframework.core.io.Resource;
  23. /**
  24. * Convenience extension of DefaultListableBeanFactory that reads bean definitions from
  25. * an XML document. Delegates to DefaultXmlBeanDefinitionReader underneath; effectively
  26. * equivalent to using a DefaultXmlBeanDefinitionReader for a DefaultListableBeanFactory.
  27. *
  28. * <p>The structure, element and attribute names of the required XML document
  29. * are hard-coded in this class. (Of course a transform could be run if necessary
  30. * to produce this format). "beans" doesn't need to be the root element of the XML
  31. * document: This class will parse all bean definition elements in the XML file.
  32. *
  33. * <p>This class registers each bean definition with the DefaultListableBeanFactory
  34. * superclass, and relies on the latter's implementation of the BeanFactory
  35. * interface. It supports singletons, prototypes and references to either of
  36. * these kinds of bean.
  37. *
  38. * @author Rod Johnson
  39. * @author Juergen Hoeller
  40. * @since 15 April 2001
  41. * @version $Id: XmlBeanFactory.java,v 1.25 2004/03/18 02:46:12 trisberg Exp $
  42. */
  43. public class XmlBeanFactory extends DefaultListableBeanFactory {
  44. private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);
  45. /**
  46. * Create a new XmlBeanFactory with the given resource,
  47. * which must be parsable using DOM.
  48. * @param resource XML resource to load bean definitions from
  49. * @throws BeansException in case of loading or parsing errors
  50. */
  51. public XmlBeanFactory(Resource resource) throws BeansException {
  52. this(resource, null);
  53. }
  54. /**
  55. * Create a new XmlBeanFactory with the given InputStream,
  56. * which must be parsable using DOM.
  57. * <p>It's preferable to use a Resource argument instead of an
  58. * InputStream, to retain location information. This constructor
  59. * is mainly kept for backward compatibility.
  60. * @param is XML InputStream to load bean definitions from
  61. * @throws BeansException in case of loading or parsing errors
  62. * @see #XmlBeanFactory(Resource)
  63. */
  64. public XmlBeanFactory(InputStream is) throws BeansException {
  65. this(new InputStreamResource(is, "(no description)"), null);
  66. }
  67. /**
  68. * Create a new XmlBeanFactory with the given input stream,
  69. * which must be parsable using DOM.
  70. * @param resource XML resource to load bean definitions from
  71. * @param parentBeanFactory parent bean factory
  72. * @throws BeansException in case of loading or parsing errors
  73. */
  74. public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
  75. super(parentBeanFactory);
  76. this.reader.loadBeanDefinitions(resource);
  77. }
  78. }