Spring创建bean的初始化过程(非整个创建过程)

844 阅读1分钟

一、自定义BeanPostProcessor

package com.morning.morningshiro.processor;

import com.morning.morningshiro.service.CarComponent;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (beanName.equals("carComponent")) {
            System.out.println("postProcessBeforeInitialization 方法执行 before");
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof CarComponent) {
            System.out.println("postProcessAfterInitialization方法执行 after");
        }
        return bean;
    }
}

二、定义一个启动时需要实例化的bean

package com.morning.morningshiro.service;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component
public class CarComponent implements InitializingBean, DisposableBean, BeanNameAware {

    private String carName;

    @Autowired
    private UserService userService;

    public CarComponent(String carName) {
        this.carName = carName;
    }

    public CarComponent() {
        System.out.println("Constructor 构造方法执行, userService = " + userService);
    }

    @PostConstruct
    public void postConstruct() {
        System.out.println("PostConstruct注解方法执行");
    }

    @PreDestroy
    public void preDestroy() {
        System.out.println("PreDestroy注解方法执行");
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("BeanNameAware setBeanNamef方法执行, userService = " + userService);
        this.carName = name;
    }

    @Override
    public void afterPropertiesSet() {
        System.out.println("InitializingBean afterPropertiesSet方法执行");
    }

    @Override
    public void destroy() {
        System.out.println("DisposableBean destroy方法执行");
    }
}

三、 @PostConstruct先于InitializingBean的afterPropertiesSet执行的原因

public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBeanPostProcessor
		implements InstantiationAwareBeanPostProcessor, BeanFactoryAware, Serializable {
		        /**
            	 * Create a new CommonAnnotationBeanPostProcessor,
            	 * with the init and destroy annotation types set to
            	 * {@link javax.annotation.PostConstruct} and {@link javax.annotation.PreDestroy},
            	 * respectively.
            	 */
            	public CommonAnnotationBeanPostProcessor() {
            		setOrder(Ordered.LOWEST_PRECEDENCE - 3);
            		setInitAnnotationType(PostConstruct.class);
            		setDestroyAnnotationType(PreDestroy.class);
            		ignoreResourceType("javax.xml.ws.WebServiceContext");
            	}
		}

四、运行结果

Constructor 构造方法执行, userService = null
BeanNameAware setBeanNamef方法执行, userService = com.morning.morningshiro.service.impl.UserServiceImpl@273ddf01
postProcessBeforeInitialization 方法执行 before
PostConstruct注解方法执行
InitializingBean afterPropertiesSet方法执行
postProcessAfterInitialization方法执行 after

PreDestroy注解方法执行
DisposableBean destroy方法执行

五、Bean初始化的执行顺序:

Constructor --> *Aware --> BeanPostProcessor的before方法 --> @PostConstruct注解的方法 --> InitializingBean的afterPropertiesSet方法 --> init-method --> BeanPostProcessor的after方法

Bean销毁的执行顺序:

@PreDestroy注解的方法 -> DisposableBean的destroy方法