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

Spring?BeanPostProcessor后處理器源碼解析

 更新時間:2023年09月25日 15:13:16   作者:保持敬畏  
這篇文章主要介紹了Spring?BeanPostProcessor后處理器源碼解析,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧

Spring后處理器-BeanPostProcessor

  • Bean被實例化后,到最終緩存到名為singletonObjects單例池之前,中間會經(jīng)過bean的初始化過程((該后處理器的執(zhí)行時機)),例如:屬性的填充、初始化方法init的執(zhí)行等,其中有一個對外拓展的點BeanPostProcessor,我們稱之為bean后處理器。與上文bean工廠后處理器相似,它也是一個接口,實現(xiàn)了該接口并被容器管理的BeanPostProcessor(即在配置文件中對其進行配置),會在流程節(jié)點上被Spring自動調(diào)用。
  • BeanPostProcessor接口代碼如下
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package org.springframework.beans.factory.config;
import org.springframework.beans.BeansException;
import org.springframework.lang.Nullable;
public interface BeanPostProcessor {
    @Nullable
    default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
    @Nullable
    default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}

創(chuàng)建實現(xiàn)該接口(BeanPsotProcessor)的類,要在配置文件中進行管理

快捷鍵 ctrl + insert 重寫接口方法

package com.example.PostProcessor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println(beanName + ":postProcessBeforeInitialization");
        return bean;
    }
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println(beanName + ":postProcessAfterInitialization");
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
}

測試類代碼

package com.example.Test;
import com.example.Service.Impl.UserServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestApplicationContext {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        System.out.println(context.getBean(UserServiceImpl.class));
    }
}

運行結(jié)果如下

展示了該后處理器的執(zhí)行時機 

Before和After執(zhí)行時機

在Bean實例化過程可以配置相關對于bean對象的操作方法,具體間往期文章:Bean的配置

注冊為bean的類

package com.example.Service.Impl;
import com.example.DAO.UserDAO;
import com.example.Service.UserService;
import org.springframework.beans.factory.InitializingBean;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class UserServiceImpl implements UserService, InitializingBean {
    // todo 無參構(gòu)造方法
    public UserServiceImpl() {
        System.out.println("UserServiceImpl實例化");
    }
    // todo 自定義初始化方法
    public void init() {
        System.out.println("自定義初始化方法init()");
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("屬性設置之后執(zhí)行afterPropertiesSet()");
    }
}

實現(xiàn)bean后處理器的類

package com.example.PostProcessor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println(beanName + ":postProcessBeforeInitialization");
        return bean;
    }
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println(beanName + ":postProcessAfterInitialization");
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <bean class="com.example.PostProcessor.MyBeanPostProcessor"></bean>
    <bean id="userService" class="com.example.Service.Impl.UserServiceImpl" init-method="init">
</beans>

測試類

package com.example.Test;
import com.example.Service.Impl.UserServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestApplicationContext {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        System.out.println(context.getBean(UserServiceImpl.class));
    }
}

運行結(jié)果

小結(jié)

  從上述運行結(jié)果來看,首先完成bean對象的創(chuàng)建,然后執(zhí)行后處理器中的before方法,然后執(zhí)行屬性設置之后的方法,然后執(zhí)行自定義的初始化方法,最后執(zhí)行后處理器的after方法

案例

  • 對Bean方法進行執(zhí)行時間日志增強
  • 要求
    • Bean的方法執(zhí)行之前控制臺打印當前時間
    • Bean的方法執(zhí)行之后控制臺打印當前時間
  • 分析
    • 對方法進行增強主要就是代理設計模式和包裝設計模式
    • 由于Bean方法不確定,所以使用動態(tài)代理在運行期間執(zhí)行增強操作
    • 在Bean實例創(chuàng)建完畢之后,進入到單例之前,使用Proxy真實的目標bean
  • 具體代碼如下

代理類(實現(xiàn)了bean后處理接口)

package com.example.PostProcessor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Date;
public class TimeLogBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        // todo 使用動態(tài)代理對目標bean進行增強,返回proxy對象,進而存儲到單例池singletonObjects中
        Object beanProxy = Proxy.newProxyInstance(bean.getClass().getClassLoader(), bean.getClass().getInterfaces(), (InvocationHandler) (proxy, method, args) -> {
            // 輸出開始時間
            System.out.println("方法" + method.getName() + "開始執(zhí)行時間" + new Date());
            // 執(zhí)行目標方法
            Object rs = method.invoke(bean, args);
            // 輸出結(jié)束時間
            System.out.println("方法" + method.getName() + "結(jié)束執(zhí)行時間" + new Date());
            return rs;
        });
        return beanProxy;// 將增強的bean存入單例池中
    }
}
 

