1. 程式人生 > >Spring Bean前置後置處理器的使用

Spring Bean前置後置處理器的使用

Spirng中BeanPostProcessor和InstantiationAwareBeanPostProcessorAdapter兩個介面都可以實現對bean前置後置處理的效果,那這次先講解一下BeanPostProcessor處理器的使用

先看一下BeanPostProcessor介面的原始碼,它定義了兩個方法,一個在bean初始化之前,一個在bean初始化之後

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;
    }
}

下面,我們來實現這個類,測試一下Spring中的前置後置處理器吧

首先是pom.xml,增加Spring相關的依賴

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
​
  <groupId>com.myspring</groupId>
  <artifactId>myspring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
​
  <name>myspring</name>
  <url>http://maven.apache.org</url>
​
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
​
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- Spring 5.0 核心工具包 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.0.7.RELEASE</version>
    </dependency>
    <!-- Spring 5.0 Bean管理工具包 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.0.7.RELEASE</version>
    </dependency>
    <!-- Spring 5.0 context管理工具包 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.7.RELEASE</version>
    </dependency>
    <!-- Spring 5.0 aop支援包 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>5.0.7.RELEASE</version>
    </dependency>
  </dependencies>
</project>

定義一個測試介面:

public interface BaseService {
    String doSomething();
    String eat();
}
定義介面實現類:
public class ISomeService implements BaseService {
​
    public String doSomething() {
        // 增強效果:返回內容全部大寫
        return "Hello i am kxm";
    }
    public String eat() {
        return "eat food";
    }
}

實現BeanPostProcessor介面

public class MyBeanPostProcessor implements BeanPostProcessor  {
    // 前置處理器
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        Class beanClass = bean.getClass();
        if (beanClass == ISomeService.class) {
            System.out.println("bean 物件初始化之前······");
        }
        return bean;
    }
    
    // 後置處理器 --- 此處具體的實現用的是Java中的動態代理
    public Object postProcessAfterInitialization(final Object beanInstance, String beanName) throws BeansException {
        // 為當前 bean 物件註冊監控代理物件,負責增強 bean 物件方法的能力
        Class beanClass = beanInstance.getClass();
        if (beanClass == ISomeService.class) {
            Object proxy = Proxy.newProxyInstance(beanInstance.getClass().getClassLoader(),beanInstance.getClass().getInterfaces(), new InvocationHandler() {
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("ISomeService 中的 doSome() 被攔截了···");
                    String result = (String) method.invoke(beanInstance, args);
                    return result.toUpperCase(); 
                }
            });     
            return proxy;
        }
        return beanInstance;
    }
}

Spring的配置檔案如下:

<!-- 註冊 bean:被監控的實現類 -->
<bean id="iSomeService" class="com.my.spring.beanprocessor.ISomeService"></bean>
<!-- 註冊代理實現類 -->
<bean class="com.my.spring.beanprocessor.MyBeanPostProcessor"></bean>
測試類如下:
public class TestBeanPostProcessor {
​
    public static void main(String[] args) {
        /**
         * BeanPostProcessor 前置後置處理器
         */
        ApplicationContext factory = new ClassPathXmlApplicationContext("spring_config.xml");
        BaseService serviceObj = (BaseService) factory.getBean("iSomeService");
        System.out.println(serviceObj.doSomething());
    }
}

測試結果截圖:

可以觀察到,我們明明在程式碼中對於doSomething方法定義的是小寫,但是通過後置處理器,攔截了原本的方法,而是通過動態代理的方式把方法的結果進行了一定程度的改變,這就是Spring中的前置後置處理器----BeanPostProcessor