Spring Bean的作用域和启动流程

bean的作用域

Spring容器在初始化一个Bean的实例时,会指定该实例的作用域,以下是bean的作用域种类:
(1)singleton:单例模式,使用singleton定义的Bean在Spring容器中只有一个实例,这也是Bean默认的作用域。
(2)prototype:原型模式,每次通过Spring容器获取prototype定义的Bean 时,容器都将创建一个新的Bean实例。
(3)request:在同一次HTTP请求中,容器会返回该Bean的同一个实例。而对不同的HTTP请求,会返回不同的实例。即该作用域仅在当前HTTP请求内有效。
(4)session:在同一次HTTP Session 中,容器会返回该Bean的同一个实例。而对不同的HTTP请求,会返回不同的实例。即该作用域仅在当前HTTP Session内有效。
(5)global Session:在一个全局的HTTP Session中,容器会返回该Bean的同一个实例。即该作用域仅在使用portlet context时有效。

下面举例最常用的singleton和prototype作用域:
1.singleton作用域
创建SingletonBean类:

@Component("singletonBean") //表明该类会作为组件类并告知Spring要为这个类创建bean
@Scope("singleton") //声明作用域
public class SingletonBean {
    private String flag;

    public void setFlag(String flag) {
        this.flag = flag;
    }

    public void seeFlag(){
        System.out.println(flag);
    }
}

创建配置类:

@Configuration
@ComponentScan //启动组件扫描
public class BeanConfig {
}

测试bean:

public class App {
    public static void main( String[] args ) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
        SingletonBean singletonBean0 = (SingletonBean) applicationContext.getBean("singletonBean");
        singletonBean0.setFlag("I am SingletonBean!");
        singletonBean0.seeFlag();
        SingletonBean singletonBean1 = (SingletonBean) applicationContext.getBean("singletonBean");
        singletonBean1.seeFlag();
    }
}

运行后控制台输出如下:


截图20200305215009.png

可以看出两次输出的结果相同,这说明Spring容器只创建了一个SingletonBean的实例。

2.prototype作用域
同样的,创建PrototypeBean类:

@Component("prototypeBean")
@Scope("prototype")
public class PrototypeBean {
    private String flag;

    public void setFlag(String flag) {
        this.flag = flag;
    }

    public void seeFlag(){
        System.out.println(flag);
    }
}

测试bean:

public class App {
    public static void main( String[] args ) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
        PrototypeBean prototypeBean0 = (PrototypeBean) applicationContext.getBean("prototypeBean");
        prototypeBean0.setFlag("I am PrototypeBean!");
        prototypeBean0.seeFlag();
        PrototypeBean prototypeBean1 = (PrototypeBean) applicationContext.getBean("prototypeBean");
        prototypeBean1.seeFlag();
    }
}

控制台输出如下:


Spring Bean的作用域和启动流程_第1张图片
截图

可以看出两次输出的结果不同,即两次是不同的PrototypeBean实例。

Spring Bean启动流程

启动流程为:
1.实例化BeanFactoryPostProcessor实现类。
2.调用BeanFactoryPostProcessor的postProcessBeanFactory方法。
3.实例化BeanPostProcessor实现类。
4.实例化InstantiationAwareBeanPostProcessorAdapter实现类。
5.调用InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation方法。
6.Bean实例化。
7.调用InstantiationAwareBeanPostProcessor的postProcessAfterInstantiation方法。
8.调用InstantiationAwareBeanPostProcessor的postProcessPropertyValues方法。
9.注入属性message。
10.调用BeanNameAware的setBeanName()方法。
11.调用BeanFactoryAware的setBeanFactory()方法。
12.调用ApplicationContextAware的setApplicationContext方法。
13.调用BeanPostProcessor的postProcessBeforeInitialization方法。
14.调用InitializingBean的afterPropertiesSet()方法。
15.调用init-method属性配置的初始化方法。
16.调用BeanPostProcessor的postProcessAfterInitialization方法。
此时,容器初始化成功,Bean也就可以使用了,如果调用者使用的是singleton bean,则从Spring的缓冲池中将准备好的bean返回给调用者,其生命周期还是Spring容器在管理;如果配置的是prototype bean,则将准备好的Bean返回给调用者,生命周期就交给调用方管理了。
17.singleton bean使用完毕之后,调用DiposibleBean的destory()方法。
18.调用destroy-method属性配置的初始化方法,销毁bean。
证明:
创建bean类并继承有关接口:

public class Bean implements BeanFactoryAware, BeanNameAware,InitializingBean, DisposableBean, ApplicationContextAware {
    private String message;

    public Bean() {
        System.out.println("6.Bean实例化");
    }

    public void setMessage(String message) {
        System.out.println("9.注入属性message");
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    @Override
    public String toString() {
        return "Bean{" +
                "message='" + message + '\'' +
                '}';
    }

    @Override
    public void setBeanName(String s) {
        System.out.println("10.调用BeanNameAware的setBeanName()方法");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("11.调用BeanFactoryAware的setBeanFactory()方法");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("12.调用ApplicationContextAware的setApplicationContext方法");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("14.调用InitializingBean的afterPropertiesSet()方法");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("17.调用DiposibleBean的destory()方法");
    }

    public void myInit() {
        System.out.println("15.调用init-method属性配置的初始化方法");
    }

    public void myDestory() {
        System.out.println("18.调用destroy-method属性配置的初始化方法,销毁bean");
    }
}

创建BeanFactoryPostProcessor实现类:

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    public MyBeanFactoryPostProcessor() {
        super();
        System.out.println("1.实例化BeanFactoryPostProcessor实现类");
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        System.out.println("2.调用BeanFactoryPostProcessor的postProcessBeanFactory方法");
    }
}

创建BeanPostProcessor实现类:

public class MyBeanPostProcessor implements BeanPostProcessor {

    public MyBeanPostProcessor() {
        super();
        System.out.println("3.实例化BeanPostProcessor实现类");
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println("16.调用BeanPostProcessor的postProcessAfterInitialization方法");
        return bean;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println("13.调用BeanPostProcessor的postProcessBeforeInitialization方法");
        return bean;
    }
}

创建InstantiationAwareBeanPostProcessorAdapter实现类:

//InstantiationAwareBeanPostProcessor是BeanPostProcessor的子接口
public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {

    public MyInstantiationAwareBeanPostProcessor() {
        super();
        System.out.println("4.实例化InstantiationAwareBeanPostProcessorAdapter实现类");
    }

    @Override
    public Object postProcessBeforeInstantiation(Class beanClass,
                                                 String beanName) throws BeansException {
        System.out.println("5.调用InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation方法");
        return null;
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        System.out.println("7.调用InstantiationAwareBeanPostProcessor的postProcessAfterInstantiation方法");
        return true;
    }

    @Override
    public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
        System.out.println("8.调用InstantiationAwareBeanPostProcessor的postProcessPropertyValues方法");
        return pvs;
    }
}

创建配置文件applicationContext.xml:




    
    

    
    

    
    

    

测试:

public class App {
    public static void main( String[] args ) {
        System.out.println("------提示:现在开始初始化容器------");
        ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("------提示:容器初始化成功------");
        Bean bean = factory.getBean(Bean.class);
        System.out.println("------输出bean:" + bean + "------");
        System.out.println("------提示:现在开始关闭容器------");
        ((ClassPathXmlApplicationContext)factory).close();
    }
}

控制台输出如下:


Spring Bean的作用域和启动流程_第2张图片
截图

你可能感兴趣的:(Spring Bean的作用域和启动流程)