bean對象對應的類

package com.example.Service.Impl;
import com.example.Service.UserService;
public class UserServiceImpl implements UserService {
    public void show() {
        System.out.println("show......");
    }
}

接口類

package com.example.Service;
public interface UserService {
    public void show();
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean class="com.example.PostProcessor.TimeLogBeanPostProcessor"></bean>
    <bean id="userService" class="com.example.Service.Impl.UserServiceImpl">
    </bean>
</beans>

測試類

package com.example.Test;
import com.example.Service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestApplicationContext {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        UserService UserServiceBean = (UserService) context.getBean(UserService.class);
        UserServiceBean.show();
    }
}
 

運行結(jié)果

Bean實例化基本流程圖

首先通過Reader讀取配置文件,解析bean標簽,然后將每個bean標簽變成beanDefinition對象存儲到beanDefinitionMap中,然后經(jīng)過所有的BeanFactoryPostProcessor(bean工廠后處理器),再從Map中取出每個beanDefiniton對象,通過反射變成Object對象,創(chuàng)建完對象后,經(jīng)過beanPoatProcessor中的before方法和after方法(之間還存在bean中的init方法,后面會講述),最終存入單例池中。 

到此這篇關于Spring BeanPostProcessor后處理器的文章就介紹到這了,更多相關Spring BeanPostProcessor后處理器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java Socket編程實例(五)- NIO UDP實踐

    Java Socket編程實例(五)- NIO UDP實踐

    這篇文章主要講解Java Socket編程中NIO UDP的實例,希望能給大家做一個參考。
    2016-06-06
  • Java?Optional用法面試題精講

    Java?Optional用法面試題精講

    這篇文章主要為大家介紹了Java?Optional用法面試題精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • Token安全存儲的幾種方式小結(jié)

    Token安全存儲的幾種方式小結(jié)

    在現(xiàn)代 Web 應用中,身份認證與授權(quán)是確保系統(tǒng)安全性的重要部分,Token被廣泛應用,作為實現(xiàn)身份認證的主要方式,然而,如何安全地存儲這些 Token,是每個開發(fā)者在構(gòu)建前端應用時必須考慮的問題,本文將深入探討Token安全存儲的幾種方式,需要的朋友可以參考下
    2025-04-04
  • 基于springboot處理date參數(shù)過程解析

    基于springboot處理date參數(shù)過程解析

    這篇文章主要介紹了基于springboot處理date參數(shù)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • IntelliJ IDEA 如何徹底刪除項目的步驟

    IntelliJ IDEA 如何徹底刪除項目的步驟

    本篇文章主要介紹了IntelliJ IDEA 如何徹底刪除項目的步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • java 二進制數(shù)據(jù)與16進制字符串相互轉(zhuǎn)化方法

    java 二進制數(shù)據(jù)與16進制字符串相互轉(zhuǎn)化方法

    今天小編就為大家分享一篇java 二進制數(shù)據(jù)與16進制字符串相互轉(zhuǎn)化方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Spring中XML schema擴展機制的深入講解

    Spring中XML schema擴展機制的深入講解

    這篇文章主要給大家介紹了關于Spring中XML schema擴展機制的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-09-09
  • Java批量操作如何提升ORM框架的批處理性能

    Java批量操作如何提升ORM框架的批處理性能

    本文介紹的批量插入、更新、刪除和讀取優(yōu)化技術(shù),以及性能監(jiān)控與調(diào)優(yōu)方法,為開發(fā)者提供了全面的批處理性能優(yōu)化思路,感興趣的朋友一起看看吧
    2025-05-05
  • springboot+Oauth2實現(xiàn)自定義AuthenticationManager和認證path

    springboot+Oauth2實現(xiàn)自定義AuthenticationManager和認證path

    本篇文章主要介紹了springboot+Oauth2實現(xiàn)自定義AuthenticationManager和認證path,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Java詳解實現(xiàn)多線程的四種方式總結(jié)

    Java詳解實現(xiàn)多線程的四種方式總結(jié)

    哈哈!經(jīng)過一個階段的學習,Java基礎知識學習終于到多線程了!Java多線程以及后面互斥鎖的概念都是Java基礎學習的難點,所以我做了一個總結(jié),希望對大家也有幫助
    2022-07-07

最新評論