Spring的Bean后置处理器

1. 实体类

/**
* @author 70KG
* @Title: Train
* @Description:
* @date 2018/7/23下午11:31
* @From www.nmyswls.com
*/
public class Train { public Train() {
System.out.println("Train构造方法执行。。。。。。");
} public void init() {
System.out.println("Train的init方法执行。。。。。。");
} public void destroy() {
System.out.println("Train的destroy方法执行。。。。。。");
} }

2. 配置类

⚠️加上对实现类的扫描

/**
* @author 70KG
* @Title: config
* @Description:
* @date 2018/7/23下午11:31
* @From www.nmyswls.com
*/
@Configuration
@ComponentScan("com.nmys.story.springCore.springioc.beanpostprocessor_")
public class Config01 { @Bean(initMethod = "init",destroyMethod = "destroy")
public Train train(){
return new Train();
} }

3. BeanPostProcessor的实现类

⚠️加上@Component注解,目的为了让spring来管理这个实现类

/**
* @author 70KG
* @Title: BeanPostProcessorImpl
* @Description: bean的后置处理器
* @date 2018/7/23下午11:31
* @From www.nmyswls.com
*/
@Component
public class BeanPostProcessorImpl implements BeanPostProcessor { @Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// 返回一个传过来的对象
// 在初始化方法调用之前进行后置处理工作
// 什么时候调用呢?在init-method方法之前就调用了
System.out.println("postProcessBeforeInitialization======" + beanName + "======" + bean);
return bean;
} @Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization======" + beanName + "======" + bean);
return bean;
} }

4. 测试

/**
* @author 70KG
* @Title: FutureTaskTest
* @Description:
* @date 2018/7/23下午11:31
* @From www.nmyswls.com
*/
public class Test01 { @Test
public void test() { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(Config01.class); ac.close(); } }

5. 结果

Train构造方法执行。。。。。。
postProcessBeforeInitialization======train======com.nmys.story.springCore.springioc.beanpostprocessor_.Train@3b69e7d1
Train的init方法执行。。。。。。
postProcessAfterInitialization======train======com.nmys.story.springCore.springioc.beanpostprocessor_.Train@3b69e7d1
Train的destroy方法执行。。。。。。

BeanPostProcessor主要是在类初始化之前,跟之后处理相应的事。

Spring源码窥探之:BeanPostProcessor的更多相关文章

  1. [spring源码学习]五-BeanPostProcessor的使用

    一.接口描述 spring提供了一个接口类-BeanPostProcessor,我们叫他:bean的加工器,应该是在bean的实例化过程中对bean做一些包装处理,里边提供两个方法 public in ...

  2. spring 源码分析之BeanPostProcessor

    1.官方解答: Factory hook that allows for custom modification of new bean instances, e.g. checking for ma ...

  3. Spring源码窥探之:注解方式的AOP原理

    AOP入口代码分析 通过注解的方式来实现AOP1. @EnableAspectJAutoProxy通过@Import注解向容器中注入了AspectJAutoProxyRegistrar这个类,而它在容 ...

  4. Spring源码窥探之:单实例Bean的创建过程

    finishBeanFactoryInitialization(beanFactory);初始化剩下的所有的单实例(非懒加载)Bean(Instantiate all remaining (non-l ...

  5. Spring源码窥探之:声明式事务

    1. 导入驱动,连接池,jdbc和AOP的依赖 <!-- c3p0数据库连接池 --> <dependency> <groupId>c3p0</groupId ...

  6. Spring源码窥探之:AOP注解

    AOP也就是我们日常说的@面向切面编程,看概念比较晦涩难懂,难懂的是设计理念,以及这样设计的好处是什么.在Spring的AOP中,常用的几个注解如下:@Aspect,@Before,@After,@A ...

  7. Spring源码窥探之:@Profile

    Spring为我们提供的多环境启动 1. 配置类,注入三个不同环境的数据源,并加上注解 /** * description: 以下准备了三套不同环境的数据源 * * @author 70KG * @d ...

  8. Spring源码窥探之:Spring AOP初步使用

    AOP即面向切面编程.它的底层实际是用了spring的动态代理,具体是JDK的代理还是CGLIB的代理,就视情况而定了.本博客园仅仅作为平时记录,显得有些杂乱无章,如果想了解动态代理,设计模式,请访问 ...

  9. Spring源码窥探之:@Value

    1. 首先定义实体 /** * @author 70KG * @Title: Apple * @Description: 苹果实体 * @date 2018/10/22下午9:26 * @From w ...

随机推荐

  1. [转帖]说一说JVM双亲委派机制与Tomcat

    说一说JVM双亲委派机制与Tomcat https://www.cnblogs.com/dengchengchao/p/11844022.html 讲个故事: 以前,爱捣鼓的小明突然灵机一动,写出了下 ...

  2. Redhat7.6Linux版本下,在Oracle VM VirtualBox下hostonly下IP地址配置

    安装配置Linux的Redhat7.6教程见:https://www.cnblogs.com/xuzhaoyang/p/11264563.html 然后,配置完之后,我们开始配置IP地址,配置IP地址 ...

  3. 关于st表

    #include<cstdio> #include<iostream> #include<cmath> #include<cctype> #includ ...

  4. laravel中一些非常常用的php artisan命令

    php artisan 命令在开发laravel项目中非常常用,下面是一些总结 composer config -g repo.packagist composer https://mirrors.a ...

  5. Django RuntimeError: Model class app_anme.models.Ad doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.报错

    报错内容 RuntimeError: Model class app_anme.models.Ad doesn't declare an explicit app_label and isn't in ...

  6. JavaScript中数组的key-value在对象中倒装的妙用

    对于数组的去重.寻找指定元素的索引,通常我们都是通过遍历来解决,但是在某些应用场景下,将数组的value-key进行倒装,也即将value当做对象的key,key当做对象value,可以极大降低算法的 ...

  7. linux route详细解读

    route命令用于显示和操作IP路由表.要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现.在Linux系统中,设置路由通常是 为了解决以下问题:该Linu ...

  8. sql语句将图片插入image类型的字段中

    update table set photo=(SELECT * FROM OPENROWSET(BULK N'D:\no.png', SINGLE_BLOB) as Photo)    From:h ...

  9. wstngfw中使用虚拟IP映射内网IP

    wstngfw中使用虚拟IP映射内网IP -------------------------------- Server01: IP: 192.168.195.73/24 GW: 192.168.19 ...

  10. 《Linux》跟老男孩学Linux核心系统命令

    一.命令行简介 1.1 Linux 命令行提示符介绍 [root@root_pc ~]# #<==这是超级管理员root用户对应的命令行 [oldboy@oldboy_pc ~]$ #<= ...