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. import org.springframework.core.io.FileSystemResource;
  20. import org.springframework.core.io.Resource;
  21. /**
  22. * Standalone XML application context, taking the context definition files
  23. * from the file system or from URLs. Mainly useful for test harnesses,
  24. * but also for standalone environments.
  25. *
  26. * <p>Treats resource paths as file system resources, when using
  27. * ApplicationContext.getResource. Resource paths are considered relative
  28. * to the current VM working directory, even if they start with a slash.
  29. *
  30. * <p>The config location defaults can be overridden via setConfigLocations,
  31. * respectively via the "contextConfigLocation" parameters of ContextLoader and
  32. * FrameworkServlet. Config locations can either denote concrete files like
  33. * "/myfiles/context.xml" or Ant-style patterns like "/myfiles/*-context.xml"
  34. * (see PathMatcher javadoc for pattern details).
  35. *
  36. * <p>Note: In case of multiple config locations, later bean definitions will
  37. * override ones defined in earlier loaded files. This can be leveraged to
  38. * deliberately override certain bean definitions via an extra XML file.
  39. *
  40. * @author Rod Johnson
  41. * @author Juergen Hoeller
  42. * @see #getResource
  43. * @see #getResourceByPath
  44. * @see #getResourcePatternResolver
  45. */
  46. public class FileSystemXmlApplicationContext extends AbstractXmlApplicationContext {
  47. private String[] configLocations;
  48. /**
  49. * Create a new FileSystemXmlApplicationContext, loading the definitions
  50. * from the given XML file.
  51. * @param configLocation file path
  52. */
  53. public FileSystemXmlApplicationContext(String configLocation) throws BeansException {
  54. this.configLocations = new String[] {configLocation};
  55. refresh();
  56. }
  57. /**
  58. * Create a new FileSystemXmlApplicationContext, loading the definitions
  59. * from the given XML files.
  60. * @param configLocations array of file paths
  61. */
  62. public FileSystemXmlApplicationContext(String[] configLocations) throws BeansException {
  63. this.configLocations = configLocations;
  64. refresh();
  65. }
  66. /**
  67. * Create a new FileSystemXmlApplicationContext with the given parent,
  68. * loading the definitions from the given XML files.
  69. * @param configLocations array of file paths
  70. * @param parent the parent context
  71. */
  72. public FileSystemXmlApplicationContext(String[] configLocations, ApplicationContext parent)
  73. throws BeansException {
  74. super(parent);
  75. this.configLocations = configLocations;
  76. refresh();
  77. }
  78. protected String[] getConfigLocations() {
  79. return this.configLocations;
  80. }
  81. /**
  82. * Resolve resource paths as file system paths.
  83. * <p>Note: Even if a given path starts with a slash, it will get
  84. * interpreted as relative to the current VM working directory.
  85. * This is consisted with the semantics in a Servlet container.
  86. * @param path path to the resource
  87. * @return Resource handle
  88. */
  89. protected Resource getResourceByPath(String path) {
  90. if (path != null && path.startsWith("/")) {
  91. path = path.substring(1);
  92. }
  93. return new FileSystemResource(path);
  94. }
  95. }