欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring擴(kuò)展接口知識總結(jié)

 更新時(shí)間:2021年05月25日 16:22:04   作者:普通人zzz~  
今天帶大家學(xué)習(xí)Java Spring的相關(guān)知識,文中對Spring擴(kuò)展接口作了非常詳細(xì)的介紹及代碼示例,對正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下

一、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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中的Semaphore信號量深入解析

    Java中的Semaphore信號量深入解析

    這篇文章主要介紹了Java中的Semaphore信號量深入解析,Semaphore是Java里面另外一個(gè)基本的并發(fā)工具包類,主要的的作用是用來保護(hù)共享資源的訪問的,也就是僅僅允許一定數(shù)量的線程訪問共享資源,需要的朋友可以參考下
    2023-11-11
  • 淺談Java數(shù)值類型的轉(zhuǎn)換與強(qiáng)制轉(zhuǎ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-04
  • Java使用JSqlParser解析SQL語句應(yīng)用場景

    Java使用JSqlParser解析SQL語句應(yīng)用場景

    JSqlParser是一個(gè)功能全面的Java庫,用于解析SQL語句,支持多種SQL方言,它可以輕松集成到Java項(xiàng)目中,并提供靈活的操作方式,本文介紹Java使用JSqlParser解析SQL語句總結(jié),感興趣的朋友一起看看吧
    2024-09-09
  • SpringBoot?ScheduledTaskRegistrar解決動態(tài)定時(shí)任務(wù)思路詳解

    SpringBoot?ScheduledTaskRegistrar解決動態(tài)定時(shí)任務(wù)思路詳解

    本文將從問題出發(fā),詳細(xì)介紹ScheduledTaskRegistrar類是如何解決動態(tài)調(diào)整定時(shí)任務(wù)的思路,并給出關(guān)鍵的代碼示例,幫助大家快速地上手學(xué)習(xí)
    2023-02-02
  • Java?web實(shí)現(xiàn)購物車案例

    Java?web實(shí)現(xiàn)購物車案例

    這篇文章主要為大家詳細(xì)介紹了Java?web實(shí)現(xiàn)購物車案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Springboot使用@WebListener?作為web監(jiān)聽器的過程解析

    Springboot使用@WebListener?作為web監(jiān)聽器的過程解析

    這篇文章主要介紹了Springboot使用@WebListener作為web監(jiān)聽器的過程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • springboot jpa 延遲加載問題的2種解決

    springboot jpa 延遲加載問題的2種解決

    這篇文章主要介紹了springboot jpa 延遲加載問題的2種解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • springboot日志文件名稱叫l(wèi)ogback-spring.xml的原因解析

    springboot日志文件名稱叫l(wèi)ogback-spring.xml的原因解析

    這篇文章主要介紹了springboot日志文件名稱為什么叫l(wèi)ogback-spring.xml,本文給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • 關(guān)于aop切面 注解、參數(shù)如何獲取

    關(guān)于aop切面 注解、參數(shù)如何獲取

    這篇文章主要介紹了關(guān)于aop切面 注解、參數(shù)如何獲取,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教。
    2022-01-01
  • 淺談Java字符串比較的三種方法

    淺談Java字符串比較的三種方法

    這篇文章主要介紹了淺談Java字符串比較的三種方法,字符串比較是常見的操作,包括比較相等、比較大小、比較前綴和后綴串等,需要的朋友可以參考下
    2023-04-04

最新評論