Spring Bean的生命周期函数

首先:InstantiationAwareBeanPostProcessor接口的postProcessAfterInstantiation方法返回的是false和本文走的不是一样的步骤,结果如何可以根据我提供的代码自行尝试

在这里插入图片描述
上图是一个大概的流程,接下来做具体解释:
1.实现了BeanFactoryPostProcessor的类实例化
2.调用BeanFactoryPostProcessor的postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)方法
这里可以得到beanFactory,(GenericBeanDefinition)beanFactory.getBeanDefinition(“studentService”);
它可以得到类的定义
3.实现了BeanPostProcessor的类实例化
4.实现了InstantiationAwareBeanPostProcessor的类实例化

以上四步只会执行一次,下面的几步对于每个需要实例化的bean都会执行

5.调用InstantiationAwareBeanPostProcessor中的
postProcessBeforeInstantiation(Class<?> beanClass, String beanName)方法
该方法可以获得正准备实例化的类的beanName和该bean的Class
6.实例化bean的类(调用构造方法),此时已经有实例化的对象了,只不过没有填充属性值
7.调用InstantiationAwareBeanPostProcessor中的
postProcessAfterInstantiation(Object bean, String beanName)方法
该方法可以刚刚已经实例化过的类的实例(bean)和beanName(默认是类的首字母小写),该方法返回一个boolean值,如果为
true的话走8
8.调用InstantiationAwareBeanPostProcessor中的
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)
和上面的7作用差不多,多了PropertyValues pvs,它里面包含三个属性,还不知道干什么的
propertyValueList = {ArrayList@1596} size = 0
processedProperties = null
converted = false
9.装配属性(@Autowired的属性),如果对应的类没有被实例化,走一波它的生命周期(没有销毁)
10.如果正在实例化的这个类实现了BeanNameAware接口,执行它的setBeanName方法,因为在类内部,所以可以通过this得到实例化
的对象,因为走了9,所以得到的实际上已经是实例化完全的bean了,不过bean的生命周期还没走完
11.如果正在实例化的这个类实现了BeanFactoryAware接口,执行它的setBeanFactory(BeanFactory beanFactory)方法,
得到的beanFactory可以使用getBean(String beanName)获得其他实例化好的bean
12.调用BeanPostProcessor中的
postProcessBeforeInitialization(Object bean, String beanName)方法
你可以通过beanName.equals(“你想要的bean名字”)来得到对应实例化的对象bean
13.如果正在实例化的这个类实现了InitializingBean接口,执行它的afterPropertiesSet()方法,也可以用this。。。
14.
用法和12差不多

此时bean除了销毁,已经走过了它大部分的生命周期,可以在主线程中通过getBean方法获得bean了

15.如果bean的类实现了DisposableBean接口并且上下文调用了close()方法,执行重写的destroy()方法,最后销毁。

ps:使用xml配置的bean的init-method,destory-method对应方法和实现InitializingBean,DisposableBean接口
重写它们的afterPropertiesSet(),destroy()方法效果一样,不用写多余的东西。
下面是测试程序所涉及的代码
配置类:

package com.cui.config;

import com.cui.model.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName Config
 * @Description
 * @Author Senwei Cui
 * @Date 2019/9/23 0023 下午 3:37
 * @Version
 */
@Configuration
@ComponentScan("com.cui")
public class Config {

	public Config(){
		System.out.println("Config 实例化");
	}

	@Bean
	public Student student(){
		Student student = new Student();
		student.setAge(20);
		student.setName("cui");
		return student;
	}
}

模型

package com.cui.model;

/**
 * @ClassName Student
 * @Description
 * @Author Senwei Cui
 * @Date 2019/9/23 0023 下午 3:36
 * @Version
 */
public class Student{
	private String name;
	private Integer age;

	public Student(){
		System.out.println("student 实例化");
	}

	public String getName() {
		return name;
	}

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

