2024年3月6日
Spring ResourcePropertySource Author: HuiFer 源码阅读仓库: SourceHot-spring 全路径: org.springframework.core.io.support.ResourcePropertySource source 依然是 map 结构 getNameForResource 1 2 3 4 5 6 7 8 9 private static String getNameForResource(Resource resource) { // 获取 resource 的介绍 String name = resource.getDescription(); if (!StringUtils.hasText(name)) { // 短类名+@+hashcode name = resource.getClass().getSimpleName() + "@" + System.identityHashCode(resource); } return name; } withName 创建 ResourcePropertySource 对象, 根据 name 属性 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public ResourcePropertySource withName(String name) { if (this.name.equals(name)) { return this; } // Store the original resource name if necessary... if (this.resourceName != null) { if (this.resourceName.equals(name)) { return new ResourcePropertySource(this.resourceName,……
阅读全文
2024年3月6日
Spring PropertiesPropertySource Author: HuiFer 源码阅读仓库: SourceHot-spring 全路径: org.springframework.core.env.PropertiesPropertySource Properties 是 map 结构。可以做类型转换. getPropertyNames 就转换成了父类 MapPropertySource 的方法了 map.keySet() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class PropertiesPropertySource extends MapPropertySource { @SuppressWarnings({"rawtypes", "unchecked"}) public PropertiesPropertySource(String name, Properties source) { super(name, (Map) source); } protected PropertiesPropertySource(String name, Map<String, Object> source) { super(name, source); } @Override public String[] getPropertyNames() { synchronized (this.source) { return super.getPropertyNames(); } } }……
阅读全文
2024年3月6日
Spring MockPropertySource Author: HuiFer 源码阅读仓库: SourceHot-spring 内部 source 是 Properties 类型 withProperty 设置属性名称和属性值 1 2 3 4 public MockPropertySource withProperty(String name, Object value) { this.setProperty(name, value); return this; } setProperty 1 2 3 public void setProperty(String name, Object value) { this.source.put(name, value); } 完整代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 public class MockPropertySource extends PropertiesPropertySource { /** * {@value} is……
阅读全文
2024年3月6日
Spring MapPropertySource Author: HuiFer 源码阅读仓库: SourceHot-spring 类全路径: org.springframework.core.env.MapPropertySource 内部数据结构是一个Map<String,Object> 这是一个对 map 的操作. 整体代码如下. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public class MapPropertySource extends EnumerablePropertySource<Map<String, Object>> { public MapPropertySource(String name, Map<String, Object> source) { super(name, source); } @Override @Nullable public Object getProperty(String name) { // 从map中获取 name 对应的value return this.source.get(name); }……
阅读全文
2024年3月6日
Spring EnumerablePropertySource Author: HuiFer 源码阅读仓库: SourceHot-spring 全路径: org.springframework.core.env.EnumerablePropertySource 在这个类中定义了一个抽象方法getPropertyNames 用来获取所有的 property 的名称 1 public abstract String[] getPropertyNames(); 整体代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public abstract class EnumerablePropertySource<T> extends PropertySource<T> { public EnumerablePropertySource(String name, T source) { super(name, source); } protected EnumerablePropertySource(String name) { super(name); } /** * Return whether this {@code PropertySource} contains a property with the given name. * <p>This implementation……
阅读全文
2024年3月6日
Spring CompositePropertySource Author: HuiFer 源码阅读仓库: SourceHot-spring 全路径: org.springframework.core.env.CompositePropertySource 整体代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 public class CompositePropertySource extends EnumerablePropertySource<Object> { /** * set 集合 */ private final Set<PropertySource<?>> propertySources = new LinkedHashSet<>(); /** * Create……
阅读全文
2024年3月6日
Spring ComparisonPropertySource Author: HuiFer 源码阅读仓库: SourceHot-spring 整体代码如下. 下面几个调用方法会直接抛出异常 getSource containsProperty getProperty 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 static class ComparisonPropertySource extends StubPropertySource { // 异常信息 private static final String USAGE_ERROR = "ComparisonPropertySource instances are for use with collection comparison only"; public ComparisonPropertySource(String name) { super(name); } @Override public Object getSource() { // 抛异常 throw new UnsupportedOperationException(USAGE_ERROR); } @Override public boolean containsProperty(String name) { // 抛异常 throw new UnsupportedOperationException(USAGE_ERROR); } @Override @Nullable public String getProperty(String name) { // 抛异常 throw new UnsupportedOperationException(USAGE_ERROR); }……
阅读全文
2024年3月6日
Spring CommandLinePropertySource Author: HuiFer 源码阅读仓库: SourceHot-spring 类全路径: org.springframework.core.env.CommandLinePropertySource 作用: 用来存储命令行参数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 public abstract class CommandLinePropertySource<T> extends EnumerablePropertySource<T> { public static final String COMMAND_LINE_PROPERTY_SOURCE_NAME = "commandLineArgs"; public static final String DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME = "nonOptionArgs"; private String nonOptionArgsPropertyName = DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME; public CommandLinePropertySource(T source) { //……
阅读全文
2024年3月6日
Spring SystemPropertyPlaceholderResolver 类全路径: org.springframework.util.SystemPropertyUtils.SystemPropertyPlaceholderResolver 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 private static class SystemPropertyPlaceholderResolver implements PropertyPlaceholderHelper.PlaceholderResolver { private final String text; public SystemPropertyPlaceholderResolver(String text) { this.text = text; } @Override @Nullable public String resolvePlaceholder(String placeholderName) { try { String propVal = System.getProperty(placeholderName); if (propVal == null) { // Fall back to searching the system environment. // 获取系统属性 propVal = System.getenv(placeholderName); } return propVal; } catch (Throwable ex) { System.err.println("Could not resolve placeholder '" + placeholderName + "' in [" + this.text + "] as system property: " + ex); return null; } } }……
阅读全文
2024年3月6日
Spring ServletContextPlaceholderResolver 类全路径: org.springframework.web.util.ServletContextPropertyUtils.ServletContextPlaceholderResolver 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 private static class ServletContextPlaceholderResolver implements PropertyPlaceholderHelper.PlaceholderResolver { private final String text; private final ServletContext servletContext; public ServletContextPlaceholderResolver(String text, ServletContext servletContext) { this.text = text; this.servletContext = servletContext; } @Override @Nullable public String resolvePlaceholder(String placeholderName) { try { // servlet 上下文获取 String propVal = this.servletContext.getInitParameter(placeholderName); if (propVal == null) { // Fall back to system properties. propVal = System.getProperty(placeholderName); if (propVal == null) { // Fall back to searching the system environment. propVal = System.getenv(placeholderName); } } return propVal; } catch (Throwable ex) { System.err.println("Could not resolve placeholder '" + placeholderName + "' in……
阅读全文