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……
阅读全文
2024年3月6日
Spring PropertyPlaceholderConfigurerResolver 类全路径: org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.PropertyPlaceholderConfigurerResolver 这个类是从 Properties 中获取属性 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 private final class PropertyPlaceholderConfigurerResolver implements PlaceholderResolver { private final Properties props; private PropertyPlaceholderConfigurerResolver(Properties props) { this.props = props; } @Override @Nullable public String resolvePlaceholder(String placeholderName) { return PropertyPlaceholderConfigurer.this.resolvePlaceholder(placeholderName, this.props, systemPropertiesMode); } } 详细方法如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @Nullable protected String resolvePlaceholder(String placeholder, Properties props, int systemPropertiesMode) { String propVal = null; if (systemPropertiesMode == SYSTEM_PROPERTIES_MODE_OVERRIDE) { propVal = resolveSystemProperty(placeholder); } if (propVal == null) { propVal = resolvePlaceholder(placeholder, props); } if (propVal == null && systemPropertiesMode == SYSTEM_PROPERTIES_MODE_FALLBACK) { propVal = resolveSystemProperty(placeholder); } return propVal; } 1 2 3 4……
阅读全文
2024年3月6日
Spring PlaceholderResolver 类全路径: org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver 类作用将占位符中的内容替换成属性值. 假设现有属性表: user.dir = c:\home 传入参数 user.dir 会获得 c:\home 1 2 3 4 5 6 7 8 9 10 11 12 @FunctionalInterface public interface PlaceholderResolver { /** * Resolve the supplied placeholder name to the replacement value. * @param placeholderName the name of the placeholder to resolve * @return the replacement value, or {@code null} if no replacement is to be made */ @Nullable String resolvePlaceholder(String placeholderName); } 类图如下……
阅读全文
2024年3月6日
Spring 事务 Author: HuiFer 源码阅读仓库: SourceHot-Spring 声明式事务 Propagation 事务传播 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 public enum Propagation { /** * 有事务则加入,没有则新建 */ REQUIRED(TransactionDefinition.PROPAGATION_REQUIRED), /** * 有事务就用,如果没有就不开启(继承关系) * @see org.springframework.transaction.support.AbstractPlatformTransactionManager#setTransactionSynchronization */ SUPPORTS(TransactionDefinition.PROPAGATION_SUPPORTS), /** * 必须在已有事务中 */ MANDATORY(TransactionDefinition.PROPAGATION_MANDATORY), /** * 不管……
阅读全文
2024年3月6日
引言:庞大的代码量让人心生怠倦,有趣的故事让技术也疯狂。 大家好,我是 IoC 容器家族的第 17 代传人,我们家族世世代代在 spring 商业街上卖烤面筋,大家都叫我“面筋哥”,另外我爹还给我起了个高大上的英文名字,叫“FileSystemXmlApplicationContext”,但有群臭猴子嫌麻烦,……
阅读全文