Spring擴(kuò)展接口知識(shí)總結(jié)
一、BeanPostProcessor
BeanPostProcessor 接口是 Spring 提供的眾多接口之一,他的作用主要是如果需要在Spring 容器完成 Bean 的實(shí)例化、配置和其他的初始化前后添加一些自己的邏輯處理,可以通過實(shí)現(xiàn) BeanPostProcessor 來(lái)完成。
public interface BeanPostProcessor {
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// bean初始化方法調(diào)用前被調(diào)用
return bean;
}
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// bean初始化方法調(diào)用后被調(diào)用
return bean;
}
}
運(yùn)行順序:
----------------Spring IOC容器實(shí)例化Bean ----------------調(diào)用BeanPostProcessor的postProcessBeforeInitialization方法 ----------------調(diào)用bean實(shí)例的初始化方法 ----------------調(diào)用BeanPostProcessor的postProcessAfterInitialization方法
二、BeanFactoryPostProcessor
BeanFactoryPostProcessor 接口與 BeanPostProcessor 接口類似,可以對(duì)bean的定義(配置元數(shù)據(jù))進(jìn)行處理;也就是spring ioc運(yùn)行BeanFactoryPostProcessor 在容器實(shí)例化任何其他的bean之前讀取配置元數(shù)據(jù),并有可能修改它;
如果業(yè)務(wù)需要,可以配置多個(gè)BeanFactoryPostProcessor 的實(shí)現(xiàn)類,通過 ”order” 控制執(zhí)行次序(要實(shí)現(xiàn) Ordered 接口)。
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
public MyBeanFactoryPostProcessor() {
System.out.println("----------------execute MyBeanFactoryPostProcessor constructor");
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
System.out.println("----------------execute MyBeanFactoryPostProcessor postProcessBeanFactory");
}
}
打印輸出:
----------------execute MyBeanFactoryPostProcessor constructor
----------------execute MyBeanFactoryPostProcessor postProcessBeanFactory
postProcessBeanFactory 方法在構(gòu)造函數(shù)方法之后執(zhí)行。
三、InitialingBean和DisposableBean
InitializingBean 接口為bean提供了初始化方法的方式,它只包括afterPropertiesSet 方法,凡是繼承該接口的類,在初始化bean的時(shí)候都會(huì)執(zhí)行該方法。
public interface InitializingBean {
void afterPropertiesSet() throws Exception;
}
DisposableBean 也是一個(gè)接口,提供了一個(gè)唯一的方法destory(),凡是繼承該接口的類,在Bean生命周期結(jié)束前都會(huì)執(zhí)行該方法。
public interface DisposableBean {
void destroy() throws Exception;
}
這里借用網(wǎng)上的一張Bean生命周期的過程圖片:

