Spring study notes-bean life cycle and scope

bean life cycle

Reprinted article: https://blog.csdn.net/qq_39323228/article/details/103284456

https://blog.csdn.net/w_linux/article/details/80086950

  • Instantiate the bean object (via constructor or factory method)
  • Set object properties (setter, etc.) (dependency injection)
  • If the Bean implements the BeanNameAware interface, the factory calls the setBeanName() method of the Bean to pass the ID of the Bean. (And the following one are for checking the Aware interface)
  • If the Bean implements the BeanFactoryAware interface, the factory calls the setBeanFactory() method to pass in the factory itself
  • Pass the Bean instance to the postProcessBeforeInitialization(Object bean, String beanname) method of the Bean's preprocessor
  • Call the initialization method of Bean
  • Pass the Bean instance to the postProcessAfterInitialization(Object bean, String beanname) method of the Bean's post processor
  • Use Bean
  • Before the container is closed, call the destruction method of the Bean

 

After reading the above two articles carefully, I basically understand it. Here is the code when understanding the life cycle

package com.example.Spingbootdemo.dto;

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.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


/**
 * @Author: xx
 * @Date: 2020/12/5
 */

public class LomBoom implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean,
    DisposableBean {
    private String field;

    public LomBoom() {
        System.out.println("SpringBean 构造方法");
    }

    public String getField() {
        System.out.println("SpringBean get方法");
        return field;
    }

    public void setField(String field) {
        System.out.println("SpringBean set方法");
        this.field = field;
    }

    @Override
    public void setBeanName(String s) {
        System.out.println("BeanName:"+s);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("BeanFactory");

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("ApplicationContext");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("destroy");
    }

    public void init(){
        System.out.println("init");
    }

    public void des(){
        System.out.println("des");
    }
}

 

package com.example.Spingbootdemo;

import com.example.Spingbootdemo.dto.LomBoom;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@SpringBootApplication
public class SpingbootdemoApplication {

	public static void main(String[] args) {

		SpringApplication.run(SpingbootdemoApplication.class, args);

		//ApplicationContext context = new AnnotationConfigApplicationContext(LomBoom.class);

		ApplicationContext context=new ClassPathXmlApplicationContext("component-bean.xml");

		LomBoom lomBoom = (LomBoom) context.getBean("lomBoom");
		//Bean的使用

		System.out.println(lomBoom);
		//关闭容器
		((AbstractApplicationContext) context).close();


	}

}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
       default-autowire="byName">

    <bean id="lomBoom" class="com.example.Spingbootdemo.dto.LomBoom"  init-method="init" destroy-method="des" >
        <property name="field" value="test" />
    </bean>

</beans>

 

bean scope

Spring 3 defines 5 scopes for Bean, namely singleton (singleton), prototype (prototype), request, session and global session. The 5 scopes are explained as follows:

  1. Singleton: Singleton mode. Only one shared Bean instance exists in the Spring IoC container. No matter how many Beans refer to it, it always points to the same object. The Singleton scope is the default scope in Spring. You can also explicitly define the Bean as a singleton mode and configure it as:
    • <bean id="userDao" class="com.ioc.UserDaoImpl" scope="singleton"/>
  2. Prototype: Prototype mode. Every time a bean defined by prototype is obtained through the Spring container, the container will create a new Bean instance. Each Bean instance has its own properties and state, while the singleton has only one object globally. According to experience, use prototype scope for stateful beans and singleton scope for stateless beans.
  3. request: In an Http request, the container will return the same instance of the Bean. For different Http requests, a new bean will be generated, and the bean is only valid in the current Http Request.
    • <bean id="loginAction" class="com.cnblogs.Login" scope="request"/>, for each Http request, the Spring container creates a brand new instance according to the bean definition, and the instance is only in the current Http request The request is valid, but other requests cannot see the state changes in the current request. When the current Http request ends, the bean instance will also be destroyed.
  4. Session: In an Http Session, the container will return the same instance of the Bean. A request for a different Session will create a new instance, and the bean instance is only valid in the current Session.
    • <bean id="userPreference" class="com.ioc.UserPreference" scope="session"/>, same as Http request, each session request creates a new instance, and different instances do not share attributes, and the instance It is only valid in your own session request. When the request is over, the instance will be destroyed.
  5. Global Session: In a global Http Session, the container will return the same instance of the Bean, which is only valid when the portlet context is used.

 

Guess you like

Origin blog.csdn.net/mumuwang1234/article/details/113729550