• (009)spring之BeanFactoryPostProcessor的执行时机和作用


      BeanFactoryPostProcessor接口中只有一个方法postProcessBeanFactory,该方法在spring容器初始化后执行,并且只执行一次。它会在BeanPostProcessor中的方法执行之前先执行。示例如下:

      pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.edu.spring</groupId>
        <artifactId>spring</artifactId>
        <version>1.0.0</version>
    
        <name>spring</name>
        <!-- FIXME change it to the project's website -->
        <url>http://www.example.com</url>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
        
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.3.2.RELEASE</version>
            </dependency>
        </dependencies>
    
    </project>
    View Code

      User.java

    package com.edu.spring;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    
    public class User {
    
    private ApplicationContext applicationContext;
        
        public void init(){
            System.out.println("User init()");
        }
        public void show(){
            System.out.println("User show(): "+applicationContext);
        }
        
        @Autowired
        public void setApplicationContext(ApplicationContext applicationContext) {
            System.out.println("------set()-----");
            this.applicationContext = applicationContext;
        }
        
    }
    View Code

      MyConfig.java

    package com.edu.spring;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class MyConfig {
    
        @Bean(initMethod="init")
        public User createUser(){
            return new User();
        }
    }
    View Code

      EchoBeanPostProcssor.java

    package com.edu.spring;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    import org.springframework.stereotype.Component;
    
     
    @Component
    public class EchoBeanPostProcssor implements BeanPostProcessor {
    
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName)
                throws BeansException {
            // TODO Auto-generated method stub
            System.out.println("-------------postProcessBeforeInitialization-------------"+bean.getClass());
            return bean;
        }
    
    
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName)
                throws BeansException {
            // TODO Auto-generated method stub
            System.out.println("-------------postProcessAfterInitialization-------------"+bean.getClass());
            return bean;
        }
    
    }
    View Code

      MyBeanFactoryPostProcessor.java

    package com.edu.spring;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    
        @Override
        public void postProcessBeanFactory(
                ConfigurableListableBeanFactory beanFactory) throws BeansException {
            // TODO Auto-generated method stub
            System.out.println("--------------------"+beanFactory.getBeanDefinitionCount());
        }
    
    }
    View Code

      运行结果如下:

  • 相关阅读:
    C# 与Sql server 获取数据和执行命令
    关于*.ashx 处理程序调试时 未能创建类型 错误
    winform 利用Http向服务器上传与下载文件
    CSS 使用absolute 是<div>居中
    C# int[,] 和 int[][]
    《Head First JavaScript》 学习笔记
    【单片机】关于头文件
    【单片机】【710】定时器
    【C#】委托
    【C#】关于接口的理解
  • 原文地址:https://www.cnblogs.com/javasl/p/11783491.html
Copyright © 2020-2023  润新知