四、FactoryBean
FactoryBean 是一個(gè)接口,當(dāng)在 IOC 容器中的 Bean 實(shí)現(xiàn)了 FactoryBean 后,通過 getBean(String BeanName) 獲取到的Bean對(duì)象并不是 FactoryBean 的實(shí)現(xiàn)類對(duì)象,而是這個(gè)實(shí)現(xiàn)類中的 getObject() 方法返回的對(duì)象。
public interface FactoryBean<T> {
// 獲取類對(duì)象
@Nullable
T getObject() throws Exception;
// 獲取類類型
@Nullable
Class<?> getObjectType();
// 是否單例
default boolean isSingleton() {
return true;
}
}
五、BeanDefinitionRegistryPostProcessor
BeanDefinitionRegistryPostProcessor 可以完成新的 BeanDefinition 注冊(cè),對(duì)已有 BeanDefinition 進(jìn)行修改等操作。
public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {
/**
* 在Spring的標(biāo)準(zhǔn)初始化完成之后,此時(shí)所有的符合 Spring 規(guī)則的BeanDefinition已經(jīng)全部完成加載,但是還沒有任何一個(gè) Bean 被初始化,
* Spring允許在下一個(gè)post-processing開始處理之前通過此接口添加更多的BeanDefinition
*/
void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;
}
寫一個(gè)類實(shí)現(xiàn) BeanDefinitionRegistryPostProcessor 往容器中手動(dòng)注冊(cè)一個(gè)BeanDefinition。
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
// 創(chuàng)建一個(gè)bean的定義類的對(duì)象
RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(MyMapperFactoryBean.class);
// 將Bean 的定義注冊(cè)到Spring環(huán)境
beanDefinitionRegistry.registerBeanDefinition("myMapperFactoryBean", rootBeanDefinition);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {// bean的名字為key, bean的實(shí)例為value
}
}
MyMapperFactoryBean :
public class MyMapperFactoryBean implements FactoryBean {
@Override
public Object getObject() throws Exception {
// 創(chuàng)建一個(gè)代理對(duì)象
return Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{TestBeanDefRegPostProMapper.class},
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("----------execute:" + method.getName());
Class<?> returnType = method.getReturnType();
return "xxxxxxxxxxxx";
}
});
}
@Override
public Class<?> getObjectType() {
return TestBeanDefRegPostProMapper.class;
}
}
TestBeanDefRegPostProMapper 接口:
public interface TestBeanDefRegPostProMapper {
String exexute();
}
測(cè)試:
@SpringBootApplication
public class SpringbootApplication implements CommandLineRunner {
@Autowired
private TestBeanDefRegPostProMapper testBeanDefRegPostProMapper;
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println(testBeanDefRegPostProMapper.exexute());
}
}
測(cè)試結(jié)果:
----------execute:exexute
xxxxxxxxxxxx
最經(jīng)典的案例就是Mybatis與Spring集成中的 MapperScannerConfigurer 和 MapperFactoryBean
MapperScannerConfigurer :
public class MapperScannerConfigurer implements BeanDefinitionRegistryPostProcessor, InitializingBean, ApplicationContextAware, BeanNameAware {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
if (this.processPropertyPlaceHolders) {
processPropertyPlaceHolders();
}
ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
scanner.setAddToConfig(this.addToConfig);
scanner.setAnnotationClass(this.annotationClass);
scanner.setMarkerInterface(this.markerInterface);
scanner.setSqlSessionFactory(this.sqlSessionFactory);
scanner.setSqlSessionTemplate(this.sqlSessionTemplate);
scanner.setSqlSessionFactoryBeanName(this.sqlSessionFactoryBeanName);
scanner.setSqlSessionTemplateBeanName(this.sqlSessionTemplateBeanName);
scanner.setResourceLoader(this.applicationContext);
scanner.setBeanNameGenerator(this.nameGenerator);
scanner.registerFilters();
// 掃描Mybatis配置MapperScan包,進(jìn)行注冊(cè),將每一個(gè)Mapper接口都注冊(cè)為一個(gè)MapperFactoryBean對(duì)象
scanner.scan(StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
}
}
MapperFactoryBean:
public class MapperFactoryBean<T> extends SqlSessionDaoSupport implements FactoryBean<T> {
@Override
public T getObject() throws Exception {
// 返回一個(gè)代理對(duì)象,用于執(zhí)行sql
return getSqlSession().getMapper(this.mapperInterface);
}
}
六、BeanNameAware、ApplicationContextAware 和 BeanFactoryAware
1、實(shí)現(xiàn) BeanNameAware 接口的 Bean,在 Bean 加載的過程中可以獲取到該 Bean 的 id。
public interface BeanNameAware extends Aware {
void setBeanName(String beanName);
}
2、實(shí)現(xiàn) ApplicationContextAware 接口的 Bean,在 Bean 加載的過程中可以獲取到 Spring的ApplicationContext,ApplicationContext 是 Spring 應(yīng)用上下文,從 ApplicationContext 中可以獲取包括任意的 Bean 在內(nèi)的大量 Spring 容器內(nèi)容和信息。
public interface ApplicationContextAware extends Aware {
void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}
3、實(shí)現(xiàn) BeanFactoryAware 接口的 Bean,在 Bean 加載的過程中可以獲取到加載該 Bean的BeanFactory。
public interface BeanFactoryAware extends Aware {
void setBeanFactory(BeanFactory beanFactory) throws BeansException;
}
到此這篇關(guān)于Spring擴(kuò)展接口知識(shí)總結(jié)的文章就介紹到這了,更多相關(guān)Spring擴(kuò)展接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解常用的Spring Bean擴(kuò)展接口
- Springboot添加支付接口
- SpringBoot集成Spring security JWT實(shí)現(xiàn)接口權(quán)限認(rèn)證
- SpringBoot使用Feign調(diào)用其他服務(wù)接口
- Spring的BeanFactoryPostProcessor接口示例代碼詳解
- Spring Boot接口限流的常用算法及特點(diǎn)
- SpringBoot 防止接口惡意多次請(qǐng)求的操作
- 使用SpringBoot跨系統(tǒng)調(diào)用接口的方案
- Springboot 掃描mapper接口的2種操作
- SpringBoot項(xiàng)目如何把接口參數(shù)中的空白值替換為null值(推薦)
相關(guān)文章
Spring mvc服務(wù)端數(shù)據(jù)校驗(yàn)實(shí)現(xiàn)流程詳解
這篇文章主要介紹了Spring mvc服務(wù)端數(shù)據(jù)校驗(yàn)實(shí)現(xiàn)流程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Java redisTemplate阻塞式處理消息隊(duì)列
用redis中的List可以實(shí)現(xiàn)隊(duì)列,這樣可以用來(lái)做消息處理和任務(wù)調(diào)度的隊(duì)列。因此,本文將主要為大家介紹如何利用redisTemplate處理消息隊(duì)列,感興趣的小伙伴可以了解一下2021-12-12
使用Autowired為什么會(huì)被IDEA警告最佳修改方法
這篇文章主要介紹了使用Autowired為什么會(huì)被IDEA警告,應(yīng)該怎么修改最佳,除了使用@Autowired以外,我們其實(shí)也有幾種好用的方式,使用@Resource替代@Autiwired方法是其中一種,只需要改變一個(gè)注解,這里就不展示了,需要的朋友可以參考下2023-02-02
Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:三角數(shù)字
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:三角數(shù)字,本文直接給出實(shí)現(xiàn)代碼,代碼中包含詳細(xì)注釋,需要的朋友可以參考下2015-06-06
spring boot項(xiàng)目同時(shí)傳遞參數(shù)和文件的多種方式代碼演示
這篇文章主要介紹了spring boot項(xiàng)目同時(shí)傳遞參數(shù)和文件的多種方式,在開發(fā)接口中,遇到了需要同時(shí)接收參數(shù)和文件的情況,可以有多種方式實(shí)現(xiàn)文件+參數(shù)的接收,這里基于spring boot 3 + vue 3 + axios,做一個(gè)簡(jiǎn)單的代碼演示,需要的朋友可以參考下2023-06-06

