超详细Spring、SpringBoot所有扩展点整理

欢迎来我博客阅读更多的文章~https://endwas.cn

spring中有太多的扩展点,总记得很零散,有些扩展点也只简单知道用途,不知其所以然,所以写篇博客总结归纳一下,如果有遗漏欢迎告知,会进行补充~

1.Spring中扩展点

1.BeanFactoryProcessor

容器扩展接口,调用时机在Spring读取BeanDefinition信息之后,实例化bean之前。
可用作修改已经注册的BeanDefinition,比如修改类的属性、是否懒加载、类名等。

/**
*	一个接口方法用于
**/
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
子接口 BeanDefinitionRegistryPostProcessor

这个接口也只有一个实现方法,这个方法参数是BeanDefinitionRegistry,我们的容器是实现了这个接口,所以可以对容器中的beandefinition进行增删查改。

void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;

2.BeanPostProcessor

Bean的后置处理器,在bean的生命周期各个阶段起到扩展和应用,常见的应用有初始化前@postConstruct解析执行,初始化后aop的动态代理。

	/**
	*	初始化前的扩展
	**/
	@Nullable
	default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}
	
	@Nullable
	default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

1.子接口InstantiationAwareBeanPostProcessor

Bean实例化处理器,相当于在创建这个bean的前后做扩展,在bean通过反射创建前后的扩展。

  • postProcessAfterInstantiation:注意它的返回值是boolean而不是obj,因为它的返回值是决定要不要调用postProcessPropertyValues方法。
  • postProcessProperties: 顾名思义处理属性值 作用:对属性值进行修改,如果postProcessAfterInstantiation方法返回false,该方法可能不会被调用。可以在该方法内对属性值进行修改。
	@Nullable
	default Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		return null;
	}

	default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
		return true;
	}

	@Nullable
	default PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)
			throws BeansException {

		return null;
	}

2.子接口SmartInstantiationAwareBeanPostProcessor

InstantiationAwareBeanPostProcessor的子接口,比父类多了三个方法,我们常用的AbstractAdvisorAutoProxyCreator用于aop和提前暴露解决循环依赖的就是该接口的实现。

  • predictBeanType:该触发点发生在postProcessBeforeInstantiation之前,这个方法用于预测Bean的类型,返回第一个预测成功的Class类型,如果不能预测返回null;当你调用BeanFactory.getType(name)时当通过bean的名字无法得到bean类型信息时就调用该回调方法来决定类型信息。
  • determineCandidateConstructors:该触发点发生在postProcessBeforeInstantiation之后,用于确定该bean的构造函数之用,返回的是该bean的所有构造函数列表。用户可以扩展这个点,来自定义选择相应的构造器来实例化这个bean。
  • getEarlyBeanReference:该触发点发生在postProcessAfterInstantiation之后,当有循环依赖的场景,当bean实例化好之后,为了防止有循环依赖,会提前暴露回调方法,用于bean实例化的后置处理。这个方法就是在提前暴露的回调方法中触发。
	@Nullable
	default Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {
		return null;
	}

	
	@Nullable
	default Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName)
			throws BeansException {

		return null;
	}
	
	default Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
		return bean;
	}
3.MergedBeanDefinitionPostProcessor

这个用于收集bean上的注解,比如我们常见的@Value、@NacosValue、@Mapper等,收集了,收集好的数据缓存在injectionMetadataCache中,以便后续比如属性注入的时候使用。

public interface MergedBeanDefinitionPostProcessor extends BeanPostProcessor {

	
	void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName);


	default void resetBeanDefinition(String beanName) {
	}

}

3.ApplicationListener

ApplicationListener可以监听某个事件的event,触发时机可以穿插在业务方法执行过程中,用户可以自定义某个业务事件。但是spring内部也有一些内置事件,这种事件,可以穿插在启动调用中。我们也可以利用这个特性,来自己做一些内置事件的监听器来达到和前面一些触发点大致相同的事情。最常见的Spring Cloud中的注册就是通过监听WebServerInitializedEvent事件来进行服务注册。

当然还有很多不同的事件,在Spring生命周期过程中。会进行触发。

事件触发时期
ContextRefreshedEventApplicationContext 被初始化或刷新时
ContextStartedEvent当使用 ConfigurableApplicationContext (ApplicationContext子接口)接口中的 start() 方法启动 ApplicationContext时,该事件被发布。
ContextStoppedEvent当使用 ConfigurableApplicationContext接口中的 stop()停止ApplicationContext 时,发布这个事件
ContextClosedEvent当使用 ConfigurableApplicationContext接口中的 close()方法关闭 ApplicationContext 时,该事件被发布。
RequestHandledEvent当Spring处理用户请求结束后,系统会自动触发该事件

