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 org.springframework.beans.BeansException;
  18. import org.springframework.context.ApplicationContext;
  19. /**
  20. * Standalone XML application context, taking the context definition
  21. * files from the class path. Mainly useful for test harnesses,
  22. * but also for application contexts embedded within JARs.
  23. *
  24. * <p>Treats resource paths as class path resources, when using
  25. * ApplicationContext.getResource. Only supports full classpath resource
  26. * names that include the package path, like "mypackage/myresource.dat".
  27. *
  28. * <p>The config location defaults can be overridden via setConfigLocations,
  29. * respectively via the "contextConfigLocation" parameters of ContextLoader and
  30. * FrameworkServlet. Config locations can either denote concrete files like
  31. * "/mypackage/context.xml" or Ant-style patterns like "/mypackage/*-context.xml"
  32. * (see PathMatcher javadoc for pattern details).
  33. *
  34. * <p>Note: In case of multiple config locations, later bean definitions will
  35. * override ones defined in earlier loaded files. This can be leveraged to
  36. * deliberately override certain bean definitions via an extra XML file.
  37. *
  38. * @author Rod Johnson
  39. * @author Juergen Hoeller
  40. * @see #getResource
  41. * @see #getResourceByPath
  42. * @version $Id: ClassPathXmlApplicationContext.java,v 1.12 2004/04/05 07:18:42 jhoeller Exp $
  43. */
  44. public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext {
  45. private String[] configLocations;
  46. /**
  47. * Create a new ClassPathXmlApplicationContext, loading the definitions
  48. * from the given XML file.
  49. * @param configLocation file path
  50. */
  51. public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
  52. this.configLocations = new String[] {configLocation};
  53. refresh();
  54. }
  55. /**
  56. * Create a new ClassPathXmlApplicationContext, loading the definitions
  57. * from the given XML files.
  58. * @param configLocations array of file paths
  59. */
  60. public ClassPathXmlApplicationContext(String[] configLocations) throws BeansException {
  61. this.configLocations = configLocations;
  62. refresh();
  63. }
  64. /**
  65. * Create a new ClassPathXmlApplicationContext with the given parent,
  66. * loading the definitions from the given XML files.
  67. * @param configLocations array of file paths
  68. * @param parent the parent context
  69. */
  70. public ClassPathXmlApplicationContext(String[] configLocations, ApplicationContext parent)
  71. throws BeansException {
  72. super(parent);
  73. this.configLocations = configLocations;
  74. refresh();
  75. }
  76. protected String[] getConfigLocations() {
  77. return this.configLocations;
  78. }
  79. }