首页 > 其他分享 >Spring6.0官方文档示例:(28)多种方式添加BeanPostProcessor

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

时间:2023-11-10 12:32:19浏览次数:44  
标签:BeanPostProcessor Object Spring6.0 示例 beanName 28 bean import public


一、定义三个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_java


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

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

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


标签:BeanPostProcessor,Object,Spring6.0,示例,beanName,28,bean,import,public
From: https://blog.51cto.com/amadeusliu/8295879

相关文章

  • [ABC286G] Unique Walk 题解
    洛谷题目链接At题目链接这题一看就很欧拉路径,但是怎么做呢?如果只有关键边,那么连通图首先会变成一堆连通块,这时只需要分别对每个连通块进行欧拉路径判断,但是对于不属于欧拉路径的连通块,很难对加上非关键边的情况进行扩展。如果只有非关键边,那么连通图还是变成一堆连通块,但是这......
  • 16、Flink 的table api与sql之连接外部系统: 读写外部系统的连接器和格式以及FileSyst
    文章目录Flink系列文章一、Table&SQLConnectors1、概述2、支持的外部连接3、使用示例:kafka4、Transformtableconnector/formatresources5、SchemaMapping6、Metadata7、PrimaryKey8、TimeAttributes9、ProctimeAttributes10、RowtimeAttributes11、完整示例1)、建表2)、......
  • R语言和jsonlite库编写代码示例
    R语言和jsonlite库来下载的程序。#导入jsonlite库library(jsonlite)#设置代理主机和端口proxy_host<-""proxy_port<-#使用httr库创建一个对象proxy<-create_proxy(proxy_host,proxy_port)#使用httr库的GET方法下载网页内容url<-""response<-GET(url,pro......
  • Ruby语言和VCR库编写代码示例
    Ruby语言和VCR库编写一个下载程序来完成任务。以下是代码的详细解释:require'vcr'require'open-uri'#设置VCR的配置VCR.configuredo|config|config.cassette_library_dir='vcr_cassettes'config.hook_into:webmockconfig.default_cassette_options={:rec......
  • Rust和isahc库编写代码示例
    Rust和isahc库编写的图像爬虫程序的代码:useisahc::{Client,Response};fnmain(){letclient=Client::new().with_proxy("").finish();leturl="";letresponse=client.get(url).send().await......
  • 典型的 HTTP 会话-响应示例(请求资源不存在的网页响应)
    1HTTP/1.1404NotFound2Content-Type:text/html;charset=utf-83Content-Length:382174Connection:keep-alive5Cache-Control:no-cache,no-store,must-revalidate,max-age=06Content-Language:en-US7Date:Thu,06Dec201817:35:13GMT8Ex......
  • 典型的 HTTP 会话-响应示例
    1HTTP/1.1200OK2Content-Type:text/html;charset=utf-83Content-Length:557434Connection:keep-alive5Cache-Control:s-maxage=300,public,max-age=06Content-Language:en-US7Date:Thu,06Dec201817:37:18GMT8ETag:"2e77ad1dc6ab......
  • 19、Flink 的Table API 和 SQL 中的自定义函数及示例(4)
    (文章目录)本文展示了自定义函数在Flinksqlclient的应用以及自定义函数中使用pojo的示例。本文依赖flink、kafka集群能正常使用。本文分为2个部分,即自定义函数在Flinksqlclient中的应用以及自定义函数中使用pojo数据类型。本文的示例如无特殊说明则是在Flink1.17版本中运......
  • 简单的C程序示例
        程序调整:程序的输出是否在屏幕上一闪而过?如果遇到这种情况,可以在程序中添加额外的代码,让窗口等待用户按下一个键后关闭。一种方法是,在程序的return语句前添加一行代码。  getchar();这行代码会让程序等待击键,窗口会在用户按下一个键后才关闭。    1、#inclu......
  • C语言程序设计 函数递归调用示例
    函数递归调用示例(教材习题5.3,运行结果012345)#include<stdio.h>voidfun(intk);voidmain(){intw=5;fun(w);}voidfun(intk){if(k>0)fun(k-1);printf("%d",k);}......