3.xxxxAware

资源注入,一般我们常用的有BeannameAware用于设置bean名字、BeanFactoryAware用于设置beanFactory等,还有很多aware接口,我们根据bean对应所需的属性设置即可。

4.InitializingBean/DisposableBean

InitializingBean 初始化Bean时候判断bean是否实现该接口,如果实现则会调用afterPropertiesSet方法,它的调用时机在BeanPostProcessor的before、after方法中间。用于bean的初始化

public class CustomizeInitializing implements InitializingBean{
	// 用法,实现对应afterPropertiesSet方法。
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("[InitializingBean] bean");
    }
}

DisposableBean 销毁bean的是判断是否实现该接口,如果实现就会调用destroy方法。

public class CustomizeDestroy implements DisposableBean {
    @Override
    public void destroy() throws Exception {
        System.out.println("[DisposableBean] bean");
    }
}

5.FactoryBean

顾名思义工厂bean,当我们调用getBean获取bean的时候,如果判断该bean是工厂bean那么就会调用工厂的getObject方法来获取。本质就是避免了我们创建bean所需要提供的大量配置,用工厂bean简洁创建。避免底层繁琐实现。

最常用的实现就是mybatis中sqlSessionFactory,用工厂方法帮助我们创建每个连接器。

public interface FactoryBean<T> {


	@Nullable
	T getObject() throws Exception;

	@Nullable
	Class<?> getObjectType();

	default boolean isSingleton() {
		return true;
	}

}

6.SmartInitializingSingleton

这个类的作用是在spring容器管理的所有**单例对象(非懒加载对象)**初始化完成之后调用的回调接口。它只有一个方法afterSingletonsInstantiated

public class TestSmartInitializingSingleton implements SmartInitializingSingleton {
    @Override
    public void afterSingletonsInstantiated() {
        System.out.println("[TestSmartInitializingSingleton]");
    }
}

7.@postConstruct

这个注解是用于实例化后的初始化操作的,和InitializingBean作用很像,但两者的实现位置不一样,
postConstruct是在BeanPostProcessor的前置方法中判断和回调的。
而InitializingBean则是前置方法执行完进行初始化操作中回调的。

我们定位到CommonAnnotationBeanPostProcessor这个处理器,在父类postProcessBeforeInitialization通过反射回调。

	public CommonAnnotationBeanPostProcessor() {
		setOrder(Ordered.LOWEST_PRECEDENCE - 3);
		// 注入了postConstruct和preDestroy注解
		setInitAnnotationType(PostConstruct.class);
		setDestroyAnnotationType(PreDestroy.class);
		ignoreResourceType("javax.xml.ws.WebServiceContext");
	}

8.CommandLineRunner

这个接口也只有一个方法:run(String… args),触发时机为整个项目启动完毕后,自动执行。如果有多个CommandLineRunner,可以利用@Order来进行排序。

使用场景:用户扩展此接口,进行启动项目之后一些业务的预处理。

public class TestCommandLineRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("[TestCommandLineRunner]");
    }
}

9.ApplicationRunner&CommandLineRunner

这两个接口是SpringBoot提供的,作用是获取一些命令行参数 比如–port = 8080、a=1等,ApplicationRunner能获取更多的参数,我们可以获取到参数做自己的扩展等。

private void callRunners(ApplicationContext context, ApplicationArguments args) {
	List<Object> runners = new ArrayList<>();
	runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
	runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
	AnnotationAwareOrderComparator.sort(runners);
	for (Object runner : new LinkedHashSet<>(runners)) {
		if (runner instanceof ApplicationRunner) {
			callRunner((ApplicationRunner) runner, args);
		}
		if (runner instanceof CommandLineRunner) {
			callRunner((CommandLineRunner) runner, args);
		}
	}
}

10.SpringApplicationRunListener&ApplicationContextInitializer

这两个接口无法通过@Component组件引入,但可以配置在Spring.factories方法引入,因为他是用SPI机制去扫描获取对应的监听器和初始化。

总结

