Spring学习笔记之bean生命周期

 更新时间:2017年12月13日 11:21:47   作者:小崔  
Spring Bean是Spring应用中最最重要的部分了。下面这篇文章主要给大家介绍了关于Spring学习笔记之bean生命周期的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。

前言

上一篇文章主要学习了下bean的配置、注入、自定义属性编辑器,今天来熟悉bean的生命周期。

任何一个事物都有自己的生命周期,生命的开始、生命中、生命结束。大家最熟悉的应该是servlet 的生命周期吧。和 servlet 一样 spring bean 也有自己的生命周期。

在开发中生命周期是一个很常见的名词,基本每种编程语言都能找到与它关联的。关于bean的生命周期我在网上也找了好多,基本都差不多。这里我主要是想通过代码来验证,毕竟学的知识都是一样的,都是学的Java,最重要的是动手练习,这样印象更深。

下面是它生命周期的描述,我们通过demo来进行验证。

下图是它执行的顺序。

一、创建LiftCycle类实现BeanFactoryAware,BeanNameAware,InitializingBean,DisposableBean,ApplicationContextAware5个接口方法

package Cuiyw.Spring.Service;
import org.springframework.beans.BeansException;
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;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class LifeCycle implements BeanFactoryAware,BeanNameAware,InitializingBean,DisposableBean,ApplicationContextAware{
 
 private String name;
 public String getName() {
 System.out.println("getName name="+name);
 return name;
 }
 public void setName(String name) {
  System.out.println("setName name="+name);
 this.name = name;
 }
 public void afterPropertiesSet() throws Exception {
 // TODO Auto-generated method stub
  System.out.println("InitializingBean.afterPropertiesSet()");
 }
 public void setBeanName(String arg0) {
 // TODO Auto-generated method stub
 System.out.println("BeanNameAware.setBeanName");
 }
 public void setBeanFactory(BeanFactory arg0) throws BeansException {
 // TODO Auto-generated method stub
 System.out.println("BeanFactoryAware.setBeanFactory");
 }
 public void destroy() throws Exception {
 // TODO Auto-generated method stub
 System.out.println("DisposableBean.destroy");
 }
 public void myInit() {
 System.out.println("【init-method】调用<bean>的init-method属性指定的初始化方法");
 }
 public void myDestory() {
 System.out.println("【destroy-method】调用<bean>的destroy-method属性指定的初始化方法");
 }
 public void setApplicationContext(ApplicationContext arg0) throws BeansException {
 // TODO Auto-generated method stub
  System.out.println("ApplicationContextAware.setApplicationContext");
 } 
}

二、注册InstantiationAwareBeanPostProcessor接口

package Cuiyw.Spring.Service;

import java.beans.PropertyDescriptor;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;

public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor{
 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
 // TODO Auto-generated method stub
 System.out.println("InstantiationAwareBeanPostProcessor.postProcessAfterInitialization");
 return bean;
 }
 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
 // TODO Auto-generated method stub
 System.out.println("InstantiationAwareBeanPostProcessor.postProcessBeforeInitialization");
 return bean;
 }
 public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
 // TODO Auto-generated method stub
 System.out.println("InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation");
 return true;
 }
 public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
 // TODO Auto-generated method stub
 System.out.println("InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation");
 return null;
 }
 public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean,
  String beanName) throws BeansException {
 // TODO Auto-generated method stub
 System.out.println("InstantiationAwareBeanPostProcessor.postProcessPropertyValues");
 return pvs;
 }
}

三、注册BeanPostProcessor接口

其实InstantiationAwareBeanPostProcessor继承BeanPostProcessor,所以在上面我也实现了BeanPostProcessor接口的方法

package Cuiyw.Spring.Service;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
 // TODO Auto-generated method stub
 System.out.println("BeanPostProcessor.postProcessAfterInitialization ");
 return bean;
 }
 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
 // TODO Auto-generated method stub
 System.out.println("BeanPostProcessor.postProcessBeforeInitialization");
 return bean;
 }
}

四、注册BeanFactoryPostProcessor接口

package Cuiyw.Spring.Service;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
 public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException {
 // TODO Auto-generated method stub
 System.out.println("BeanFactoryPostProcessor.postProcessBeanFactory");
 }
}

五、在上下文中配置

