Spring-1 - important concepts

Keywords: Spring Attribute xml Junit

1, IOC and DI

  1. IOC (reverse control): the creation of objects is handed over to spring for configuration.
  2. DI (dependency injection): sets values to properties in a class.
    Relationship between IOC and DI: DI cannot exist alone and needs to be completed on the basis of IOC.

2, Scope of Bean

Singleton is a singleton type. When you create a container, you automatically create a bean object at the same time. Whether you use it or not, it exists. Every object you get is the same object. Note that the singleton scope is the default in Spring.
Prototype is a prototype type. It is not instantiated when we create the container, but only when we get the bean to create an object, and the object we get each time is not the same object.

3, Bean's life cycle

The entire execution of the Bean lifecycle is described below.

1) Call the Bean constructor or factory method to instantiate the Bean according to the configuration.

2) Use dependency injection to complete the configuration injection of all attribute values in the Bean.

3) If the Bean implements the BeanNameAware interface, Spring calls the setBeanName() method of the Bean to pass in the id value of the current Bean.

4) If the Bean implements the BeanFactoryAware interface, Spring calls the setBeanFactory() method to pass in a reference to the current factory instance.

5) If the Bean implements the ApplicationContextAware interface, Spring calls the setApplicationContext() method to pass in a reference to the current ApplicationContext instance.

6) If BeanPostProcessor is associated with a Bean, Spring will call the pre initialization method of the interface, postprocessbeforeinitialization(), to process the Bean. Here, it is very important that Spring's AOP is implemented with it.

7) If the Bean implements the InitializingBean interface, Spring calls the afterPropertiesSet() method.

8) If an initialization method is specified in the configuration file through the init method attribute, the initialization method is called.

9) If BeanPostProcessor is associated with a Bean, Spring calls the interface's initialization method, postProcessAfterInitialization(). At this point, the Bean can be used by the application system.

10) If the scope of the Bean is specified as scope = "singleton" in, putting the Bean into the cache pool of Spring IoC will trigger Spring's life cycle management of the Bean; if the scope of the Bean is specified as scope = "prototype" in, the Bean will be handed over to the caller, and the caller will manage the Bean's life cycle, and Spring will no longer Manage the Bean.

11) If the Bean implements the DisposableBean interface, Spring will call the destroy() method to destroy the Bean in Spring; if the destroy method of the Bean is specified through the destroy method attribute in the configuration file, Spring will call the method to destroy the Bean.

Spring provides a detailed and comprehensive life cycle process for beans. By implementing a specific interface or property setting, you can have an impact on the Bean's life cycle process. Although the properties can be configured at will, it is recommended not to use beans too much to implement the interface, because this will lead to too tight aggregation of code and spring.

Life cycle validation

Class Person:

package com.seven.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class Person implements BeanNameAware, BeanFactoryAware, ApplicationContextAware,
        InitializingBean, DisposableBean {
    private String name;

    public void setBeanName(String name) {
        System.out.println("Implemented BeanNameAware Interface setBeanName Method");
    }

    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("Implemented BeanFactoryAware Interface setBeanFactory Method");
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("Implemented ApplicationContextAware Interface setApplicationContext Method");
    }

    public void afterPropertiesSet() throws Exception {
        System.out.println("Implemented InitializingBean Interface afterPropertiesSet Method");
    }

    public void myInitMethod() {
        System.out.println("Custom initialization method executed");
    }

    public void destroy() throws Exception {
        System.out.println("Implemented DisposableBean Interface destroy Method");
    }

    public void myDestoryMethod() {
        System.out.println("Custom destruction method implemented");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("Set property value");
        this.name = name;
    }
}

MyBeanPostProcessor class:

package com.seven.bean;

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

public class MyBeanPostProcessor implements BeanPostProcessor {
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("Implemented BeanPostProcessor Interface postProcessBeforeInitialization Method");
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("Implemented BeanPostProcessor Interface postProcessAfterInitialization Method");
        return bean;
    }
}

xml file:

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

    <bean id="person" class="com.seven.bean.Person" init-method="myInitMethod" destroy-method="myDestoryMethod">
        <property name="name" value="Set up name Value"/>
    </bean>

    <bean class="com.seven.bean.MyBeanPostProcessor"/>

</beans>

Test class:

import com.seven.bean.Person;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CycleTest {
    private ApplicationContext context;

    @Before
    public void init() {
        context = new ClassPathXmlApplicationContext("spring.config.xml");
    }

    @After
    public void destory() {
        ((ClassPathXmlApplicationContext) context).close();
    }

    @Test
    public void test() {
        System.out.println("------------------------------");
        Person person = (Person) context.getBean("person");
        System.out.println("Use this bean");
    }
}

Execution result:

Update time: January 18, 2020
37 original articles published, praised 0, and 387 visitors
Private letter follow

Posted by Willburt on Sat, 18 Jan 2020 00:04:01 -0800