最后附上一张流程图,在bean生命周期的各个位置的扩展点,bean是spring关键的功能,所以主要扩展点也都在bean的生命周期上。这就是Spring和SpringBoot所有的扩展点,如果有遗漏欢迎留言告知,我们会后续补充~
另外因为ComponentScan是发生在refresh阶段的,也就是说如果在此阶段前尝试去获取bean是无法拿到我们自己引入该类型的bean,也就是实现会无法应用,但我们可以通过Spring.factories文件配置,因为部分组件会在启动时候去扫描获取。
在这里插入图片描述

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
学习尚硅谷视频整理的文档 Spring Boot 1 1 Spring Boot入门 4 1.1 简介 4 1.2 微服务(martin fowler发表了一篇文章) 5 1.3 环境约束 7 1.4 第一个Spring Boot项目(jar):HelloWorld 8 1.5 入门案例详解 11 1.5.1 POM文件 11 1.5.2 主程序类,主入口类 12 1.6 使用Spring Initializer向导快速创建Spring Boot 16 2 Spring Boot配置 18 2.1 配置文件 18 2.2 YML语法 19 2.3 YML配置文件值获取 21 2.4 properties配置文件乱码问题 24 2.5 @ConfigurationProperties与@Value的区别 25 2.6 配置@PropertySource、@ImportResource、@Bean 27 2.7 配置文件占位符 30 2.8 Profile多环境支持 31 2.9 配置文件的加载位置 33 2.10 外部配置加载顺序 36 2.11 自动配置原理 37 2.12 @Conditional派生注解 41 3 Spring Boot与日志 42 3.1 日志框架分类和选择 42 3.2 SLF4j使用 43 3.3 其他日志框架统一转换成slf4j+logback 44 3.4 Spring Boot日志使用 45 3.5 Spring Boot默认配置 47 3.6 指定日志文件和日志Profile功能 52 3.7 切换日志框架(不使用SLF4j+LogBack) 54 4 Spring Boot与Web开发 55 4.1 Web开发简介 55 4.2 静态资源映射规则 56 4.3 引入Thymeleaf 60 4.4 Thymeleaf语法 61 4.5 SpringMVC自动配置原理 67 4.6 SpringBoot扩展与全面接管 70 4.7 如何修改SpringBoot的默认配置 72 4.8 【实验】CRUD操作 73 4.8.1 默认访问首页 73 4.8.2 登录页面国际化 74 4.8.3 登录 80 4.8.4 拦截器进行登录检查 81 4.8.5 实验要求(没按要求做,不想改了!) 82 4.8.6 CRUD-员工列表 83 4.8.7 CRUD-员工修改 86 4.8.8 CRUD-员工添加 87 4.8.9 CRUD-员工删除 88 4.9 错误处理原理&错误页面定制 90 4.10 配置嵌入式Servlet容器(springboot 1.50版本) 97 4.10.1 如何定制和修改Servelt容器的相关配置 97 4.10.2 注册servlet三大组件【servlet,filter,listener】 98 4.10.3 替换为其他嵌入式容器 102 4.10.4 嵌入式servlet容器自动配置原理 103 4.10.5 嵌入式servlet容器启动原理 103 4.11 使用外置的Servlet容器 104 4.11.1 步骤 104 4.11.2 原理 107 5 Spring Boot与Docker(虚拟化容器技术) 110 5.1 简介 110 5.2 核心概念 111 5.3 安装Docker 112 5.4 Docker常用命令&操作 113 5.5 安装MySQL示例 114 6 Spring Boot与数据访问 115 6.1 JDBC 115 6.1.1 实现 115 6.1.2 自动配置原理 116 6.2 整合Durid数据源 117 6.3 整合Mybatis 122 6.3.1 注解版 123 6.3.2 配置文件版 124 6.4 整合SpringData JPA 125 6.4.1 SpringData简介 125 6.4.2 整合 126 7 Spring Boot启动配置原理 128 7.1 启动流程(Springboot 1.50版本) 128 7.1.1 创建SpringApplication对象 129 7.1.2 运行run方法 130 7.1.3 编写事件监听机制 132 8 Spring Boot自定义starters 136 8.1 概述 136 8.2 步骤 137 9 更多Springboot整合示例 144 10 Spring Boot与缓存 145 10.1 JSR107缓存规范 145 10.2 Spring的缓存抽象 146 10.2.1 基本概念 146 10.2.2 整合项目 146 10.2.3 CacheEnable注解 148 10.2.4 Cache注解 150 10.3 整合redis 154 10.3.1 在Docker上安装redis 154 10.3.2 Redis的Template 154 10.3.3 整合(百度) 155

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值