2024年3月6日
1 SpringMVC 应用场景 在使用 SpringMVC 时,除了要在 web.xml 中配置 ContextLoaderListener 外,还要对 DispatcherServlet 进行配置。作为一个 Servlet,这个 DispatcherServlet 实现的是 Sun 的 J2EE 核心模式 中的 前端控制器模式(Front Controller), 作为一个前端控制器,所有的 Web 请求 都需要通过它来进行转发、匹配、数据处理,然后转由页面进行展现,因此这个 DispatcerServlet 可以看……
阅读全文
2024年3月6日
Spring-MVC 跨域 CrossOrigin 注解 通过注解设置跨域 demo 如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com.huifer.source.controller; import org.springframework.web.bind.annotation.*; import java.util.HashMap; @CrossOrigin(maxAge = 3600) @RequestMapping("/") @RestController public class JSONController { @ResponseBody @GetMapping(value = "/json") public Object ob() { HashMap<String, String> hashMap = new HashMap<>(); hashMap.put("1", "a"); return hashMap; } } 切入点: org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#registerHandlerMethod org.springframework.web.servlet.handler.AbstractHandlerMethodMapp……
阅读全文
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……
阅读全文