2024年3月6日
Spring BeanFactoryPostProcessor Author: HuiFer 源码阅读仓库: SourceHot-Spring 作用: 定制或修改BeanDefinition的属性 Demo 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 public class ChangeAttrBeanPostProcessor implements BeanFactoryPostProcessor { private Set<String> attr; public ChangeAttrBeanPostProcessor() { attr = new HashSet<>(); } public Set<String> getAttr() { return attr; } public void setAttr(Set<String> attr) { this.attr = attr; } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames(); for (String beanName : beanDefinitionNames) { BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName); StringValueResolver stringValueResolver = new StringValueResolver() { @Override public String resolveStringValue(String……
阅读全文
2024年3月6日
Spring BeanDefinitionReaderUtils Author: HuiFer 源码阅读仓库: SourceHot-spring createBeanDefinition org.springframework.beans.factory.support.BeanDefinitionReaderUtils.createBeanDefinition 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public static AbstractBeanDefinition createBeanDefinition( @Nullable String parentName, @Nullable String className, @Nullable ClassLoader classLoader) throws ClassNotFoundException { GenericBeanDefinition bd = new GenericBeanDefinition(); // 设置 父bean bd.setParentName(parentName); if (className != null) { if (classLoader != null) { // 设置 class // 内部是通过反射创建 class bd.setBeanClass(ClassUtils.forName(className, classLoader)); } else { // 设置 class name bd.setBeanClassName(className); } } return bd; } generateBeanName org.springframework.beans.factory.support.BeanDefinitionReaderUtils.generateBeanName(org.springframework.beans.factory.config.BeanDefinition, org.springframework.beans.factory.support.BeanDefinitionRegistry, boolean) 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……
阅读全文
2024年3月6日
Spring BeanDefinitionParserDelegate Author: HuiFer 源码阅读仓库: SourceHot-spring 全路径org.springframework.beans.factory.xml.BeanDefinitionParserDelegate 解析 xml 中标签的委托类 在这个类中定义常量如下,为后续解析提供帮助 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……
阅读全文
2024年3月6日
Spring initApplicationEventMulticaster Author: HuiFer 源码阅读仓库: SourceHot-Spring demo 1 2 3 4 5 6 7 8 9 10 11 package com.huifer.source.spring.applicationListener; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; public class DemoApplicationListener implements ApplicationListener { @Override public void onApplicationEvent(ApplicationEvent event) { System.out.println("com.huifer.source.spring.applicationListener.DemoApplicationListener.onApplicationEvent"); } } 1 2 3 4 5 6 7 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="demoApplicationListener" class="com.huifer.source.spring.applicationListener.DemoApplicationListener"/> </beans> 1 2 3 4 5 public class ListenerSourceCode { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Listener-demo.xml"); } } 初始化入口 org.springframework.context.support.AbstractAppli……
阅读全文
2024年3月6日
Spring AnnotationUtils Author: HuiFer 源码阅读仓库: SourceHot-Spring org.springframework.core.annotation.AnnotationUtils提供了注解相关的方法 getAnnotation: 获取注解 findAnnotation: 寻找注解 getValue: 获取属性值 getDefaultValue: 获取默认值 getAnnotation 测试用例如下 1 2 3 4 5 6 7 @Test public void findMethodAnnotationOnLeaf() throws Exception { Method m = Leaf.class.getMethod("annotatedOnLeaf"); assertNotNull(m.getAnnotation(Order.class)); assertNotNull(getAnnotation(m, Order.class)); assertNotNull(findAnnotation(m, Order.class)); } org.springframework.core.annotation.AnnotationUtils.getAnnotation(java.lang.reflect.Method, java.lang.Class<A>) 1 2 3 4 5 6 7 8 9 10 11 12……
阅读全文
2024年3月6日
Spring StubPropertySource Author: HuiFer 源码阅读仓库: SourceHot-spring 整体代码如下. 通过 StubPropertySource 的 getProperty 方法永远返回 null 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public static class StubPropertySource extends PropertySource<Object> { public StubPropertySource(String name) { super(name, new Object()); } /** * Always returns {@code null}. */ @Override @Nullable public String getProperty(String name) { return null; } }……
阅读全文
2024年3月6日
Spring SimpleCommandLinePropertySource 全路径: org.springframework.core.env.SimpleCommandLinePropertySource 1 public class SimpleCommandLinePropertySource extends CommandLinePropertySource<CommandLineArgs> {} SimpleCommandLinePropertySource 的 source 类型是 CommandLineArgs 具体解释请看下面分析 CommandLineArgs 两个内部属性 1 2 3 4 5 6 7 8 9 10 11 12 class CommandLineArgs { /** * 选项参数列表 */ private final Map<String, List<String>> optionArgs = new HashMap<>(); /** * 非选项参数列表 */ private final List<String> nonOptionArgs = new ArrayList<>(); } addOptionArg 添加 选项参数 1 2 3 4 5 6 7 8 public void addOptionArg(String optionName, @Nullable String optionValue) { if (!this.optionArgs.containsKey(optionName)) { this.optionArgs.put(optionName, new ArrayList<>()); } if (optionValue != null) { this.optionArgs.get(optionName).add(optionValue); } } getOptionNames 获取选项参数列表 1 2 3 public Set<String>……
阅读全文
2024年3月6日
Spring SimpleCommandLineArgsParser Author: HuiFer 源码阅读仓库: SourceHot-spring 类全路径: `org.springframework.core.env.SimpleCommandLineArgsParser 类作用: 将命令行参数解析成 org.springframework.core.env.CommandLineArgs 完整代码如下. 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 class SimpleCommandLineArgsParser { /** * Parse the given {@code String} array based on the rules described {@linkplain * SimpleCommandLineArgsParser above}, returning a fully-populated * {@link CommandLineArgs} object. * @param args command line arguments, typically from a {@code main()} method */ public CommandLineArgs parse(String... args) { CommandLineArgs commandLineArgs = new CommandLineArgs(); for (String arg : args) { if (arg.startsWith("--")) { String optionText = arg.substring(2, arg.length()); String……
阅读全文
2024年3月6日
Spring ServletContextPropertySource Author: HuiFer 源码阅读仓库: SourceHot-spring 类全路径: org.springframework.web.context.support.ServletContextPropertySource 内部数据结构是 ServletContext 接口 整体代码如下. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class ServletContextPropertySource extends EnumerablePropertySource<ServletContext> { public ServletContextPropertySource(String name, ServletContext servletContext) { super(name, servletContext); } @Override public String[] getPropertyNames() { // javax.servlet.ServletContext.getInitParameterNames 方法调用 return StringUtils.toStringArray(this.source.getInitParameterNames()); } @Override @Nullable public String getProperty(String name) { // javax.servlet.ServletContext.getInitParameter return this.source.getInitParameter(name); } }……
阅读全文
2024年3月6日
Spring ServletConfigPropertySource Author: HuiFer 源码阅读仓库: SourceHot-spring 类全路径: org.springframework.web.context.support.ServletConfigPropertySource 内部数据结构是 ServletConfig 整体代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class ServletConfigPropertySource extends EnumerablePropertySource<ServletConfig> { public ServletConfigPropertySource(String name, ServletConfig servletConfig) { super(name, servletConfig); } @Override public String[] getPropertyNames() { // javax.servlet.ServletConfig.getInitParameterNames return StringUtils.toStringArray(this.source.getInitParameterNames()); } @Override @Nullable public String getProperty(String name) { // javax.servlet.ServletConfig.getInitParameter return this.source.getInitParameter(name); } }……
阅读全文