BeanPostProcessor接口 bean的后置处理器

实现功能主要是 可以在bean初始化之前和之后做增强处理。

自定义MyBeanProcessor实现BeanPostProcessor接口,重写里面的postProcessBeforeInitialization和postProcessAfterInitialization方法,这里两个方法的主要是用来bean初始化(init方法)之前和之后做增强处理。

/**
 * @author liujian
 * @date 2018/1/9
 */
public class MyBeanProcessor implements BeanPostProcessor{
    @Override
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        System.out.println("初始化之前做处理");
        //bean初始化之前做处理的方法
        if(s.equals("test")){
           HelloWorld helloWorld=(HelloWorld)o;
           helloWorld.setMessage("helloworld 处理后");
           return helloWorld;
        }

        return o;
    }

    @Override
    public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
        System.out.println("初始化之后做处理");
        return o;
    }
}

将自定义的MyBeanProcessor 添加到spring 容器中

<bean id="myBeanProcessor" class="com.spring.demo.MyBeanProcessor">

自定义一个实体类HelloWorld,并添加到spring容器中

/**
 * @author liujian
 * @date 2018/1/8
 */
public class HelloWorld {

    private String message;

    public void getMessage(){
        System.out.println("message is "+message);
    }

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

    public HelloWorld() {
    }
    //初始化方法
    public void init(){
        System.out.println("bean 初始化方法");
    }
    //销毁方法
    public void destory(){
        System.out.println("bean 销毁方法");
    }
}

bean.xml文件中 配置helloworld的bean

<?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">
    <!--通过构造器方法创建bean,定义初始化方法initmethod 和销毁方法-->
    <bean id="test" class="com.spring.demo.HelloWorld" init-method="init" destroy-method="destory">
        <property name="message" value="hello world 1111"></property>
     </bean>
    <!--自定义bean的后置处理器-->
    <bean class="com.spring.demo.MyBeanProcessor"/>
</beans>

测试工具类:

/**
 * @author liujian
 * @date 2018/1/8
 */
public class MainApp {

    public static void main(String []args){
    /*    ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
        HelloWorld obj=(HelloWorld) context.getBean("helloWorld");
        obj.getMessage();
        XmlBeanFactory xmlBeanFactory=new XmlBeanFactory(new ClassPathResource("beans.xml"));
        HelloWorld obj1=(HelloWorld) xmlBeanFactory.getBean("helloWorld");
        obj1.getMessage();*/
        AbstractApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
        HelloWorld objA=(HelloWorld) context.getBean("test");
        objA.getMessage();
        context.registerShutdownHook();
    }
}

输出结果:

spring中Bean后置处理器实现总结的更多相关文章

  1. Spring点滴五:Spring中的后置处理器BeanPostProcessor讲解

    BeanPostProcessor接口作用: 如果我们想在Spring容器中完成bean实例化.配置以及其他初始化方法前后要添加一些自己逻辑处理.我们需要定义一个或多个BeanPostProcesso ...

  2. spring学习四:Spring中的后置处理器BeanPostProcessor

    BeanPostProcessor接口作用: 如果我们想在Spring容器中完成bean实例化.配置以及其他初始化方法前后要添加一些自己逻辑处理.我们需要定义一个或多个BeanPostProcesso ...

  3. Spring中的后置处理器BeanPostProcessor讲解

    Spring中提供了很多PostProcessor供开发者进行拓展,例如:BeanPostProcessor.BeanFactoryPostProcessor.BeanValidationPostPr ...

  4. [原创]java WEB学习笔记101:Spring学习---Spring Bean配置:IOC容器中bean的声明周期,Bean 后置处理器

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. Spring Bean后置处理器

    本例子源于:W3CSchool,在此作记录 Bean 后置处理器允许在调用初始化方法前后对 Bean 进行额外的处理. BeanPostProcessor 接口定义回调方法,你可以实现该方法来提供自己 ...

  6. Spring Bean 后置处理器

    Bean 后置处理器允许在调用初始化方法前后对 Bean 进行额外的处理. BeanPostProcessor 接口定义回调方法,你可以实现该方法来提供自己的实例化逻辑,依赖解析逻辑等. 你也可以在 ...

  7. Spring之BeanPostProcessor(后置处理器)介绍

      为了弄清楚Spring框架,我们需要分别弄清楚相关核心接口的作用,本文来介绍下BeanPostProcessor接口 BeanPostProcessor   该接口我们也叫后置处理器,作用是在Be ...

  8. Spring 如何保证后置处理器的执行顺序 - OrderComparator

    Spring 如何保证后置处理器的执行顺序 - OrderComparator Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.htm ...

  9. Spring-Spring Bean后置处理器

    Spring Bean后置处理器 BeanPostProcessor接口定义回调方法,你可以实现该方法来提供自己的实例化逻辑,依赖解析逻辑等.你也可以在Spring容器通过插入一个或多个BeanPos ...

随机推荐

  1. 函数的作用域与this指向 --- 性能篇

    紧接上一篇博文:js函数的作用域与this指向 先来说说this的作用于链,this后的属性或者方法在使用时是先从本实例中查找,如果找到就先返回,如果没找到就接着向上从原型链中查找,如果有多重继承关系 ...

  2. 7、创建ROS msg和srv

    一.msg和srv介绍 msg: msg文件使用简单的文本格式声明一个ROS message的各个域. 仅须要创建一个msg文件,就能够使用它来生成不同语言的message定义代码. srv:srv文 ...

  3. linux0.11学习笔记(1)

    公布软件包包括内容: bootimage.Z - 具有美国键盘代码的压缩启动映像文件: rootimage.Z - 以1200kB 压缩的根文件系统映像文件: linux-0.11.tar.Z- 内核 ...

  4. hdu 5635 LCP Array(BC第一题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5635 LCP Array Time Limit: 4000/2000 MS (Java/Others) ...

  5. 微信小程序的Web API接口设计及常见接口实现

    微信小程序给我们提供了一个很好的开发平台,可以用于展现各种数据和实现丰富的功能,通过小程序的请求Web API 平台获取JSON数据后,可以在小程序界面上进行数据的动态展示.在数据的关键 一环中,我们 ...

  6. ABP入门系列(9)——权限管理

    ABP入门系列目录--学习Abp框架之实操演练 源码路径:Github-LearningMpaAbp 完成了简单的增删改查和分页功能,是不是觉得少了点什么? 是的,少了权限管理.既然涉及到了权限,那我 ...

  7. String、StringBuilder和StringBuffer类

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  8. JPA 单向一对多关联关系

    映射单向一对多的关联关系 1.首先在一的一端加入多的一端的实体类集合 2.使用@OneToMany 来映射一对多的关联关系3.使用@JoinColumn 来映射外键列的名称4.可以使用@OneToMa ...

  9. dataTables 使用整理

    官方网站:http://www.datatables.net/ 简介:DataTables是一个jQuery的表格插件.这是一个高度灵活的工具,依据的基础逐步增强,这将增加先进的互动控制,支持任何HT ...

  10. 【功能代码】---5 JS通过事件隐藏显示元素

    JS通过事件隐藏显示元素 在开发中,很多时候我们需要点击事件,才显示隐藏元素.那如何做到页面刚开始就把标签隐藏. 有两种方法: (1) display:none    <div id=" ...