Spring6.0官方文档示例:(28)多种方式添加BeanPostProcessor
  Ta2cNb9VdLMk 2023年11月19日 26 0


一、定义三个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);
    }
}

五、运行结果:

Spring6.0官方文档示例:(28)多种方式添加BeanPostProcessor_开发语言


可以看到,ComponentScan扫描到的BeanPostProcessor最先执行,xml配置文件里的随后执行,而我们手动添加的BeanPostProcessor最后执行(因为它在保存BeanPostProcessor的CopyOnWriteArrayList的末尾)###############################

执行顺序貌似和官方文档描述有不同

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


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月19日 0

暂无评论

推荐阅读
Ta2cNb9VdLMk