欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Bean生命周期

程序员文章站 2022-05-24 17:48:35
...

Spring Bean生命周期

一.概述

在spring bean生命周期中,涉及初始化和销毁​​回调。在bean生命周期中也会调用不同的spring bean感知类。一旦依赖注入完成,就会执行初始化回调方法。它们的目的是检查已在bean属性中设置的值,执行任何自定义初始化或在原始bean上提供包装等。一旦初始化回调完成,就可以使用bean了。当IoC容器即将删除bean时,执行销毁回调方法。它们的目的是释放bean持有的资源或执行任何其他完成任务。当bean实现了多个初始化和析构回调方法时,那些方法按特定顺序执行。我们将讨论初始化和销毁​​回调以及spring bean感知类的执行顺序。

二.bean生命周期步骤

bean生命周期包括以下步骤:
1.在IoC容器中,使用类构造函数创建一个spring bean。
2.使用setter方法执行依赖注入。
3.一旦依赖注入完成,BeanNameAware.setBeanName()就会被调用。在bean工厂中设置名称。
4.调用BeanClassLoaderAware.setBeanClassLoader()提供bean实例。
5.调用BeanFactoryAware.setBeanFactory()为bean实例提供工厂。
6.IoC容器调用BeanPostProcessor.postProcessBeforeInitialization。
7.在@PostConstruct调用带注释的方法。
8.调用@PostConstruct,InitializingBean.afterPropertiesSet()方法。
9.在init-method中调用XML配置信息找到bean中的属性。
10.调用BeanPostProcessor.postProcessAfterInitialization()
11.完成bean实例。
12.当ApplicationContext关闭时,调用registerShutdownHook()。
13.调用DisposableBean.destroy()。
14。在destroy-method调用XML配置中bean 的属性指定的方法。
15.在垃圾收集之前,调用finalize()。

三.示例

1.创建Book实体

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class Book implements InitializingBean, DisposableBean,
		BeanFactoryAware, BeanNameAware, BeanClassLoaderAware {
	private String bookName;

	private Book() {
		System.out.println("---inside constructor---");
	}

	@Override
	public void setBeanClassLoader(ClassLoader classLoader) {
		System.out.println("---BeanClassLoaderAware.setBeanClassLoader---");
	}

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

	public void myPostConstruct() {
		System.out.println("---init-method---");
	}

	@PostConstruct
	public void springPostConstruct() {
		System.out.println("[email protected]");
	}

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		System.out.println("---BeanFactoryAware.setBeanFactory---");
	}

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

	public String getBookName() {
		return bookName;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
		System.out.println("setBookName: Book name has set.");
	}

	public void myPreDestroy() {
		System.out.println("---destroy-method---");
	}

	@PreDestroy
	public void springPreDestroy() {
		System.out.println("[email protected]");
	}

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

	@Override
	protected void finalize() {
		System.out.println("---inside finalize---");
	}
}

2.

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {
		System.out.println("BeanPostProcessor.postProcessAfterInitialization");
		return bean;
	}
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		System.out.println("BeanPostProcessor.postProcessBeforeInitialization");
		return bean;
	}
} 

3.创建main

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDemo {
	public static void main(String[] args) {
		AbstractApplicationContext context = new ClassPathXmlApplicationContext("application-config.xml");
		Book book = (Book)context.getBean("book");
		System.out.println("Book Name:"+ book.getBookName());
	        context.registerShutdownHook();
	}
} 

4.配置bean

<?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: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">
	<context:component-scan base-package="com.example.demo" />
	<bean id="book" class="com.example.demo.Book" init-method="myPostConstruct"
		destroy-method="myPreDestroy">
		<property name="bookName" value="Mahabharat" />
	</bean>
	<bean class="com.example.demo.MyBeanPostProcessor" />
</beans> 

5.运行结果

---inside constructor---
setBookName: Book name has set.
---BeanNameAware.setBeanName---
---BeanClassLoaderAware.setBeanClassLoader---
---BeanFactoryAware.setBeanFactory---
BeanPostProcessor.postProcessBeforeInitialization
[email protected]
---InitializingBean.afterPropertiesSet---
---init-method---
BeanPostProcessor.postProcessAfterInitialization
Book Name:Mahabharat
[email protected]
---DisposableBean.destroy---
---destroy-method---