spring bean生命周期

spring bean生命周期_第1张图片

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> 
    </parent>
<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
package fcc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component
public class A extends Base{
    @Autowired
    private String name;
    public A(){
        System.out.println("第一步:A实例化。。。");
    }
    public void setName(String name) {
        System.out.println("第二步:A属性注入");
        this.name = name;
    }
    @PostConstruct
    public void init1(){
        System.out.println("第?步:init-method");
    }
    @PreDestroy
    public void destroy1(){
        System.out.println("第八步:destroy");
    }
}
package fcc;

public class B extends Base{
    public void sayHello(){
        System.out.println("B sayHello");
    }
    public void init(){
        System.out.println("B's Init...");
    }
}
package fcc;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public abstract class Base implements BeanNameAware, ApplicationContextAware , InitializingBean {
    private static AnnotationConfigApplicationContext context;
    private String email;
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    @Override
    public void setBeanName(String name) {
        System.out.println("第三步01:Aware :"+name);
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = (AnnotationConfigApplicationContext)applicationContext;
        System.out.println("第三步02:Aware :"+applicationContext);
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("第五步01:afterPropertiesSet 重写:");
    }
    public void registerBean(String beanName,Object object){
        context.getBeanFactory().registerSingleton(beanName,object);
    }
    public  void destroy(String beanName,Object object){
        context.getBeanFactory().destroyBean(beanName, object);
    }
}
package fcc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = {"fcc"})
public class MainConfig {
    @Bean
    public String name(){
        return "a";
    }
    @Bean
    public String email(){
        return "[email protected]";
    }
    @Bean(initMethod = "init")
    public B b(){
        return new B();
    }
}
package fcc;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MyAnnotationConfigApplicationContext extends AnnotationConfigApplicationContext {
    public MyAnnotationConfigApplicationContext(Class<?>... annotatedClasses){
//        setAllowCircularReferences();
    }
}
package fcc;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

import java.util.Iterator;

@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        Iterator<String> it = beanFactory.getBeanNamesIterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
    }
}
package fcc;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
    @Autowired
    private String email ;
    private  Boolean isLogFlag =false;
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof A) {
            System.out.println("第四步:BeanPostProcessor Before:" + beanName);
            ( (Base)bean).setEmail(email);
        }
        return bean;
    }
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof A) {
            System.out.println("第七步:BeanPostProcessor After:" + beanName);
//            ( (Base)bean).setEmail(email);
            if(isLogFlag){
                System.out.println("开始记日志:"+beanName);
            }
        }
        return bean;
    }
}
public class MainClass {
    public static void main(String[] args){
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfig.class);
        A a = ctx.getBean(A.class);
//        B b = new B();
//        b.registerBean("b",b);
//        System.out.println(ctx.getBean("b"));
//        b.destroy("b",b);
//        System.out.println(ctx.getBean("b"));
        ctx.close();
    }
}

你可能感兴趣的:(Spring)