springbean的生命周期详细_fragment生命周期详解

springbean的生命周期详细_fragment生命周期详解SpringBean生命周期详解一、简述:Spring是我们每天都在使用的框架,Bean是被Spring管理的Java对象,是Spring框架最重要的部分之一,那么让我们一起了解一下Spring中Bean的生命周期是怎样的吧二、流程图我们先从宏观的角度看一下Spring的生命周期:![在这里插入图片描述](https://img-blog.csdnimg.cn/20201028174058916.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

SpringBean生命周期详解

一、简述:

Spring是我们每天都在使用的框架,Bean是被Spring管理的Java对象,是Spring框架最重要的部分之一,那么让我们一起了解一下Spring中Bean的生命周期是怎样的吧

二、流程图

在这里插入图片描述

总体分为四个阶段:

  ①实例化 CreateBeanInstance
  ②属性赋值 PopulateBean
  ③初始化 Initialization
  ④销毁 Destruction**

其中多个增强接口贯穿了这四个阶段!

三、SpringBean生命周期中的增强接口PostProcessor:

在上图里有多种后置处理器接口,它们贯穿了Bean的生命周期,且它们的实现类都会在SpringIOC容器进行初始化的时候进行实例化,让我们来做一个区分:

|  |  ||--|--||  |  |

解释:

Bean的实例化:
是指Spring通过反射获取Bean的构造方法进行实例化的过程
Bean的初始化:
是指Bean的属性赋值、执行初始化方法(init-method)的过程

四、实例展示

SpringBeanDemo

package com.rx.spring;
 
import com.rx.spring.domain.Person;
import com.rx.spring.domain.Student;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
public class SpringBeanDemo { 
   
    public static void main(String[] args) throws Exception { 
   
        System.out.println("****开始启动****");
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
        System.out.println("****启动完毕****");
        Person person = applicationContext.getBean("person", Person.class);
        Student student = applicationContext.getBean("student", Student.class);
 
        System.out.println("=============================================");
        System.out.println("person:" + person);
        System.out.println("student:" + student);
        person.destroy();
 
        System.out.println("============现在开始关闭容器======================");
        applicationContext.registerShutdownHook();
    }
}

Config

package com.rx.spring;
 
import org.springframework.context.annotation.*;
 
@Configuration
@ComponentScan("com.rx.spring")
@ImportResource("classpath:spring.xml")
public class Config { 
   
     
}

Person

package com.rx.spring.domain;
 
import lombok.Data;
import org.springframework.beans.factory.DisposableBean;
 
@Data
public class Person implements DisposableBean { 
   
    private String name;
    private String address;
    private String tel;
 
    public Person(String name, String address, String tel) { 
   
        System.out.println("Person--->>>有参构造方法");
        this.name = name;
        this.address = address;
        this.tel = tel;
    }
 
    public Person() { 
   
        System.out.println("Person--->>>无参构造方法");
    }
 
    private void raoInitMethod() { 
   
        System.out.println("person--->>>InitMethod...");
    }
 
    private void raoDestroyMethod() { 
   
        System.out.println("person--->>>DestroyMethod...");
    }
 
    @Override
    public void destroy() throws Exception { 
   
        System.out.println("【DisposableBean接口】调用DisposableBean.destroy()");
    }
}

Student

package com.rx.spring.domain;
 
import lombok.Data;
import org.springframework.beans.factory.DisposableBean;
 
@Data
public class Student implements DisposableBean { 
   
    private String username;
    private String password;
 
    public Student(String username, String password) { 
   
        System.out.println("student--->>有参构造方法");
        this.username = username;
        this.password = password;
    }
    public Student() { 
   
        System.out.println("student--->>>无参构造方法");
    }
 
    private void raoInitMethod() { 
   
        System.out.println("student--->>>InitMethod...");
    }
 
    private void raoDestroyMethod() { 
   
        System.out.println("student--->>>DestroyMethod...");
    }
 
    @Override
    public void destroy() throws Exception { 
   
        System.out.println("【DisposableBean接口】调用DisposableBean.destroy()");
    }
}

RaoBeanFactoryPostProcessor

package com.rx.spring.beanfactorypostprocessor;
 
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;
 
@Component
public class RaoBeanFactoryPostProcessor implements BeanFactoryPostProcessor { 
   
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { 
   
        System.out.println("postProcessBeanFactory...");
        String[] beanStr = beanFactory.getBeanDefinitionNames();
        for (String beanName : beanStr) { 
   
            if ("person".equals(beanName)) { 
   
                BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
                MutablePropertyValues m = beanDefinition.getPropertyValues();
                if (m.contains("address")) { 
   
                    //这个方法是判断是否有propertyName=username,有就替换,没有就添加
                    m.addPropertyValue("address", "大兴区");
                    System.out.println("***修改了address属性初始值了***");
                }
            }
        }
    }
}

RaoInstantiationAwareBeanPostProcessor

package com.rx.spring.beanpostprocessor;
 
import com.rx.spring.domain.Person;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.stereotype.Component;
 
@Component
public class RaoInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter { 
   
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException { 
   
        if ("person".equals(beanName) || "student".equals(beanName)) { 
   
            System.out.println(beanName + "--->>>InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation....");
        }
        return null;
    }
 
    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException { 
   
        if ("person".equals(beanName) || "student".equals(beanName)) { 
   
            System.out.println(beanName + "--->>>InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation....");
        }
        return bean instanceof Person;
    }
 
    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException { 
   
        System.out.println(beanName + "--->>>InstantiationAwareBeanPostProcessor.postProcessProperties...");
        PropertyValue[] propertyValues = pvs.getPropertyValues();
        for (PropertyValue propertyValue : propertyValues) { 
   
            if ("name".equals(propertyValue.getName())) { 
   
                propertyValue.setConvertedValue("改后rx");
            }
        }
        return pvs;
    }
}

RaoBeanPostProcessor


package com.rx.spring.beanpostprocessor;
 
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
 
@Component
public class RaoBeanPostProcessor implements BeanPostProcessor { 
   
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { 
   
        if ("person".equals(beanName) || "student".equals(beanName)) { 
   
            System.out.println(beanName + "--->>>BeanPostProcessor.postProcessBeforeInitialization...");
        }
        return bean;
    }
 
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { 
   
        if ("person".equals(beanName) || "student".equals(beanName)) { 
   
            System.out.println(beanName + "--->>>BeanPostProcessor.postProcessAfterInitialization....");
        }
        return bean;
    }
}

spring.xml


<?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 ="person" class="com.rx.spring.domain.Person" init-method="raoInitMethod" destroy-method="raoDestroyMethod">
        <property name="name" value="rx"/>
        <property name="address" value="beijing"/>
        <property name="tel" value="157********"/>
    </bean>
 
    <bean id ="student" class="com.rx.spring.domain.Student" init-method="raoInitMethod" destroy-method="raoDestroyMethod">
        <property name="username" value="rx"/>
        <property name="password" value="1234"/>
    </bean>
 
</beans>

运行结果:

在这里插入图片描述
在这里插入图片描述

运行结果符合预期,成功验证了之前的结论!!!

原创不易,转载请附上本页面链接

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/191906.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • mysql decimal 空,MySQL DECIMAL数据类型

    mysql decimal 空,MySQL DECIMAL数据类型同事问MySQL数据类型DECIMAL(N,M)中N和M分别表示什么含义,M不用说,显然是小数点后的小数位数,但这个N究竟是小数点之前的最大位数,还是加上小数部分后的最大位数?这个还真记不清了。于是乎,创建测试表验证了一番,结果如下:测试表,seller_cost字段定义为decimal(14,2)CREATETABLE`test_decimal`(`id`int(11)NOTNULL,`sell…

    2022年7月17日
    19
  • TkMapper(通用mapper)「建议收藏」

    TkMapper(通用mapper)「建议收藏」TkMapper的配置及使用  TkMapper主要是做单标查询,复杂的多表查询我们还得自己写sql。使用的是Springboot框架使用的数据库表ums_permision:idpidnamevalueicontypeuristatuscreate_timesort10商品nullnull0null12018-09-2916:…

    2022年10月6日
    0
  • vue 部署上线清除浏览器缓存「建议收藏」

    vue 部署上线清除浏览器缓存「建议收藏」vue项目打包上线之后,每一次都会有浏览器缓存问题,需要手动的清除缓存。这样用户体验非常不好,所以我们在打包部署的时候需要尽量避免浏览器的缓存。下面是我的解决方案:一、修改根目录index.html在head里面添加下面代码<metahttp-equiv=”pragram”content=”no-cache”><metahttp-equiv=”cache-control”content=”no-cache,no-store,must-revalidate”>

    2022年7月18日
    12
  • 宿主机与目标机_宿主机目标机开发方法原理

    宿主机与目标机_宿主机目标机开发方法原理在嵌入式开发过程中,有宿主机和目标机的角色之分:宿主机是执行编译、链接嵌入式软件的计算机;目标机是运行嵌入式软件的硬件平台。通常我们用的PC机就是宿主机,而我们用的开发板则是目标机。   我们在宿主机上编译链接生成的软件需要放到目标机上运行,那么怎么放呢?图一则演示了宿主机将软件放到目标机的方式,可以通过串口、网络、USB、JTAG或者JLINK下载到目标机上。如果是

    2022年8月20日
    21
  • jdk1.8 HashMap扩容机制变化「建议收藏」

    jdk1.8 HashMap扩容机制变化「建议收藏」概述JDK1.8中的HashMap较于前代有了较大的变更,主要变化在于扩容机制的改变。在JDK1.7及之前HashMap在扩容进行数组拷贝的时候采用的是头插法,因此会造成并发情景下形成环状链表造成死循环的问题。JDK1.8中改用了尾插法进行数组拷贝,修复了这个问题。其次,JDK1.8开始HashMap改用数组+链表/红黑树组合的数据结构来提高查询效率,降低哈希冲突产生的链表过长导致的查询效率减缓现象。本文的主要内容是对JDK1.8中的扩容机制与前代进行比较。JDK1.8之前的扩容由res

    2022年6月22日
    23
  • 网络编程socket原理_socket的基本概念和原理

    网络编程socket原理_socket的基本概念和原理一、客户机/服务器模式在TCP/IP网络中两个进程间的相互作用的主机模式是客户机/服务器模式(Client/Servermodel)。该模式的建立基于以下两点:1、非对等作用;2、通信完全是异步的。客户机/服务器模式在操作过程中采取的是主动请示方式:首先服务器方要先启动,并根据请示提供相应服务:(过程如下)1、打开一通信通道并告知本地主机,它愿意在某一个公认地址上接收客户请求。2、等待客户请求到

    2022年10月10日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号