Spring bean生命周期

Spring bean生命周期_第1张图片
测试结果
Spring bean生命周期_第2张图片
测试代码
配置类

package com.dill.config;

import com.dill.entity.Blog;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @program: spring原理探究
 * @description:
 * @author: vua
 * @create: 2020-02-23 12:21
 */
@Configuration
@Import(_ImportSelector.class)
public class SpringConfig {
    @Bean(initMethod ="init",destroyMethod = "destroy")
    public Blog blog(){
        Blog blog=new Blog();
        blog.setName("blog");
        return blog;
    }
}

package com.dill.config;

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @program: spring原理探究
 * @description:
 * @author: vua
 * @create: 2020-02-23 12:32
 */
public class _ImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        return new String[]{"com.dill.life._BeanDefinitionRegistryPostProcessor",
                "com.dill.life._BeanFactoryPostProcessor",
                "com.dill.life._BeanPostProcessor",
                "com.dill.life._InstantiationAwareBeanPostProcesson"};
    }
}

实体类

package com.dill.entity;

import lombok.Data;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

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

/**
 * @program: spring原理探究
 * @description:
 * @author: vua
 * @create: 2020-02-23 12:27
 */
@Data
public class Blog implements  BeanNameAware , BeanFactoryAware, ApplicationContextAware,InitializingBean {
    private String name;
    public Blog(){
        System.out.println("blog 实例化");
        System.out.println("属性name赋值前:"+name);
    }
    @Override
    public void setBeanName(String s) {
        System.out.println("属性name赋值后:"+name);
        System.out.println("BeanNameAware:"+s);
    }
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("ApplicationContextAware:setBeanFactory blog");
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("BeanFactoryAware:setApplicationContext blog");
    }
    @PostConstruct
    public void postConstruct(){
        System.out.println("@PostConstruct blog");
    }
    @Override
    public void afterPropertiesSet(){
        System.out.println("InitializingBean:afterPropertiesSet blog");
    }
    public void init() {
        System.out.println("initMethod blog");
    }
    @PreDestroy
    public void preDestroy(){
        System.out.println("@PreDestroy blog");
    }
    public void destroy(){
        System.out.println("destroyMethod blog");
    }
}

后置处理器

package com.dill.life;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;

/**
 * @program: spring原理探究
 * @description:
 * @author: vua
 * @create: 2020-03-12 11:45
 */
public class _BeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
    public _BeanDefinitionRegistryPostProcessor(){
        System.out.println("_BeanDefinitionRegistryPostProcessor 实例化");
    }
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        System.out.println("BeanDefinitionRegistryPostProcessor:postProcessBeanDefinitionRegistry");
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        System.out.println("BeanDefinitionRegistryPostProcessor:postProcessBeanFactory");
    }
}

package com.dill.life;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

/**
 * @program: spring原理探究
 * @description:
 * @author: vua
 * @create: 2020-03-12 11:46
 */
public class _BeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    public _BeanFactoryPostProcessor(){
        System.out.println("_BeanFactoryPostProcessor 实例化");
    }
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        System.out.println("BeanFactoryPostProcessor:postProcessBeanFactory");
    }
}
package com.dill.life;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.lang.Nullable;

/**
 * @program: spring原理探究
 * @description:
 * @author: vua
 * @create: 2020-03-12 11:38
 */
public class _InstantiationAwareBeanPostProcesson implements InstantiationAwareBeanPostProcessor {
    public _InstantiationAwareBeanPostProcesson(){
        System.out.println("_InstantiationAwareBeanPostProcesson 实例化");
    }
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        System.out.println("InstantiationAwareBeanPostProcessor:postProcessBeforeInstantiation "+beanName);
        return null;
    }

    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        System.out.println("InstantiationAwareBeanPostProcessor:postProcessAfterInstantiation "+beanName);
        return true;
    }

    @Nullable
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        System.out.println("InstantiationAwareBeanPostProcessor:postProcessProperties "+beanName);
        return null;
    }
}

package com.dill.life;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;


/**
 * @program: spring原理探究
 * @description:
 * @author: vua
 * @create: 2020-02-23 12:41
 */
public class _BeanPostProcessor implements BeanPostProcessor {
    public _BeanPostProcessor(){
        System.out.println("_BeanPostProcessor 实例化");
    }
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("BeanPostProcessor:postProcessBeforeInitialization"+" "+beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("BeanPostProcessor:postProcessAfterInitialization"+" "+beanName);
        return bean;
    }
}


你可能感兴趣的:(java,java,spring,java,spring)