2024年3月6日
Spring 自定义属性解析器 Author: HuiFer 源码阅读仓库: SourceHot-Spring 用例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <?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 class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="propertyEditorRegistrars"> <list> <bean class="com.huifer.source.spring.bean.DatePropertyRegister"/> </list> </property> <property name="customEditors"> <map> <entry key="java.util.Date" value="com.huifer.source.spring.bean.DatePropertyEditor"> </entry> </map> </property> </bean> <bean id="apple" class="com.huifer.source.spring.bean.Apple"> <property name="date" value="2020-01-01 01:01:01"/> </bean> </beans> 1 2 3 4 5 6 7 8 public class DatePropertyRegister implements PropertyEditorRegistrar { @Override public void registerCustomEditors(PropertyEditorRegistry registry) { registry.registerCustomEditor(Date.class, new CustomDateEditor( new SimpleDateFormat("yyyy-MM-dd"), true) ); } } 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 public class DatePropertyEditor extends PropertyEditorSupport {……
阅读全文
2024年3月6日
Spring Conditional Author: HuiFer 源码阅读仓库: SourceHot-spring Conditional 1 2 3 4 5 6 7 8 9 10 11 @Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Conditional { /** * 多个匹配器接口 */ Class<? extends Condition>[] value(); } Condition @FunctionalInterface public interface Condition { /** * 匹配,如果匹配返回true进行初始化,返回false跳过初始化 */ boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata); } ConditionContext 上下文 AnnotatedTypeMetadata 注解信息 ConditionContext public interface ConditionContext { /** * bean的定义 */ BeanDefinitionRegistry getRegistry(); /** * bean 工厂 */ @Nullable ConfigurableListableBeanFactory getBeanFactory(); /** * 环境 */ Environment getEnvironment(); /** * 资……
阅读全文
2024年3月6日
Spring BeanNameGenerator Author: HuiFer 源码阅读仓库: SourceHot-spring org.springframework.beans.factory.support.BeanNameGenerator 方法用来生成 beanName 1 2 3 4 5 6 7 8 9 10 11 12 13 public interface BeanNameGenerator { /** * Generate a bean name for the given bean definition. * 生成 beanName * @param definition the bean definition to generate a name for * @param registry the bean definition registry that the given definition * is supposed to be registered with * @return the generated bean name */ String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry); } DefaultBeanNameGenerator org.springframework.beans.factory.support.DefaultBeanNameGenerator 调用工具类方法进行生成 1 2 3 4 @Override public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { return BeanDefinitionReaderUtils.generateBeanName(definition, registry); } ClassName + # + 十六进制字符 parentName + $child + # + 十……
阅读全文
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>……
阅读全文
-
上一页
-
1
-
...
-
38
-
39
-
40
-
41
-
42
-
...
-
250
-
下一页