ICO operation Bean management (bean scope and life cycle)

Table of contents

bean scope

How to prove whether it is a single instance or multiple instances?

How to set it as single instance or multiple instances?

scope attribute value

The difference between singleton and prototype

bean life cycle

Demo bean life cycle

Bean post processor, seven steps!


bean scope

In Spring, set whether to create a bean instance as a single instance or multiple instances

How to prove whether it is a single instance or multiple instances?


Get it multiple times in the class and then output it. Look at the output address. The address is the same as a single instance, and the address is different. Multiple instances

     @Test
    public void testCollection2(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean2.xml");
        Book book1 = context.getBean("book", Book.class);
        Book book2 = context.getBean("book", Book.class);
         System.out.println(book1); 
         System.out.println(book2);
    }

In the console output, it can be seen from the figure below that the default is a single-instance object , and we can also set it to a multi-instance object.

How to set it as single instance or multiple instances?

There is a property (scope) in the bean tag of the spring configuration file to set whether it is a single instance or multiple instances.

scope attribute value

The first value is the default value, singleton means a single instance object

The second value, prototype, represents a multi-instance object

xml file. The test method is the same as above, run and view the console, as shown in the figure below, the address is different, and it is concluded that it is set as a multi-instance object.

    <bean id="book" class="com.atguigu.spring5.collectiontype.Book" scope="prototype">
        <property name="list" ref="booklist"></property>
    </bean>

The difference between singleton and prototype

The first singleton single instance, prototype multiple instances

Second, when the scope value is set to singleton, a single instance object will be created when the spring configuration file is loaded; if the scope value is set to prototype, the object is not created when the spring configuration file is loaded, but a multi-instance object is created when the getBean method is called

bean life cycle

Lifecycle: The process from object creation to object destruction.

  1.  Create a bean instance through the constructor (no parameter construction);
  2. Set values ​​for bean properties and other bean references (call the set method);
  3. Call the initialization method of the bean (the method that needs to be configured and initialized);
  4. The bean can be used (the object is obtained);
  5. When the container is closed, call the bean's destruction method (the method that needs to be configured and destroyed)

Demo bean life cycle

basic structure:

    <bean id="orders" class="com.atguigu.spring5.bean.Orders">
        <property name="oname" value="地方"></property>
    </bean>
public class Orders {

    private String oname;

    public void setOname(String oname) {
        this.oname = oname;
    }
}

   Create a bean instance through the constructor (no parameter construction):

public class Orders {

    //无参数构造
    public Orders() {
        System.out.println("第一步 执行无参数构造创建bean实例 ");
    }

    private String oname;
   // 为bean的属性设置值
    public void setOname(String oname) {
        this.oname = oname;
        System.out.println("第二步 调用set 方法设置属性值");
    }

    //创建执行的初始化的方法
    public void initMethod(){
        System.out.println("第三步 执行初始化的方法");
    }
    //创建执行的初始化的方法
    public void destroyMethod(){
        System.out.println("第五步 执行销毁的方法");
    }
}
    @Test
    public void testBean3(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
        Orders orders = context.getBean("orders", Orders.class);
        System.out.println("第四步 获取创建bean实例对象");
        System.out.println(orders);

        //手动让bean实例销毁
        context.close();
    }

Bean post processor, seven steps!

  1.  Create a bean instance through the constructor (no parameter construction);
  2. Set values ​​for bean properties and other bean references (call the set method);
  3. The method of passing the bean instance to the bean post-processor: postProcessBeforeInitialization
  4. Call the initialization method of the bean (the method that needs to be configured and initialized);
  5. The method of passing the bean instance to the bean post-processor: postProcessAfterInitialization
  6. The bean can be used (the object is obtained);
  7. When the container is closed, call the bean's destruction method (the method that needs to be configured and destroyed)

Demonstration of adding post processor effects

public class MyBatisPost implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("在初始化之前执行的方法");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("在初始化之后执行的方法");
        return bean;
    }
}
 <!--配置后置处理器-->
    <bean id="myBeanPost" class="com.atguigu.spring5.bean.MyBatisPost"></bean>

 

Supongo que te gusta

Origin blog.csdn.net/m0_57448314/article/details/128120414
Recomendado
Clasificación