2024年3月6日
EntityResolver Author: HuiFer 源码阅读仓库: huifer-spring 源码路径: org.xml.sax.EntityResolver,非 Spring 类 DelegatingEntityResolver#resolveEntity org.springframework.beans.factory.xml.DelegatingEntityResolver.resolveEntity 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @Override @Nullable public InputSource resolveEntity(@Nullable String publicId, @Nullable String systemId) throws SAXException, IOException { if (systemId != null) { if (systemId.endsWith(DTD_SUFFIX)) { return this.dtdResolver.resolveEntity(publicId, systemId); } else if (systemId.endsWith(XSD_SUFFIX)) { return this.schemaResolver.resolveEntity(publicId, systemId); } } // Fall back to the parser's default behavior. return null; } 上述这段代码是针对 xml 进行校验 1 2 3 <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"> 如上所示以.x……
阅读全文
2024年3月6日
DefaultSingletonBeanRegistry Author: HuiFer 源码阅读仓库: SourceHot-Spring 源码路径: org.springframework.beans.factory.support.DefaultSingletonBeanRegistry 官方提供的测试类: org.springframework.beans.factory.support.DefaultSingletonBeanRegistryTests 类图 注册方法解析 从名字可以看出这是一个单例对象的注册类 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.registerSingleton 测试用例出发 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 @Test public void testSingletons() { DefaultSingletonBeanRegistry beanRegistry = new DefaultSingletonBeanRegistry(); TestBean tb = new TestBean(); beanRegistry.registerSingleton("tb", tb); assertSame(tb, beanRegistry.getSingleton("tb")); TestBean tb2 = (TestBean) beanRegistry.getSingleton("tb2", new ObjectFactory<Object>() { @Override public Object getObject() throws BeansException { return new TestBean(); } }); assertSame(tb2, beanRegistry.getSingleton("tb2")); assertSame(tb, beanRegistry.getSingleton("tb")); assertSame(tb2, beanRegistry.getSingleton("tb2")); assertEquals(2, beanRegistry.getSingletonCount()); String[] names = beanRegistry.getSingletonNames();……
阅读全文
2024年3月6日
Spring 自定义标签解析 Author: HuiFer 源码阅读仓库: SourceHot-Spring 与自定义标签解析相关的类 org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser org.springframework.beans.factory.xml.NamespaceHandlerSupport 开始源码之前先搭建一个环境 环境搭建 创建对象 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class UserXtd { private String userName; private String emailAddress; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getEmailAddress() { return emailAddress; } public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; } } 创建 xsd 文件 1 2 3 4 5 6 7 8 9 10 11 12 13 <?xml version="1.0" encoding="UTF-8" ?> <schema xmlns="http://www.w3.org/2001/XMLSchema"……
阅读全文
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……
阅读全文
-
上一页
-
1
-
...
-
5
-
6
-
7
-
8
-
9
-
...
-
235
-
下一页