	public Integer getAge() {
		return age;
	}

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

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

三个不同后置处理器的实现类

package com.cui.post_processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.stereotype.Component;

/**
 * @ClassName CuiBeanFactoryPostProcessor
 * @Description
 * @Author Senwei Cui
 * @Date 2019/9/23 0023 下午 8:56
 * @Version
 */
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

	public MyBeanFactoryPostProcessor(){
		System.out.println("BeanFactoryPostProcessor 实例化");
	}

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		GenericBeanDefinition beanDefinition = (GenericBeanDefinition)beanFactory.getBeanDefinition("studentService");
		System.out.println("BeanFactoryPostProcessor--postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)");
	}
}

package com.cui.post_processor;

import com.cui.sercice.StudentService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * @ClassName StudentServiceBeanPostProcessor
 * @Description
 * @Author Senwei Cui
 * @Date 2019/10/1 0001 下午 3:29
 * @Version
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor{

	public MyBeanPostProcessor(){
		System.out.println("BeanPostProcessor 实例化");
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println(beanName+" before BeanPostProcessor"+bean);
		if (beanName.equals("studentService")) {
			((StudentService)bean).setA(2);
		}
		return null;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println(beanName+" after BeanPostProcessor"+bean);
		if (beanName.equals("studentService")) {
			((StudentService)bean).setA(3);
		}
		return null;
	}
}

package com.cui.post_processor;

import com.cui.sercice.StudentService;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * @ClassName myInstantiationAwareBeanPostProcessor
 * @Description
 * @Author Senwei Cui
 * @Date 2019/10/1 0001 下午 4:02
 * @Version
 */
@Component
public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {
	public MyInstantiationAwareBeanPostProcessor(){
		System.out.println("InstantiationAwareBeanPostProcessor 实例化");
	}

	@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		System.out.println(beanName+" before InstantiationAwareBeanPostProcessor ");
		return null;
	}

	@Override
	public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
		System.out.println(beanName+" after InstantiationAwareBeanPostProcessor "+bean);
		if(beanName.equals("studentService")){
			((StudentService)bean).setB("b");
		}
		//返回false不走postProcessProperties方法
		return true;
	}

	@Override
	public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
		System.out.println(beanName+" postProcess Properties "+bean);
		return null;
	}

}

实现了其他生命周期接口的bean的类

package com.cui.sercice;

import com.cui.model.Student;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @ClassName StudentService
 * @Description
 * @Author Senwei Cui
 * @Date 2019/9/23 0023 下午 4:45
 * @Version
 */
@Component
public class StudentService implements BeanNameAware , BeanFactoryAware , InitializingBean , DisposableBean {
	private Integer a;
	private String b;

	@Autowired
	private Student student;

	public StudentService(){
		System.out.println("StudentService 实例化");
	}

	public Integer getA() {
		return a;
	}

	public void setA(Integer a) {
		this.a = a;
	}

	public String getB() {
		return b;
	}

	public void setB(String b) {
		this.b = b;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	@Override
	public String toString() {
		return "StudentService{" +
				"a=" + a +
				", b='" + b + '\'' +
				", student=" + student +
				'}';
	}

	@Override
	public void setBeanName(String name) {
		System.out.println(name+" BeanNameAware--setBeanName this->"+this);
	}

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		Object student = beanFactory.getBean("student");
		System.out.println("BeanFactoryAware--setBeanFactory beanFactory.getBean(\"student\");->"+student);
	}

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

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

测试类


```java
package com.cui;

import com.cui.config.Config;
import com.cui.sercice.StudentService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @ClassName Test1
 * @Description
 * @Author Senwei Cui
 * @Date 2019/9/23 0023 下午 3:31
 * @Version
 */
public class Test1 {
	public static void main(String[] args) {
		//初始化spring上下文,或者说初始话spring容器
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);

		System.out.println("初始化上下文结束");

		StudentService studentService = context.getBean(StudentService.class);

		Integer a = studentService.getA();
		System.out.println(a);

		context.close();
	}
}

测试结果截图
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值