概述
本文介绍的spring扩展点都是基于spring中bean的生命周期扩展的。
源码分析参考:Spring中Bean的生命周期及源码详解
Spring的扩展点分为两类:
- 某个bean专用扩展点:这个bean通过加注解或者实现接口来实现扩展,只这个bean生效。
- 处理ApplicationContextAware接口的回调,即处理ApplicationContextAware接口的setApplicationContext方法
- 初始化前,即处理@PostConstruct注解的方法
- 初始化,即处理InitializingBean接口的afterPropertiesSet方法
- 某些个bean的通用扩展点:通过实现可扩展接口来实现,所有的bean生效。
- BeanFactoryPostProcessor:Spring容器启动时会将bean信息注册到BeanDefinition中,而BeanFactoryPostProcessor是一个扩展点,修改、新增BeanDefinition中的bean定义信息,添加自定义的元数据
- BeanPostProcessor:BeanPostProcessor是应用于bean初始化前后的扩展点。Spring中的Aware接口回调、bean初始化前(InitializingBean.beforeInit)、bean初始化后(AOP代理对象创建)都是通过Spring内置的BeanPostProcessor实现的
- FactoryBean:FactoryBean是一种特殊的bean,它是一个bean工厂,负责生产其他bean,当生产Bean的过程复杂时,会用到它。
专用扩展点比较简单,这里只说通用扩展点。
BeanFactoryPostProcessor
正常Spring启动的时候会扫描所有的bean,将bean的定义存储在BeanDefinition中。
然后根据BeanDefinition中bean定义的信息实例化单例非延时加载的bean。
BeanFactoryPostProcessor是Spring框架中用于在容器实例化Bean之前修改Bean定义的接口。它在Bean实例化之前运行,因此主要用于修改Bean定义和配置,而不是实际的Bean实例。它是一个非常强大的工具,可用于在容器启动时修改Bean定义、动态注册Bean以及实现条件化的Bean注册,以满足应用程序的特定需求。
源码分析
Spring容器的启动入口都是org.springframework.context.support.AbstractApplicationContext#refresh
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
//看名字就能看出来这是处理BeanFactoryPostProcessors,这里是先扫描bean,把bean的信息注册到BeanDefinition,然后处理BeanFactoryPostProcessors
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons. //实例化单例非延时加载的bean
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
然后执行org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, java.util.List<org.springframework.beans.factory.config.BeanFactoryPostProcessor>)
这个方法里关注两个方法:
- org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors(java.util.Collection<? extends org.springframework.beans.factory.config.BeanFactoryPostProcessor>, org.springframework.beans.factory.config.ConfigurableListableBeanFactory):处理BeanFactoryPostProcessors,可以修改BeanDefinition中bean定义信息。
- org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanDefinitionRegistryPostProcessors:将bean的信息注册到BeanDefinition中。
代码示例
BeanFactoryPostProcessor有三个扩展点:
- 修改Bean的属性值
- 动态注册Bean
修改Bean的属性值
测试的bean
BeanInitTest 中有myTestInfo属性,用于测试BeanFactoryPostProcessor给BeanInitTest的bean对象的myTestInfo属性赋值。
package com.ludk.weixin.service;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
@Service
public class BeanInitTest implements ApplicationContextAware, InitializingBean {
private String myTestInfo;
private String myTestInfo2="初始值";
@Autowired
private ServiceC servicec;
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("初始化");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("ApplicationContextAware 回调方法");
}
@PostConstruct
private void beforeInit(){
System.out.println("初始化前");
}
public String getMyTestInfo() {
return myTestInfo;
}
public void setMyTestInfo(String myTestInfo) {
this.myTestInfo = myTestInfo;
}
public String getMyTestInfo2() {
return myTestInfo2;
}
public void setMyTestInfo2(String myTestInfo2) {
this.myTestInfo2 = myTestInfo2;
}
}
BeanFactoryPostProcessor
这里给beanInitTest bean的myTestInfo属性赋值为addvalue,也加了testAttribute供其他处理器或组件使用。
package com.ludk.weixin.learn;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;
@Component
public class TestBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
BeanDefinition myBeanDefinition=beanFactory.getBeanDefinition("beanInitTest");
//给指定bean的属性赋值
myBeanDefinition.getPropertyValues().add("myTestInfo","addvalue");
//自定义BeanDefinition的属性后面使用
myBeanDefinition.setAttribute("testAttribute",true);
System.out.println("TestBeanFactoryPostProcessor--------------------------------------");
}
}
动态注册Bean
这个BeanFactoryPostProcessor是实现动态注册Bean的功能。
实现的是org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor接口,而这个接口继承了BeanFactoryPostProcessor接口。
这里动态注册了ServiceC的bean。
package com.ludk.weixin.learn;
import com.ludk.weixin.service.ServiceC;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.stereotype.Component;
@Component
public class TestBeanFactoryPostProcessor2 implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
//动态注册ServiceC的Bean
BeanDefinitionBuilder builder=BeanDefinitionBuilder.genericBeanDefinition(ServiceC.class);
AbstractBeanDefinition beanDefinition=builder.getBeanDefinition();
registry.registerBeanDefinition("serviceC",beanDefinition);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
}
BeanInitTest 中依赖注入了ServiceC,用于验证ServiceC的bean是否动态注入成功。
package com.ludk.weixin.service;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
@Service
public class BeanInitTest implements ApplicationContextAware, InitializingBean {
private String myTestInfo;
private String myTestInfo2="初始值";
//用于验证动态注入
@Autowired
private ServiceC servicec;
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("初始化");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("ApplicationContextAware 回调方法");
}
@PostConstruct
private void beforeInit(){
System.out.println("初始化前");
}
public String getMyTestInfo() {
return myTestInfo;
}
public void setMyTestInfo(String myTestInfo) {
this.myTestInfo = myTestInfo;
}
public String getMyTestInfo2() {
return myTestInfo2;
}
public void setMyTestInfo2(String myTestInfo2) {
this.myTestInfo2 = myTestInfo2;
}
}
如何验证上面ServiceC的bean是动态注册的呢?
定义下面的ServiceC时不加@Service等注解,让Spring认为ServiceC不是一个归于Spring管理的bean。这样扫描bean到BeanDefinition中时就没有ServiceC的bean定义。
package com.ludk.weixin.service;
public class ServiceC {
private String a;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
}
调试启动,会发现启动成功。
如果删除掉TestBeanFactoryPostProcessor2类,则没有动态注册的逻辑。启动项目发现报错,找不到ServiceC
BeanPostProcessor
BeanPostProcessor是应用于bean初始化前后的扩展点。Spring中的Aware接口回调、bean初始化前(InitializingBean.beforeInit)、bean初始化后(AOP代理对象创建)都是通过Spring内置的BeanPostProcessor实现的。
主要有两个方法
- postProcessBeforeInitialization:bean初始化前
- postProcessAfterInitialization:bean初始化后
postProcessBeforeInitialization
- 功能:允许开发人员在Bean初始化之前对Bean对象进行预处理。
- 应用:常用于执行一些初始化前的逻辑,例如:验证、属性设置、数据加载等。
- 时机“”在Bean的属性设置之后,但在初始化方法(如init-method)之前被调用。
- 返回值:该方法的返回值是一个对象,允许开发人员修改Bean对象的实例,甚至可以返回一个完全不同的实例对象。
postProcessAfterInitialization
- 功能:允许开发人员在Bean初始化之后对Bean对象进行后处理。
- 用途:通常用于执行一些初始化后的逻辑,例如:初始化之后的验证、注册、清理等。
- 在Bean的初始化方法(如init-method)之后被调用。
- 返回值:同样是一个对象,允许开发人员修改Bean对象的实例,但返回的对象必须是Bean对象的后代(subclass)或者相同的实例。
源码分析
代码示例
定义一个bean,有初始化前、初始化、Aware接口实现。
package com.ludk.weixin.service;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
@Service
public class BeanInitTest implements ApplicationContextAware, InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("初始化");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("ApplicationContextAware 回调方法");
}
@PostConstruct
private void beforeInit(){
System.out.println("初始化前");
}
}
定义一个BeanPostProcessor,专门处理上面的bean。
package com.ludk.weixin.learn;
import com.ludk.weixin.service.BeanInitTest;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
@Component
public class TestBeanPostProcess implements BeanPostProcessor {
//bean初始化前方法
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof BeanInitTest){
System.out.println("com.ludk.weixin.learn.TestBeanPostProcess.postProcessBeforeInitialization:");
}
return bean;
}
//bean初始化后方法
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof BeanInitTest){
System.out.println("com.ludk.weixin.learn.TestBeanPostProcess.postProcessAfterInitialization:");
}
return bean;
}
}
调试代码
在下面的两个位置打断点,调试启动
进入断点后如图:
运行结果:
ApplicationContextAware 回调方法
com.ludk.weixin.learn.TestBeanPostProcess.postProcessBeforeInitialization:
初始化前
初始化
com.ludk.weixin.learn.TestBeanPostProcess.postProcessAfterInitialization:
从下面代码及注释,可以看到BeanPostProcess的执行时机。
protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
invokeAwareMethods(beanName, bean);
return null;
}, getAccessControlContext());
}
else {
invokeAwareMethods(beanName, bean);
}
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
//处理Aware接口的回调,其中包括ApplicationContextAware接口的setApplicationContext方法的执行
//初始化前,也包括自定义BeanPostProcess的前置方法
//都是用BeanProcess的前置处理方法实现的
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
try {
//初始化 ,InitializingBean接口的afterPropertiesSet方法
invokeInitMethods(beanName, wrappedBean, mbd);
}
catch (Throwable ex) {
throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null),
beanName, "Invocation of init method failed", ex);
}
if (mbd == null || !mbd.isSynthetic()) {
//初始化后,包括AOP。也包括自定义BeanPostProcess的后置方法
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
return wrappedBean;
}
如果实现org.springframework.core.Ordered接口,可以指定BeanPostProcess的执行顺序。
个人理解
BeanPostProcess会处理所有的bean,性能肯定差些。如果只有三两个bean需要特殊处理,可以用bean的初始化前、初始化方法等方式解决。
FactoryBean
FactoryBean是一种特殊的bean,它是一个bean工厂,负责生产其他bean,当生产Bean的过程复杂时,会用到它。
BeanFactory与FactoryBean
BeanFactory
org.springframework.beans.factory.BeanFactory
从源码可以看出来,它是用来获取bean的类。都是说Spring的IOC是将对象交由容器管理,BeanFactory就是容器。BeanFactory里有很多bean,用的时候可以通过getBean获取。
负责生产和管理Bean的一个工厂接口,提供一个Spring Ioc容器规范。
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.beans.factory;
import org.springframework.beans.BeansException;
import org.springframework.core.ResolvableType;
import org.springframework.lang.Nullable;
public interface BeanFactory {
String FACTORY_BEAN_PREFIX = "&";
Object getBean(String var1) throws BeansException;
<T> T getBean(String var1, Class<T> var2) throws BeansException;
Object getBean(String var1, Object... var2) throws BeansException;
<T> T getBean(Class<T> var1) throws BeansException;
<T> T getBean(Class<T> var1, Object... var2) throws BeansException;
<T> ObjectProvider<T> getBeanProvider(Class<T> var1);
<T> ObjectProvider<T> getBeanProvider(ResolvableType var1);
boolean containsBean(String var1);
boolean isSingleton(String var1) throws NoSuchBeanDefinitionException;
boolean isPrototype(String var1) throws NoSuchBeanDefinitionException;
boolean isTypeMatch(String var1, ResolvableType var2) throws NoSuchBeanDefinitionException;
boolean isTypeMatch(String var1, Class<?> var2) throws NoSuchBeanDefinitionException;
@Nullable
Class<?> getType(String var1) throws NoSuchBeanDefinitionException;
@Nullable
Class<?> getType(String var1, boolean var2) throws NoSuchBeanDefinitionException;
String[] getAliases(String var1);
}
FactoryBean
org.springframework.beans.factory.FactoryBean
FactoryBean是存储在BeanFactory中的一种bean,通过FactoryBean可以获取其他一系列bean。
对于复杂的Bean对象初始化创建使用其可封装对象的创建细节。
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.beans.factory;
import org.springframework.lang.Nullable;
public interface FactoryBean<T> {
String OBJECT_TYPE_ATTRIBUTE = "factoryBeanObjectType";
@Nullable
T getObject() throws Exception;
@Nullable
Class<?> getObjectType();
default boolean isSingleton() {
return true;
}
}
FactoryBean实例
FactoryBean
package com.ludk.weixin.learn;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* 实现FactoryBean接口,支持泛型,重写getObject、getObjectType、isSingleton方法
*/
@Component
public class MyFactoryBean2 implements FactoryBean<ServiceI> {
@Value("${myinfo.beanname}")
private String beanName;
//ServiceI 类型bean对象实例,根据配置文件中的配置不同,获取不同实例
@Override
public ServiceI getObject() throws Exception {
if ("bean1".equals(beanName)){
return new ServiceImpl1();
}else {
return new ServiceImpl2();
}
}
//获取bean对象类型
@Override
public Class<?> getObjectType() {
return ServiceI.class;
}
//是否是单例
@Override
public boolean isSingleton() {
return true;
}
}
根据FactoryBean获取实例
package com.ludk.weixin.controller;
import com.ludk.weixin.common.tool.RedisUtil;
import com.ludk.weixin.entity.Product;
import com.ludk.weixin.learn.MyFactoryBean;
import com.ludk.weixin.learn.ServiceBean;
import com.ludk.weixin.learn.ServiceI;
import com.ludk.weixin.service.IProductService;
import com.ludk.weixin.service.TestLockService;
import com.ludk.weixin.service.TestLockThread;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 实现ApplicationContextAware接口,在ProductController 初始化前通过setApplicationContext方法获取ServiceI 的实例,相当于将ServiceI 注入进来。
*/
@Controller
@Slf4j
@RequestMapping("/product")
public class ProductController implements ApplicationContextAware{
@Autowired
IProductService productService;
@Autowired
TestLockService testLockService;
@Resource(type = ServiceBean.class)
ServiceBean serviceBean;
//无法通过依赖注入注入进来
ServiceI serviceI;
/**
*测试方法
* @param userId
* @return
*/
@RequestMapping("/myProduct")
@ResponseBody
Object myProduct(@RequestParam("userId") Long userId) throws InterruptedException {
//测试方法
serviceI.test();
return "success";
}
//通过FactoryBean将ServiceI注入进来
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.serviceI=(ServiceI) applicationContext.getBean("myFactoryBean2");
}
}
配置文件、ServiceI接口及ServiceI接口的两个实现类这里就不描述了。
这个例子是根据配置文件的配置不同,Controller中注入不同的ServiceI接口的实现类。