Spring生命週期的整體回顧

一,spring的注入bean的生命週期的集中方式已經在前面的文章裏面描述了,這裏借用大佬的圖片再次描述的清楚些;

 

上面描述的是spring容器初始化的一個流程。 

這裏實現BeanPostProcessor重寫postProcessBeforeInitialization(Object bean, String beanName)和postProcessAfterInitialization(Object bean, String beanName)方法

  • postProcessBeforeInitialization(Object bean, String beanName)在afterPropertiesSet() 之前執行
  • postProcessAfterInitialization(Object bean, String beanName)在myInit() 方法之後執行

後置處理器實例

public class Category {
    private String id;
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Category{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

 

2.我的後置處理器

    配置文件

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

    <bean id="category" class="base.domain.Category">
        <property name="id" value="1"/>
        <property name="name" value="於紅亮"/>
    </bean>

    <bean id="product" class="base.lifecycle.Product"/>

    <bean id="myBeanPostProcessor" class="base.beanpostProcessor.MyBeanPostProcessor"/>

</beans>

  

3.測試

@Test
    public void test21() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext4.xml");
        Category category = (Category)ctx.getBean("category");
        System.out.println(category);
    }

  4.其他說明

會對所有的bean進行後置處理,避免這個情況的發生要善於使用Object bean, String beanName方法參數,進行instanceof或者使用beanName進行某些判斷

 

3.總結

後置處理器是在bean創建後,在初始化前後進行的 處理,通過上面的圖片應該能夠深入的理解其所在的位置。爲了嘉慶理解下面在放一次。
在這裏插入圖片描述

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

實現BeanPostProcessor重寫postProcessBeforeInitialization(Object bean, String beanName)和postProcessAfterInitialization(Object bean, String beanName)方法

  • postProcessBeforeInitialization(Object bean, String beanName)在afterPropertiesSet() 之前執行
  • postProcessAfterInitialization(Object bean, String beanName)在myInit() 方法之後執行

2.後置處理器實例

  1. 實體類
package base.domain;

/**
 * @author yuhl
 * @Date 2020/11/2 21:48
 * @Classname Category
 * @Description TODO
 */
public class Category {
    private String id;
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Category{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  1. 我的後置處理器
package base.beanpostProcessor;

import base.domain.Category;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @author yuhl
 * @Date 2020/11/2 21:45
 * @Classname MyBeanPostProcessor
 * @Description 此bean的後置處理器會對所有的bean進行加工。所以針對某一個類型的bean進行加工,或者對某一個beanName進行加工,可以進行控制哦!
 */
public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof Category) {
            Category category = (Category) bean;
            category.setName("yuhl");
        }
        return bean;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  1. 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="category" class="base.domain.Category">
        <property name="id" value="1"/>
        <property name="name" value="於紅亮"/>
    </bean>

    <bean id="product" class="base.lifecycle.Product"/>

    <bean id="myBeanPostProcessor" class="base.beanpostProcessor.MyBeanPostProcessor"/>

</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  1. 測試
  @Test
    public void test21() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext4.xml");
        Category category = (Category)ctx.getBean("category");
        System.out.println(category);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  1. 其他說明
    會對所有的bean進行後置處理,避免這個情況的發生要善於使用Object bean, String beanName方法參數,進行instanceof或者使用beanName進行某些判斷

3.總結

後置處理器是在bean創建後,在初始化前後進行的 處理,通過上面的圖片應該能夠深入的理解其所在的位置。爲了嘉慶理解下面在放一次。
在這裏插入圖片描述

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章