spring Spring's Bean basis of life cycle (c)

Spring Ioc container Bean initialization process, the essential purpose Spring Ioc container is to manage Bean, Bean for, the existence of its life cycle in a container, his initialization and destruction also requires a process, the process requires some self-defined, we can insert code to change some of their behavior to meet specific needs, which requires the use of Spring Bean knowledge of the life cycle.

  In order to understand the life cycle of the Spring Ioc container initialization process and destroy the Bean. The following diagram illustrates the process Spring Ioc container initialization and destruction of Bean

SpringIoc container management of the Bean

Explain the steps:

1 . If the Bean implements the interface BeanNameAware of setBeanName method, then calling this method will be Spring Ioc container-managed Bean.

2 . If the Bean implements the interface BeanFactoryBean of setBeanFactory method, it will call this method.

3 . If the Bean implements the interface ApplicationContextAware of setApplicationContext method, and the Spring Ioc container must also be a ApplicationContext interface implementation class, it will call this method, otherwise it will not be called.

4 . If the Bean implements the interface the BeanPostProcessor postProcessBeforeInitialization method, it will call this method.

5 . If the Bean implements the interface BeanFactoryPostProcessor afterPropertiesSet method, he would call this method.

6 If Bean custom initialization method, it will call a custom initialization method.

7 . If the Bean implements the interface the BeanPostProcessor postProcessAfterInitialzation methods, complete these calls, this time Bean initialization is complete, there are also among the Spring Ioc container, the user can derive Bean service.

When the server is down or Spring ioc encounter turned off, he will be called to Bean's destruction, as follows

8 . If the Bean implements the interface DisposableBean the destroy method, it will call it.

9 If you have defined your own methods of destruction, it will execute custom method.



He said a lot of theory, then we will practice the following Prove it

1, first define a BeanPostProcessImpl class, let Spring Ioc container to manage it, this time we need to achieve BeanPostProcessor interfaces, prove 4 and 7.


package pro.jamal.blog.domain;

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

/**
 * @author: lyj
 * @Date: 2019/6/2
 */
public class BeanPostProcessImpl implements BeanPostProcessor {

    /**
     * 在初始化之前的方法
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String s) throws BeansException {
        System.out.println("["+bean.getClass().getSimpleName()+"]:  初始化开始...");
        returnthe bean; 
    } / ** 
     treatment after initialization * 
     * / 
    @Override public Object postProcessAfterInitialization (the bean Object, String S) throws BeansException { 
        System.out.println ( " [ ." + bean.getClass () getSimpleName () + " ]: example of end ... \ n-BYE ");
         return the bean; 
    } 
}

    
    

2, in the definition of a class that implements the interface 1,2,3,5, then a custom initialization method, article 6 to prove the theory, simple printing

package pro.jamal.blog.domain;

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

/**
 * @author: lyj
 * @Date: 2019/6/2
 */
public class Person implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {
    private String name;
    private  Integer age;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {

        System.out.println("["+this.getClass().getSimpleName()+"]"+ "调用了BeanFactoryAware接口的setBeanFactory方法");
    }

    @Override
    public void setBeanName(String s) {
        System.out.println("["+this.getClass().getSimpleName()+"]"+ "调用了BeanNameAware接口的setBeanName方法");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("["+this.getClass().getSimpleName()+"]"+ "调用了DisposableBean接口的destroy方法");

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("["+this.getClass().getSimpleName()+"]"+ "调用了InitializingBean接口的afterPropertiesSet方法");

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        System.out.println("["+this. .getClass () getSimpleName () + " ] " + " call setApplicationContext ApplicationContextAware interface method "); 
    } public void diyInit () { 
        System.out.println ( " [ " + the this .getClass () getSimpleName (). + " ]: " + " custom initialization start "); 
    } public String getName () {
         return name; 
    } public void the setName (String name) {
         the this .name = name; 
    } public Integer getAge () {
         return Age; 
    } public


     





    

     

    

     void setAge(Integer age) {
        this.age = age;
    }



}

3, xml configuration

<?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
                        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="pro.jamal.blog.domain.BeanPostProcessImpl"/>
    <bean class="pro.jamal.blog.domain.Person" init-method="diyInit">
        <property name="name" value="yongjar"/>
        <property name="age" value="14"/>
    </bean>

</beans>

4. Test

public class BeanCycleTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("bean.xml");
         Person bean = context.getBean(Person.class);
        System.out.println(bean.getName() +"," + bean.getAge());
        context.close();
    }
}

Print results

image

Guess you like

Origin www.cnblogs.com/jamal/p/10963954.html