📜  Spring-Bean后处理器

📅  最后修改于: 2020-11-11 07:00:30             🧑  作者: Mango


BeanPostProcessor接口定义了回调方法,您可以实现这些回调方法以提供自己的实例化逻辑,依赖关系解析逻辑等。您还可以在Spring容器通过插入一个或多个容器完成实例化,配置和初始化Bean之后,实现一些自定义逻辑。 BeanPostProcessor实现。

您可以配置多个BeanPostProcessor接口,并且可以通过设置BeanPostProcessor实现Ordered接口的order属性来控制这些BeanPostProcessor接口的执行顺序

BeanPostProcessor在bean(或对象)实例上运行,这意味着Spring IoC容器实例化bean实例,然后BeanPostProcessor接口完成其工作。

ApplicationContext自动检测由BeanPostProcessor接口的实现定义的任何Bean,并将这些Bean注册为后处理器,然后在创建Bean时由容器进行适当调用。

下面的示例演示如何在ApplicationContext的上下文中编写,注册和使用BeanPostProcessors。

让我们拥有一个运行良好的Eclipse IDE,并采取以下步骤来创建一个Spring应用程序:

Steps Description
1 Create a project with a name SpringExample and create a package com.tutorialspoint under the src folder in the created project.
2 Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3 Create Java classes HelloWorld, InitHelloWorld and MainApp under the com.tutorialspoint package.
4 Create Beans configuration file Beans.xml under the src folder.
5 The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.

这是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之前和之后实现更复杂的逻辑,因为您可以在两个后处理器方法中访问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()方法。这将确保正常关机并调用相关的destroy方法。

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

以下是初始化和销毁方法所需的配置文件Beans.xml-





   
      
   

   


创建完源和Bean配置文件后,让我们运行该应用程序。如果您的应用程序一切正常,它将打印以下消息:

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