Spring学习笔记4

BeanPostProcessor接口定义回调方法,可以实现该方法来提供自己的实例化逻辑,依赖解析逻辑等。

也可以在Spring容器通过插入一个或多个BeanPostProcessor的实现来完成实例化,配置和初始化一个bean之后实现一些自定义逻辑回调方法。

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

ApplicationContext会自动检测由BeanPostProcessor接口的实现定义的bean,注册这些bean为后置处理器,然后通过在容器中创建bean,在适当的时候调用它。

示例:

public class HelloWorld {
    private String message;

    public void getMessage() {
        System.out.println("Your Message : " + message);
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void init() {
        System.out.println("Bean is going through init.");
    }

    public void destroy() {
        System.out.println("Bean will destroy now.");
    }

}
HelloWorld.java

这是实现BeanPostProcessor的非常简单的例子,它在任何bean的书时候的之前和之后输入该bean的名称。

你可以在初始化bean的之前和之后实现更复杂的逻辑,因为你有两个访问内置bean对象的后置处理程序的方法。

 1 import org.springframework.beans.BeansException;
 2 import org.springframework.beans.factory.config.BeanPostProcessor;
 3 
 4 public class InitHelloWorld implements BeanPostProcessor {
 5 
 6     @Override
 7     public Object postProcessAfterInitialization(Object bean, String beanName)
 8             throws BeansException {
 9         System.out.println("After Initialization: " + beanName);
10         return bean;
11     }
12 
13     @Override
14     public Object postProcessBeforeInitialization(Object bean, String beanName)
15             throws BeansException {
16         System.out.println("Before Initialization: " + beanName);
17         return bean;
18     }
19     
20 }
InitHelloWorld.java

MainApp.java文件中,需要注册一个在AbstractApplicationContext类中声明的关闭hook的registerShutdownHook()方法。它将确保正常关闭,并且调用相关的destroy()方法。

 1 import org.springframework.context.support.AbstractApplicationContext;
 2 import org.springframework.context.support.ClassPathXmlApplicationContext;
 3 
 4 public class MainApp {
 5     public static void main(String[] args) {
 6         AbstractApplicationContext context = new ClassPathXmlApplicationContext(
 7                 "beans.xml");
 8         HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
 9         
10         obj.getMessage();
11         context.registerShutdownHook();
12     }
13 }
MainApp.java
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd">
 6     
 7     <bean id="helloWorld" class="com.microyum.HelloWorld" init-method="init" destroy-method="destroy">
 8         <property name="message" value="Hello World" />
 9     </bean>
10     
11     <bean class="com.microyum.InitHelloWorld" />
12 </beans>
bean.xml

 执行结果:

Before Initialization: helloWorld
Bean is going through init.
After Initialization: helloWorld
Your Message : Hello World
Bean will destroy now.

原文地址:https://www.cnblogs.com/microyum/p/6893175.html