springbean的生命周期详细_fragment生命周期详解
详解 详细 生命周期 fragment springbean
2023-06-13 09:13:40 时间
大家好,又见面了,我是你们的朋友全栈君。
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.cn/191906.html原文链接:https://javaforall.cn
相关文章
- python中pygame怎么安_Python中pygame安装方法图文详解
- Runnable接口详细详解「建议收藏」
- RNA-seq 详细教程:详解DESeq2流程(9)
- 【流媒体开发】VLC Media Player - Android 平台源码编译 与 二次开发详解 (提供详细800M下载好的编译源码及eclipse可调试播放器源码下载)
- Centos7 服务器 yum安装mongodb的详细步骤详解
- MongoDB的导入导出详解数据库
- Oracle 11g服务详细介绍详解数据库
- MySQL与Redis的区别—详细!详解数据库
- 数据库各种索引详细解释详解数据库
- Window 下mysql binlog开启及查看,mysqlbinlog详解数据库
- slenium截屏详解编程语言
- 熟练掌握HDFS的Shell访问详解大数据
- JavaScript SHA512加密算法详细代码详解编程语言
- numpy的random模块详细解析详解大数据
- Redis在win7上的可视化应用详解大数据
- JS实现全选、反选、不选详解编程语言
- Go语言圣经–示例: 并发的Echo服务详解编程语言
- [javaEE] http协议详细详解编程语言
- 安装GCC-4.6.1详细教程详解编程语言
- JSP EL表达式详细介绍详解编程语言
- 整理一下以前的Html+css3复习笔记详解编程语言
- java中DecimalFormat详细使用用法详解编程语言
- 如何使用webservice来校验一个邮箱的有效性:详解编程语言
- springmvc 项目完整示例06 日志–log4j 参数详细解析 log4j如何配置详解编程语言
- Springboot2本地锁实践详解编程语言
- C语言_指针变量的赋值与运算,很详细详解编程语言
- Linux防火墙介绍及详细解析(linux防火墙详解)
- Linux下查看文件连接信息:LSOF使用详解(linuxlsof)
- slave架构详解MySQL主从复制的详细介绍(mysql中master)
- MySQL数据表同步的详细解析(mysql 两表同步详解)
- MySQL XOR详解了解这一操作符的使用方法(mysql xor详细)
- Redis集群功能与实现详解(redis集群详细介绍)
- Oracle ORM精解基于对象关系映射的简洁代码实现(oracle ORM详解)
- 深入mysql创建自定义函数与存储过程的详解