2024年3月6日
1 Web 环境中的 SpringMVC 在 Web 环境 中,SpringMVC 是建立在 IoC 容器 基础上的。了解 SpringMVC,首先要了解 Spring 的 IoC 容器 是如何在 Web 环境 中被载入并起作用的。 Spring 的 IoC 是一个独立模块,它并不直接在 Web 容器 中发挥作用,如果要在 Web 环境 中使用 IoC 容器,需要 Spring 为 IoC 设计一个启动过程,把 IoC 容器 导入,并在 Web 容器 中……
阅读全文
2024年3月6日
Spring5 新特性 - spring.components Author: HuiFer 源码阅读仓库: SourceHot-Spring 解析 相关类: org.springframework.context.index.CandidateComponentsIndexLoader 测试用例: org.springframework.context.annotation.ClassPathScanningCandidateComponentProviderTests.defaultsWithIndex,org.springframework.context.index.CandidateComponentsIndexLoaderTests CandidateComponentsIndexLoader是怎么找出来的,全文搜索spring.components 使用介绍 下面是从resources/example/scannable/spring.component……
阅读全文
2024年3月6日
Spring RMI Author: HuiFer 源码阅读仓库: SourceHot-Spring Spring 远程服务调用 DEMO 服务提供方 服务提供方需要准备接口、接口实现泪 接口 1 2 3 public interface IDemoRmiService { int add(int a, int b); } 接口实现 1 2 3 4 5 6 public class IDemoRmiServiceImpl implements IDemoRmiService { @Override public int add(int a, int b) { return a + b; } } xml 配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?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="demoRmiService" class="com.huifer.source.spring.rmi.impl.IDemoRmiServiceImpl"/> <bean id="demoRmi" class="org.springframework.remoting.rmi.RmiServiceExporter"> <!-- 服务--> <property name="service" ref="demoRmiService"/> <!-- 服务名称……
阅读全文
2024年3月6日
Spring JDBC Author: HuiFer 源码阅读仓库: SourceHot-Spring 环境搭建 依赖 1 2 3 compile(project(":spring-jdbc")) compile group: 'com.alibaba', name: 'druid', version: '1.1.21' compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.47' db 配置 1 2 3 4 jdbc.url= jdbc.driverClass= jdbc.username= jdbc.password= 实体对象 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class HsLog { private Integer id; private String source; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getSource() { return source; } public void setSource(String source) { this.source = source; } } DAO 1 2 3 4 5 public interface HsLogDao { List<HsLog> findAll(); void save(HsLog hsLog); } 实现类 1 2 3 4 5 6……
阅读全文
2024年3月6日
循环依赖 一个对象依赖对象闭环到自己 A -> B -> …. ->A tip: 不涉及代理对象问题 解决方法:当一个对象已经实例化完毕了,还未初始化的时候,将它注入到它所依赖的已经实例好的对象(提前暴露对象),使得它所依赖的对象是个完整对象(实例化+初始化),然后再将这个完整对象注入给它。 简单工程(Spring-……
阅读全文
2024年3月6日
BeanPostProcessor 源码分析 BeanPostProcessor 接口也叫 Bean 后置处理器,作用是在 Bean 对象实例化和依赖注入完成后,在配置文件 bean 的 init-method(初始化方法)或者 InitializingBean 的 afterPropertiesSet 的前后添加我们自己的处理逻辑。注意是 Bean 实例化完毕后及依赖注入完成后触发的,接口的源码如下。 1 2 3 4 5 6 7 8 9 10 11 12 public interface BeanPostProcessor { /** * 实例化、依赖注入完毕,……
阅读全文
2024年3月6日
BeanFactoryBeanPostProcessor 源码分析 BeanFactoryBeanPostProcessor 是当 BeanDefinition 读取完元数据(也就是从任意资源中定义的 bean 数据)后还未实例化之前可以进行修改 抄录并翻译官方的语句 BeanFactoryPostProcessor 操作 bean 的元数据配置. 也就是说,Spring IoC 容器允许 BeanFactoryPostProcessor 读取配置元数据, 并可能在容器实例化除 BeanFactoryPostProcessor 实例之外的任何 bean 之前 更改它 tip: 在 BeanFactoryPostProcessor (例如使用 BeanFactory.getBean()) 中使用这些 bean 的实例虽然在技术上……
阅读全文
2024年3月6日
前言 前面我们主要分析了 FileSystemXmlApplicationContext 这个具体的 IoC 容器实现类 的初始化源码,在 IoC 容器 中建立了 beanName 到 BeanDefinition 的数据映射,通过一个 ConcurrentHashMap。现在我们来看一下 Spring 是如何将 IoC 容器中存在依赖关系的 bean 根据配置联系在一起的。 Spring 中触发 IoC 容器“依赖注入” 的方式有两种,一个是应用程序通过 getBea……
阅读全文
2024年3月6日
前言 这篇文章分享一下 spring IoC 容器初始化第三部分的代码,也就是将前面解析出来的 BeanDefinition 对象 注册进 IoC 容器,其实就是存入一个 ConcurrentHashMap<String, BeanDefinition> 中。 (PS:可以结合我 GitHub 上对 Spring 框架源码的翻译注释一起看,会更有助于各位同学理解,地址: spring-beans https://github.com/AmyliaY/spring-beans-reading spring-context https://github.com/AmyliaY/spring-context-reading ) 正文 回过头看一下前面在 DefaultBeanDefinitionDocumentReader 中实现的 processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) 方法。 1 2 3 4 5 6 7 8 9 10 11 12 13……
阅读全文
2024年3月6日
前言 接着上一篇的 BeanDefinition 资源定位开始讲。Spring IoC 容器 BeanDefinition 解析过程就是把用户在配置文件中配置的 bean,解析并封装成 IoC 容器可以装载的 BeanDefinition 对象,BeanDefinition 是 Spring 定义的基本数据结构,其中的属性与配置文件中 bean 的属性相对应。 (PS:可以结合我 GitHub 上对 Spring 框架源码的阅读及个人理解一起……
阅读全文