Spring - Bean 後處理器



BeanPostProcessor 介面定義了您可以實現的回撥方法,以提供您自己的例項化邏輯、依賴關係解析邏輯等。您還可以透過插入一個或多個 BeanPostProcessor 實現,在 Spring 容器完成 Bean 的例項化、配置和初始化後實現一些自定義邏輯。

您可以配置多個 BeanPostProcessor 介面,並且可以透過設定 BeanPostProcessor 實現的order屬性來控制這些 BeanPostProcessor 介面的執行順序,前提是 BeanPostProcessor 介面實現了Ordered介面。

BeanPostProcessors 對 Bean(或物件)例項進行操作,這意味著 Spring IoC 容器會例項化一個 Bean 例項,然後 BeanPostProcessor 介面執行其工作。

ApplicationContext 會自動檢測任何使用BeanPostProcessor介面實現定義的 Bean,並將這些 Bean 註冊為後處理器,以便容器在建立 Bean 時適當地呼叫它們。

示例

以下示例演示如何在 ApplicationContext 的上下文中編寫、註冊和使用 BeanPostProcessors。

讓我們準備好一個可用的 Eclipse IDE,並按照以下步驟建立一個 Spring 應用程式:

步驟 描述
1 建立一個名為SpringExample的專案,並在建立的專案中的src資料夾下建立一個包com.tutorialspoint
2 Spring HelloWorld 示例章節中所述,使用新增外部JAR選項新增所需的 Spring 庫。
3 com.tutorialspoint包下建立Java類HelloWorldInitHelloWorldMainApp
4 src資料夾下建立Beans配置檔案Beans.xml
5 最後一步是建立所有Java檔案和Bean配置檔案的內容,並執行應用程式,如下所述。

以下是HelloWorld.java檔案的內容:

package com.tutorialspoint;

public class HelloWorld {
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }
   public void getMessage(){
      System.out.println("Your Message : " + message);
   }
   public void init(){
      System.out.println("Bean is going through init.");
   }
   public void destroy(){
      System.out.println("Bean will destroy now.");
   }
}

這是一個實現 BeanPostProcessor 的非常基本的示例,它在初始化任何 Bean 之前和之後列印 Bean 名稱。您可以實現更復雜的邏輯,因為您可以在兩個後處理器方法內部訪問 Bean 物件。

以下是InitHelloWorld.java檔案的內容:

package com.tutorialspoint;

import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.BeansException;

public class InitHelloWorld implements BeanPostProcessor {
   public Object postProcessBeforeInitialization(Object bean, String beanName) 
      throws BeansException {
      
      System.out.println("BeforeInitialization : " + beanName);
      return bean;  // you can return any other object as well
   }
   public Object postProcessAfterInitialization(Object bean, String beanName) 
      throws BeansException {
      
      System.out.println("AfterInitialization : " + beanName);
      return bean;  // you can return any other object as well
   }
}

以下是MainApp.java檔案的內容。在這裡,您需要註冊一個在AbstractApplicationContext類上宣告的關閉鉤子registerShutdownHook()方法。這將確保優雅地關閉並呼叫相關的銷燬方法。

package com.tutorialspoint;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
   public static void main(String[] args) {
      AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
      obj.getMessage();
      context.registerShutdownHook();
   }
}

以下是init和destroy方法所需的配置檔案Beans.xml

<?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-3.0.xsd">

   <bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld"
      init-method = "init" destroy-method = "destroy">
      <property name = "message" value = "Hello World!"/>
   </bean>

   <bean class = "com.tutorialspoint.InitHelloWorld" />

</beans>

建立原始碼和 Bean 配置檔案後,讓我們執行應用程式。如果您的應用程式一切正常,它將列印以下訊息:

BeforeInitialization : helloWorld
Bean is going through init.
AfterInitialization : helloWorld
Your Message : Hello World!
Bean will destroy now.
廣告