zoukankan      html  css  js  c++  java
  • (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

      运行结果如下:

  • 相关阅读:
    Alibaba 阿里组件 nacos注册中心 gateway网关 flowable流程引擎 vue.js前后分离 spring cloud微服务
    Java JDBC 连接 MySQL8 数据库
    Java设计模式【命令模式】
    Spring Boot内嵌tomcat关于getServletContext().getRealPath获取得到临时路径的问题
    Java代理模式学习 (Proxy模式)
    Java单例模式浅析 (Singleton模式)
    由歌词引发的模式思考之中篇(AbstractFactory模式)
    Java模拟FilterChain的实现 (Chain Of Responsibility模式)
    由歌词引发的模式思考之上篇(FactoryMethod模式)
    由歌词引发的模式思考之下篇(模拟Spring的BeanFactory)
  • 原文地址:https://www.cnblogs.com/javasl/p/11783491.html
Copyright © 2011-2022 走看看