【spring源码学习】Spring的IOC容器之BeanPostProcessor接口学习
一:含义作用
==>BeanPostProcessor接口是众多Spring提供给开发者的bean生命周期内自定义逻辑拓展接口中的一个
二:接口定义
package org.springframework.beans.factory.config;
import org.springframework.beans.BeansException;
public interface BeanPostProcessor {
/**
*IOC容器中的bean实例化之前执行
*/
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
/**
*IOC容器中的bean实例化之后执行
*/
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}
三:自定义实例(在ioc实例化的时候执行,并打印内容。xml配置bean,或用注解注释,即可生效)
package com.mobile.thinks.manages.study; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component; @Component
public class MyBeanPostProcessor implements BeanPostProcessor{ /**
* IOC容器中bean在被实例化之前执行该方法
*/
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
Class<?> cls=bean.getClass();
System.out.println("sxf 【自定义实例化之前】postProcessBeforeInitialization类路径==>"+cls);
System.out.println("sxf 【自定义实例化之前】postProcessBeforeInitialization初始化对象的名字==>"+beanName);
return bean;
} /**
* IOC容器中bean在被实例化之后执行该方法
*/
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
Class<?> cls=bean.getClass();
System.out.println("sxf 【自定义实例化之后】postProcessBeforeInitialization类路径==>"+cls);
System.out.println("sxf 【自定义实例化之后】postProcessBeforeInitialization初始化对象的名字==>"+beanName);
return bean;
} }
四:spring内部例子
【1】org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor 类
==>在postProcessAfterInitialization 方法中,将bean中的方法标注有@Async注解的bean,不返回IOC真实的实例化对象,而是返回一个代理对象(动态代理)。org.springframework.scheduling.annotation.AsyncAnnotationAdvisor作为代理对象的执行增强(切面+增加)
【2】org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator 类
==>在postProcessAfterInitialization 方法中对bean进行动态代理。
【spring源码学习】Spring的IOC容器之BeanPostProcessor接口学习的更多相关文章
- 【spring源码学习】spring的IOC容器之BeanFactoryPostProcessor接口学习
[一]org.springframework.beans.factory.config.BeanFactoryPostProcessor接口==>该接口实现方法的执行时机:该接口void pos ...
- Spring学习之——手写Spring源码V2.0(实现IOC、D、MVC、AOP)
前言 在上一篇<Spring学习之——手写Spring源码(V1.0)>中,我实现了一个Mini版本的Spring框架,在这几天,博主又看了不少关于Spring源码解析的视频,受益匪浅,也 ...
- Spring源码:Spring IoC容器加载过程(2)
Spring源码版本:4.3.23.RELEASE 一.加载XML配置 通过XML配置创建Spring,创建入口是使用org.springframework.context.support.Class ...
- Spring源码:Spring IoC容器加载过程(1)
Spring源码版本:4.3.23.RELEASE 一.加载过程概览 Spring容器加载过程可以在org.springframework.context.support.AbstractApplic ...
- spring源码解析——spring源码导入eclipse
一.前言 众所周知,spring的强大之处.几乎所有的企业级开发中,都使用了spring了.在日常的开发中,我们是否只知道spring的配置,以及简单的使用场景.对其实现的代码没有进行深入的了 ...
- spring源码系列(一)sring源码编译 spring源码下载 spring源码阅读
想对spring框架进行深入的学习一下,看看源代码,提升和沉淀下自己,工欲善其事必先利其器,还是先搭建环境吧. 环境搭建 sping源码之前是svn管理,现在已经迁移到了github中了,新版本基于g ...
- 【Spring 源码】Spring 加载资源并装配对象的过程(XmlBeanDefinitionReader)
Spring 加载资源并装配对象过程 在Spring中对XML配置文件的解析从3.1版本开始不再推荐使用XmlBeanFactory而是使用XmlBeanDefinitionReader. Class ...
- Spring源码阅读-spring启动
web.xml web.xml中的spring容器配置 <listener> <listener-class>org.springframework.web.context.C ...
- 初探Spring源码之Spring Bean的生命周期
写在前面的话: 学无止境,写博客纯粹是一种乐趣而已,把自己理解的东西分享出去,不意味全是对的,欢迎指正! Spring 容器初始化过程做了什么? AnnotationConfigApplication ...
随机推荐
- MapReduce:给出children-parents(孩子——父母)表,要求输出grandchild-grandparent(孙子——爷奶)表
hadoop中使用MapReduce单表关联案例: MapReduce:给出children-parents(孩子——父母)表,要求输出grandchild-grandparent(孙子——爷奶)表. ...
- Android Gradle 构建工具(Android Gradle Build Tools)是什么?
转载地址:http://mrfu.me/android/2015/07/17/New_Android_Gradle_Build_Tools/ 译者地址:[翻]一览新的 Android Gradle 构 ...
- Linux中df命令查询磁盘信息和fdisk命令分区的用法
df - 报告文件系统磁盘空间的使用情况 总览 df [OPTION]... [FILE]... POSIX 选项: [-kP] GNU 选项 (最短方式): [-ahHiklmPv] [-t fs ...
- 《Maven实战》第10章 使用Maven进行测试
10.2maven-surefire-plugin插件 [生命周期]的[阶段]与[插件]的[目标]绑定 default生命周期的test阶段:使用单元测试框架运行测试 Maven内置绑定:defaul ...
- Kotlin 卸载APP自身
package com.example.batdw01.myapplication import android.net.wifi.WifiManager import android.support ...
- iOS日常学习 - App之间常用的五种通信方式及适用场景总结
本文为转载学习,原文地址 iOS系统是相对封闭的系统,App各自在各自的沙盒(sandbox)中运行,每个App都只能读取iPhone上iOS系统为该应用程序程序创建的文件夹AppData下的内容,不 ...
- 多线程-闭锁CountDownLatch
闭锁相当于相当于一扇门,在闭锁到达结束状态之前,这扇门一直是关着的,所有的线程都不可以通过.它可以使一个或者一组线程等待某个时间发生.闭锁状态包括一个计数器,初始化的时候传入一个正数,这个数字表示等待 ...
- oracle 子查询详解 in和exists的区别
sql允许多层嵌套,子查询是嵌套在其他查询中的查询.我们可以把子查询当做一张表来看到,即外层语句可以把内嵌的查询结果当做一张表使用. 子查询查询结果有三种情况 不返回查询记录.若子查询不返回记录则主查 ...
- 一块网卡多个IP实现
////////////////////////////写在前面//////////////////////////////////////////// 需要注意,这里我们是一块网卡多个IP,而并非是 ...
- java PreparedStatement和statement的区别
1. PreparedStatement接口继承Statement, PreparedStatement 实例包含已编译的 SQL 语句,所以其执行速度要快于 Statement 对象.2.作为 St ...