Spring BeanPostProcessor接口详细使用

BeanPostProcessor接口简介

BeanPostProcessor也称为Bean后置处理器,它是Spring中定义的接口,在Spring容器的创建过程中(具体为Bean初始化前后)会回调BeanPostProcessor中定义的两个方法。

BeanPostProcessor的具体代码如下:

public interface BeanPostProcessor {
    //bean初始化之前
    @Nullable
    default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

	//bean初始化之后
    @Nullable
    default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}

postProcessBeforeInitialization方法会在每一个bean对象的初始化方法调用之前回调;postProcessAfterInitialization方法会在每个bean对象的初始化方法调用之后被回调。

这两个方法分别在init方法前后执行,需要注意一点,我们定义一个类实现了BeanPostProcessor,默认是会对整个Spring容器中所有的bean进行处理。
既然是默认全部处理,那么我们怎么确认我们需要处理的某个具体的bean呢?
可以看到方法中有两个参数。类型分别为Object和String,第一个参数是每个bean的实例,第二个参数是每个bean的name或者id属性的值。所以我们可以第二个参数,来确认我们将要处理的具体的bean。

BeanPostProcessor示例

创建User对象

public class User implements InitializingBean, DisposableBean {

    public User() {
        System.out.println("实例化User构造方法...");
    }

    public void initUser() {
        System.out.println("User初始化方法initUser...");
    }

    public void destroyUser() {
        System.out.println("User销毁方法destroyUser...");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("调用实现DisposableBean的destroy方法....");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("调用实现InitializingBean的afterPropertiesSet方法......");
    }
}

BeanPostProcessor实现类

public class UserPostProcessor implements BeanPostProcessor {
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
        // 这边只做简单打印   原样返回bean
        System.out.println("postProcessBeforeInitialization===="+beanName);
        return bean;
    }
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        // 这边只做简单打印   原样返回bean
        System.out.println("postProcessAfterInitialization===="+beanName);
        return bean;
    }

}

配置文件UserConfig

@Configuration
public class UserConfig {

    @Bean(initMethod = "initUser",destroyMethod = "destroyUser")
    public User user() {
         return new User();
    }

    @Bean
    public UserPostProcessor userPostProcessor() {
         return new UserPostProcessor();
    }
}

测试类

public class Test {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(UserConfig.class);
        ((AnnotationConfigApplicationContext) applicationContext).close();
    }
}

执行结果:

实例化User构造方法...
postProcessBeforeInitialization====user
调用实现InitializingBean的afterPropertiesSet方法......
User初始化方法initUser...
postProcessAfterInitialization====user
19:57:17.700 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@2437c6dc, started on Tue Oct 22 19:57:17 CST 2019
调用实现DisposableBean的destroy方法....
User销毁方法destroyUser...
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

境里婆娑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值