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.context.support;
  17. import java.io.IOException;
  18. import org.springframework.beans.BeansException;
  19. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
  20. import org.springframework.beans.factory.support.DefaultListableBeanFactory;
  21. import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
  22. import org.springframework.context.ApplicationContext;
  23. import org.springframework.context.ApplicationContextException;
  24. import org.springframework.core.io.Resource;
  25. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  26. import org.springframework.core.io.support.ResourcePatternResolver;
  27. /**
  28. * Convenient abstract superclass for ApplicationContext implementations
  29. * drawing their configuration from XML documents containing bean definitions
  30. * understood by an XmlBeanDefinitionParser.
  31. * @author Rod Johnson
  32. * @author Juergen Hoeller
  33. * @version $Revision: 1.14 $
  34. * @see org.springframework.beans.factory.xml.XmlBeanDefinitionParser
  35. */
  36. public abstract class AbstractXmlApplicationContext extends AbstractApplicationContext {
  37. /** Bean factory for this context */
  38. private ConfigurableListableBeanFactory beanFactory;
  39. /**
  40. * Create a new AbstractXmlApplicationContext with no parent.
  41. */
  42. public AbstractXmlApplicationContext() {
  43. }
  44. /**
  45. * Create a new AbstractXmlApplicationContext with the given parent context.
  46. * @param parent the parent context
  47. */
  48. public AbstractXmlApplicationContext(ApplicationContext parent) {
  49. super(parent);
  50. }
  51. protected final void refreshBeanFactory() throws BeansException {
  52. try {
  53. DefaultListableBeanFactory beanFactory = createBeanFactory();
  54. XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
  55. beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
  56. initBeanDefinitionReader(beanDefinitionReader);
  57. loadBeanDefinitions(beanDefinitionReader);
  58. this.beanFactory = beanFactory;
  59. if (logger.isInfoEnabled()) {
  60. logger.info("Bean factory for application context [" + getDisplayName() + "]: " + beanFactory);
  61. }
  62. }
  63. catch (IOException ex) {
  64. throw new ApplicationContextException("I/O error parsing XML document for application context [" +
  65. getDisplayName() + "]", ex);
  66. }
  67. }
  68. public final ConfigurableListableBeanFactory getBeanFactory() {
  69. return beanFactory;
  70. }
  71. /**
  72. * Create the bean factory for this context.
  73. * <p>Default implementation creates a DefaultListableBeanFactory with the
  74. * internal bean factory of this context's parent as parent bean factory.
  75. * <p>Can be overridden in subclasses.
  76. * @return the bean factory for this context
  77. * @see org.springframework.beans.factory.support.DefaultListableBeanFactory
  78. * @see #getInternalParentBeanFactory
  79. */
  80. protected DefaultListableBeanFactory createBeanFactory() {
  81. return new DefaultListableBeanFactory(getInternalParentBeanFactory());
  82. }
  83. /**
  84. * Initialize the bean definition reader used for loading the bean
  85. * definitions of this context. Default implementation is empty.
  86. * <p>Can be overridden in subclasses, e.g. for turning off XML validation
  87. * or using a different XmlBeanDefinitionParser implementation.
  88. * @param beanDefinitionReader the bean definition reader used by this context
  89. * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader#setValidating
  90. * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader#setParserClass
  91. */
  92. protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {
  93. }
  94. /**
  95. * Load the bean definitions with the given XmlBeanDefinitionReader.
  96. * <p>The lifecycle of the bean factory is handled by the refreshBeanFactory method;
  97. * therefore this method is just supposed to load and/or register bean definitions.
  98. * <p>Delegates to a ResourcePatternResolver for resolving location patterns
  99. * into Resource instances.
  100. * @throws BeansException in case of bean registration errors
  101. * @throws IOException if the required XML document isn't found
  102. * @see #refreshBeanFactory
  103. * @see #getConfigLocations
  104. * @see #getResourcePatternResolver
  105. */
  106. protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
  107. String[] configLocations = getConfigLocations();
  108. if (configLocations != null) {
  109. ResourcePatternResolver resourcePatternResolver = getResourcePatternResolver();
  110. for (int i = 0; i < configLocations.length; i++) {
  111. Resource[] configResources = resourcePatternResolver.getResources(configLocations[i]);
  112. for (int j = 0; j < configResources.length; j++) {
  113. reader.loadBeanDefinitions(configResources[j]);
  114. }
  115. }
  116. }
  117. }
  118. /**
  119. * Return the ResourcePatternResolver to use for resolving location patterns
  120. * into Resource instances. Default is PathMatchingResourcePatternResolver,
  121. * supporting Ant-style location patterns.
  122. * @see org.springframework.core.io.support.PathMatchingResourcePatternResolver
  123. */
  124. protected ResourcePatternResolver getResourcePatternResolver() {
  125. return new PathMatchingResourcePatternResolver(this);
  126. }
  127. /**
  128. * Return an array of resource locations, referring to the XML bean
  129. * definition files that this context should be built with.
  130. * @return an array of resource locations, or null if none
  131. */
  132. protected abstract String[] getConfigLocations();
  133. }