Spring ResourcePropertySource

  • Author: HuiFer
  • 源码阅读仓库: SourceHot-spring

  • 全路径: org.springframework.core.io.support.ResourcePropertySource

  • source 依然是 map 结构

getNameForResource

  1. private static String getNameForResource(Resource resource) {
  2. // 获取 resource 的介绍
  3. String name = resource.getDescription();
  4. if (!StringUtils.hasText(name)) {
  5. // 短类名+@+hashcode
  6. name = resource.getClass().getSimpleName() + "@" + System.identityHashCode(resource);
  7. }
  8. return name;
  9. }

withName

  • 创建 ResourcePropertySource 对象, 根据 name 属性
  1. public ResourcePropertySource withName(String name) {
  2. if (this.name.equals(name)) {
  3. return this;
  4. }
  5. // Store the original resource name if necessary...
  6. if (this.resourceName != null) {
  7. if (this.resourceName.equals(name)) {
  8. return new ResourcePropertySource(this.resourceName, null, this.source);
  9. }
  10. else {
  11. return new ResourcePropertySource(name, this.resourceName, this.source);
  12. }
  13. }
  14. else {
  15. // Current name is resource name -> preserve it in the extra field...
  16. return new ResourcePropertySource(name, this.name, this.source);
  17. }
  18. }

构造函数

  • 通过 location 字符串读取 resource
  1. public ResourcePropertySource(String name, String location, ClassLoader classLoader) throws IOException {
  2. // 默认资源读取器读取 location 转换成 resource
  3. this(name, new DefaultResourceLoader(classLoader).getResource(location));
  4. }
  • 读取 resource 信息进行存储
  1. public ResourcePropertySource(String name, EncodedResource resource) throws IOException {
  2. // 设置 name + map 对象
  3. // map 对象是 资源信息
  4. super(name, PropertiesLoaderUtils.loadProperties(resource));
  5. // 获取 resource name
  6. this.resourceName = getNameForResource(resource.getResource());
  7. }

完整代码

  1. public class ResourcePropertySource extends PropertiesPropertySource {
  2. /** The original resource name, if different from the given name. */
  3. @Nullable
  4. private final String resourceName;
  5. /**
  6. * Create a PropertySource having the given name based on Properties
  7. * loaded from the given encoded resource.
  8. */
  9. public ResourcePropertySource(String name, EncodedResource resource) throws IOException {
  10. // 设置 name + map 对象
  11. // map 对象是 资源信息
  12. super(name, PropertiesLoaderUtils.loadProperties(resource));
  13. // 获取 resource name
  14. this.resourceName = getNameForResource(resource.getResource());
  15. }
  16. /**
  17. * Create a PropertySource based on Properties loaded from the given resource.
  18. * The name of the PropertySource will be generated based on the
  19. * {@link Resource#getDescription() description} of the given resource.
  20. */
  21. public ResourcePropertySource(EncodedResource resource) throws IOException {
  22. // 设置 key: name, resource 的 name
  23. // 设置 value: resource 资源信息
  24. super(getNameForResource(resource.getResource()), PropertiesLoaderUtils.loadProperties(resource));
  25. this.resourceName = null;
  26. }
  27. /**
  28. * Create a PropertySource having the given name based on Properties
  29. * loaded from the given encoded resource.
  30. */
  31. public ResourcePropertySource(String name, Resource resource) throws IOException {
  32. super(name, PropertiesLoaderUtils.loadProperties(new EncodedResource(resource)));
  33. this.resourceName = getNameForResource(resource);
  34. }
  35. /**
  36. * Create a PropertySource based on Properties loaded from the given resource.
  37. * The name of the PropertySource will be generated based on the
  38. * {@link Resource#getDescription() description} of the given resource.
  39. */
  40. public ResourcePropertySource(Resource resource) throws IOException {
  41. super(getNameForResource(resource), PropertiesLoaderUtils.loadProperties(new EncodedResource(resource)));
  42. this.resourceName = null;
  43. }
  44. /**
  45. * Create a PropertySource having the given name based on Properties loaded from
  46. * the given resource location and using the given class loader to load the
  47. * resource (assuming it is prefixed with {@code classpath:}).
  48. */
  49. public ResourcePropertySource(String name, String location, ClassLoader classLoader) throws IOException {
  50. // 默认资源读取器读取 location 转换成 resource
  51. this(name, new DefaultResourceLoader(classLoader).getResource(location));
  52. }
  53. /**
  54. * Create a PropertySource based on Properties loaded from the given resource
  55. * location and use the given class loader to load the resource, assuming it is
  56. * prefixed with {@code classpath:}. The name of the PropertySource will be
  57. * generated based on the {@link Resource#getDescription() description} of the
  58. * resource.
  59. */
  60. public ResourcePropertySource(String location, ClassLoader classLoader) throws IOException {
  61. this(new DefaultResourceLoader(classLoader).getResource(location));
  62. }
  63. /**
  64. * Create a PropertySource having the given name based on Properties loaded from
  65. * the given resource location. The default thread context class loader will be
  66. * used to load the resource (assuming the location string is prefixed with
  67. * {@code classpath:}.
  68. */
  69. public ResourcePropertySource(String name, String location) throws IOException {
  70. this(name, new DefaultResourceLoader().getResource(location));
  71. }
  72. /**
  73. * Create a PropertySource based on Properties loaded from the given resource
  74. * location. The name of the PropertySource will be generated based on the
  75. * {@link Resource#getDescription() description} of the resource.
  76. */
  77. public ResourcePropertySource(String location) throws IOException {
  78. this(new DefaultResourceLoader().getResource(location));
  79. }
  80. private ResourcePropertySource(String name, @Nullable String resourceName, Map<String, Object> source) {
  81. super(name, source);
  82. this.resourceName = resourceName;
  83. }
  84. /**
  85. * Return the description for the given Resource; if the description is
  86. * empty, return the class name of the resource plus its identity hash code.
  87. * @see org.springframework.core.io.Resource#getDescription()
  88. */
  89. private static String getNameForResource(Resource resource) {
  90. // 获取 resource 的介绍
  91. String name = resource.getDescription();
  92. if (!StringUtils.hasText(name)) {
  93. // 短类名+@+hashcode
  94. name = resource.getClass().getSimpleName() + "@" + System.identityHashCode(resource);
  95. }
  96. return name;
  97. }
  98. /**
  99. * Return a potentially adapted variant of this {@link ResourcePropertySource},
  100. * overriding the previously given (or derived) name with the specified name.
  101. * @since 4.0.4
  102. */
  103. public ResourcePropertySource withName(String name) {
  104. if (this.name.equals(name)) {
  105. return this;
  106. }
  107. // Store the original resource name if necessary...
  108. if (this.resourceName != null) {
  109. if (this.resourceName.equals(name)) {
  110. return new ResourcePropertySource(this.resourceName, null, this.source);
  111. }
  112. else {
  113. return new ResourcePropertySource(name, this.resourceName, this.source);
  114. }
  115. }
  116. else {
  117. // Current name is resource name -> preserve it in the extra field...
  118. return new ResourcePropertySource(name, this.name, this.source);
  119. }
  120. }
  121. /**
  122. * Return a potentially adapted variant of this {@link ResourcePropertySource},
  123. * overriding the previously given name (if any) with the original resource name
  124. * (equivalent to the name generated by the name-less constructor variants).
  125. * @since 4.1
  126. */
  127. public ResourcePropertySource withResourceName() {
  128. if (this.resourceName == null) {
  129. return this;
  130. }
  131. return new ResourcePropertySource(this.resourceName, null, this.source);
  132. }
  133. }