zoukankan      html  css  js  c++  java
  • Spring Bean 后置处理器

    Bean 后置处理器允许在调用初始化方法前后对 Bean 进行额外的处理。

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

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

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

    BeanPostProcessor 可以对 bean(或对象)实例进行操作,这意味着 Spring IoC 容器实例化一个 bean 实例,然后 BeanPostProcessor 接口进行它们的工作。

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

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

    先建一个Spring项目,参考在IDEA中使用Spring写一个HelloWorld

    这里是 HelloWorld.java 文件的内容:

    package hello;
    
    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.");
        }
    }
    

    在包hello 下新建一个InitHelloWorld.java 文件,内容如下:

    package hello;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    import org.springframework.beans.BeansException;
    
    public class InitHelloWorld implements BeanPostProcessor{
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("BeforeInitialization: " + beanName);
            return bean;
        }
    
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("AfterInitialization: "+ beanName);
            return bean;
        }
    }
    

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

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

    package hello;
    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:

    <?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"
           default-init-method="init"
           default-destroy-method="destroy">
    
        <bean id="helloWorld" class="hello.HelloWorld" >
            <property name="message" value="你好,Spring!"/>
        </bean>
    
        <bean class="hello.InitHelloWorld"/>
    
    </beans>
    

    运行MainApp.java文件,结果如下:

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

    每天学习一点点,每天进步一点点。

  • 相关阅读:
    【练习】flushback基于时间的闪回查询
    【练习】审计
    【练习】新建虚拟机
    织梦安全防护教程首页被挟持、被串改、被挂马、被入侵之后如何解决?
    织梦手机站下一篇变上一篇而且还出错Request Error!
    织梦图集上传错误提示ERROR: Upload Error!
    织梦炒鸡简单3部曲修改文档标题长度限制
    织梦dede:arclist和dede:list输出每个文章的内容有多少页
    织梦后台管理模板无法正常显示模板文件列表解决方法
    织梦列表页多种属性排序[ajax]-支持select方式和降序升序切换
  • 原文地址:https://www.cnblogs.com/youcoding/p/12742653.html
Copyright © 2011-2022 走看看