Spring擴(kuò)展接口知識總結(jié)
一、BeanPostProcessor
BeanPostProcessor 接口是 Spring 提供的眾多接口之一,他的作用主要是如果需要在Spring 容器完成 Bean 的實(shí)例化、配置和其他的初始化前后添加一些自己的邏輯處理,可以通過實(shí)現(xiàn) BeanPostProcessor 來完成。
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 接口類似,可以對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í)候都會執(zhí)行該方法。
public interface InitializingBean { void afterPropertiesSet() throws Exception; }
DisposableBean 也是一個(gè)接口,提供了一個(gè)唯一的方法destory(),凡是繼承該接口的類,在Bean生命周期結(jié)束前都會執(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對象并不是 FactoryBean 的實(shí)現(xiàn)類對象,而是這個(gè)實(shí)現(xiàn)類中的 getObject() 方法返回的對象。
public interface FactoryBean<T> { // 獲取類對象 @Nullable T getObject() throws Exception; // 獲取類類型 @Nullable Class<?> getObjectType(); // 是否單例 default boolean isSingleton() { return true; } }
五、BeanDefinitionRegistryPostProcessor
BeanDefinitionRegistryPostProcessor 可以完成新的 BeanDefinition 注冊,對已有 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 往容器中手動注冊一個(gè)BeanDefinition。
@Component public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor { @Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException { // 創(chuàng)建一個(gè)bean的定義類的對象 RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(MyMapperFactoryBean.class); // 將Bean 的定義注冊到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è)代理對象 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(); }
測試:
@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()); } }
測試結(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)行注冊,將每一個(gè)Mapper接口都注冊為一個(gè)MapperFactoryBean對象 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è)代理對象,用于執(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ò)展接口知識總結(jié)的文章就介紹到這了,更多相關(guān)Spring擴(kuò)展接口內(nèi)容請搜索腳本之家以前的文章或繼續(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 防止接口惡意多次請求的操作
- 使用SpringBoot跨系統(tǒng)調(diào)用接口的方案
- Springboot 掃描mapper接口的2種操作
- SpringBoot項(xiàng)目如何把接口參數(shù)中的空白值替換為null值(推薦)
相關(guān)文章
淺談Java數(shù)值類型的轉(zhuǎn)換與強(qiáng)制轉(zhuǎn)換
這篇文章主要介紹了Java數(shù)值類型的轉(zhuǎn)換與強(qiáng)制轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Java使用JSqlParser解析SQL語句應(yīng)用場景
JSqlParser是一個(gè)功能全面的Java庫,用于解析SQL語句,支持多種SQL方言,它可以輕松集成到Java項(xiàng)目中,并提供靈活的操作方式,本文介紹Java使用JSqlParser解析SQL語句總結(jié),感興趣的朋友一起看看吧2024-09-09SpringBoot?ScheduledTaskRegistrar解決動態(tài)定時(shí)任務(wù)思路詳解
本文將從問題出發(fā),詳細(xì)介紹ScheduledTaskRegistrar類是如何解決動態(tài)調(diào)整定時(shí)任務(wù)的思路,并給出關(guān)鍵的代碼示例,幫助大家快速地上手學(xué)習(xí)2023-02-02Springboot使用@WebListener?作為web監(jiān)聽器的過程解析
這篇文章主要介紹了Springboot使用@WebListener作為web監(jiān)聽器的過程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08springboot日志文件名稱叫l(wèi)ogback-spring.xml的原因解析
這篇文章主要介紹了springboot日志文件名稱為什么叫l(wèi)ogback-spring.xml,本文給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08