Spring6.0官方文档示例:(28)多种方式添加BeanPostProcessor

一、定义三个BeanPostProcessor

package cn.edu.pku;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

@Component
public class MyScannedBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("my scanned BeanPostProcessor......before: " + beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("my scanned BeanPostProcessor......after " + beanName);
        return bean;    }
}

package cn.edu.pku;

import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyXmlBeanPostProcessor implements BeanPostProcessor {
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        System.out.println("my xml BeanPostProcessor......before : " + beanName);

        return bean; //
    }
    public Object postProcessAfterInitialization(Object bean, String beanName) {
        System.out.println("my xml BeanPostProcessor......after : " + beanName);
        return bean;
    }
}

package cn.edu.pku;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyManualBeanPostProcessor implements BeanPostProcessor{
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("my manual BeanPostProcessor......before: " + beanName);
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("my manual BeanPostProcessor......after : " + beanName);
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
}

二、定义bean类:

package cn.edu.bju.service;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Locale;

@Service
public class PrintService {
    public String printInfo(String info){
        System.out.println(info);
        return info.toUpperCase();
    }
}

三、配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">




        <bean id="printService" class="cn.edu.bju.service.PrintService" lazy-init="true"/>

        <context:component-scan base-package="cn.edu.pku"/>
        <bean class="cn.edu.pku.MyXmlBeanPostProcessor"/>


</beans>


四、定义主类

package cn.edu.tju.test;

import cn.edu.pku.MyManualBeanPostProcessor;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public final class Test04{
    public static void main(final String[] args) throws Exception {
        ConfigurableApplicationContext ctx = new
                ClassPathXmlApplicationContext("spring3.xml");

        ctx.getBeanFactory().addBeanPostProcessor(new MyManualBeanPostProcessor());
        Object printService = ctx.getBean("printService");
        System.out.println(printService);
    }
}


五、运行结果:
在这里插入图片描述
可以看到,ComponentScan扫描到的BeanPostProcessor最先执行,xml配置文件里的随后执行,而我们手动添加的BeanPostProcessor最后执行(因为它在保存BeanPostProcessor的CopyOnWriteArrayList的末尾)

###############################
执行顺序貌似和官方文档描述有不同
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值