这里还是在上一个博客demo的基础上进行修改,为了有其他干扰,我先把service去掉了。

<?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 id="beanPostProcessor" class="Cuiyw.Spring.Service.MyBeanPostProcessor"></bean>
<bean id="instantiationAwareBeanPostProcessor" class="Cuiyw.Spring.Service.MyInstantiationAwareBeanPostProcessor"></bean>
<bean id="beanFactoryPostProcessor" class="Cuiyw.Spring.Service.MyBeanFactoryPostProcessor"></bean>
<bean id="lifeCycle" class="Cuiyw.Spring.Service.LifeCycle" init-method="myInit" destroy-method="myDestory">
<property name="name" value="cuiyw1"></property>
</bean>
</beans>

六、在main中使用bean

package Cuiyw.SpringAop;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import Cuiyw.Spring.IService.IService;
import Cuiyw.Spring.Service.LifeCycle;

public class App 
{
 public static void main( String[] args )
 {
 ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"ApplicationContext.xml"});
 BeanFactory factory=context;
 LifeCycle lifeCycle=factory.getBean("lifeCycle",LifeCycle.class);
 lifeCycle.setName("cuiyw2");
  System.out.println("lifeCycle.name="+lifeCycle.getName());
 ((ClassPathXmlApplicationContext)factory).registerShutdownHook(); 
 /*service=(IService)factory.getBean("ServiceA");
 service.service("Cuiyw ServiceA"); 
 service=(IService)factory.getBean("ServiceImpl");
 service.service("Cuiyw ServiceImpl"); */
 }
}

七、输入打印结果

可以发现输出的顺序和上面图的顺序基本一致。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

相关文章

  • SpringBoot中注解@ConfigurationProperties与@Value的区别与使用详解

    SpringBoot中注解@ConfigurationProperties与@Value的区别与使用详解

    本文主要介绍了SpringBoot中注解@ConfigurationProperties与@Value的区别与使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • Java AOP动态代理详细介绍

    Java AOP动态代理详细介绍

    AOP是一种设计思想,是软件设计领域中的面向切面编程,它是面向对象编程的一种补充和完善。本文将用Java实现AOP代理的三种方式,需要的可以参考一下
    2022-08-08
  • Java实现MD5加密算法方法例子

    Java实现MD5加密算法方法例子

    这篇文章主要给大家介绍了关于Java实现MD5加密算法方法的相关资料,MD5加密是一种常见的加密方式,我们经常用在保存用户密码和关键信息上,需要的朋友可以参考下
    2023-10-10
  • Java 信息摘要加密MD2、MD4、MD5实现详解

    Java 信息摘要加密MD2、MD4、MD5实现详解

    这篇文章主要介绍了Java 信息摘要加密MD2、MD4、MD5实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • java list中包含某个字符串的两种方法实现

    java list中包含某个字符串的两种方法实现

    在Java开发中,经常需要判断一个List中是否包含特定的字符串,包括使用contains()方法和循环遍历判断,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • IDEA新手必备之各种快捷键详解

    IDEA新手必备之各种快捷键详解

    这篇文章主要介绍了IDEA新手必备之各种快捷键详解,文中有非常详细的快捷键介绍,对正在使用IDEA的小伙伴们有非常好的帮助,需要的朋友可以参考下
    2021-04-04
  • Java泛型的简单实例

    Java泛型的简单实例

    这篇文章介绍了Java泛型的简单实例,有需要的朋友可以参考一下
    2013-10-10
  • MyBatis学习教程之开发Dao的方法教程

    MyBatis学习教程之开发Dao的方法教程

    这篇文章主要给大家介绍了关于MyBatis开发Dao的相关资料,使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法。文中通过示例代码介绍的非常详细,需要的朋友们下面来一起看看吧。
    2017-07-07
  • Java设计模式常用原则解析

    Java设计模式常用原则解析

    这篇文章主要介绍了Java设计模式常用原则解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • springboot2.5.6集成RabbitMq实现Topic主题模式(推荐)

    springboot2.5.6集成RabbitMq实现Topic主题模式(推荐)

    这篇文章主要介绍了springboot2.5.6集成RabbitMq实现Topic主题模式(推荐),pom.xml引入依赖和常量类创建,本文通过实例代码给大家介绍的非常详细,需要的朋友参考下吧
    2021-11-11

最新评论