当前位置: 首页 > news >正文

深圳城乡和建设局网站今日特大军事新闻

深圳城乡和建设局网站,今日特大军事新闻,大良营销网站建设精英,59网站一起做网店广州Bean实例化的基本流程 加载xml配置文件&#xff0c;解析获取配置中的每个的信息&#xff0c;封装成一个个的BeanDefinition对象将BeanDefinition存储在一个名为beanDefinitionMap的Map<String,BeanDefinition>中ApplicationContext底层遍历beanDefinitionMap&#xff0c…

Bean实例化的基本流程

  1. 加载xml配置文件,解析获取配置中的每个的信息,封装成一个个的BeanDefinition对象
  2. 将BeanDefinition存储在一个名为beanDefinitionMap的Map<String,BeanDefinition>中
  3. ApplicationContext底层遍历beanDefinitionMap,创建Bean实例对象
  4. 创建好的Bean实例对象,被存储到一个名为singletonObjects的Map<String,Object>中当执行applicationContext.getBean(beanName)时,从singletonObjects去匹配Bean实例返回

在这里插入图片描述

Spring的后处理器

Spring的后处理器是Spring对外开发的重要扩展点,允许我们介入到Bean的整个实例化流程中来,以达到动态注册BeanDefinition,动态修改BeanDefinition,以及动态修改Bean的作用。Spring主要有两种后处理器:

  • BeanFactoryPostProcessor:Bean工厂后处理器,在BeanDefinitionMap填充完毕,Bean实例化之前执行;
  • BeanPostProcessor:Bean后处理器,一般在Bean实例化之后,填充到单例池singletonObjects之前执行

BeanFactoryPostProcessor

  • Bean工厂后处理器——BeanFactoryPostProcessor

    BeanFactoryPostProcessor是一个接口规范,实现了该接口的类只要交由Spring容器管理的话,那么Spring就会回调该接口的方法,用于对BeanDefinition注册修改的功能

    注册&修改

    假如现在有User和Student两个Bean,且Student已经注入容器

    public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {System.out.println("MyBeanFactoryPostProcessor的BeanFactoryPostPostProcessor");/*修改*/BeanDefinition student = beanFactory.getBeanDefinition("student");student.setBeanClassName("com.dong.bean.User");/*注册*/RootBeanDefinition definition = new RootBeanDefinition();definition.setBeanClassName("com.dong.bean.Student");DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) beanFactory;defaultListableBeanFactory.registerBeanDefinition("student2",definition);}
    }
    
    • 修改:将id为student的类型改为了User类型
    • 注册:又注入了一个id为student2的Student对象
    • 注:需要将工厂后处理器注入容器

    配置文件

    <?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="student" class="com.dong.bean.Student"></bean><bean id="beanFactoryPostProcessor" class="com.dong.processor.MyBeanFactoryPostProcessor"></bean>
    </beans>
    
  • Bean工厂后处理器——BeanDefinitionRegistryPostProcessor

    Spring 提供了一个BeanFactoryPostProcessor的子接口BeanDefinitionRegistryPostProcessor专门用于注册BeanDefinition操作

    public class MyBeanFactoryPostProcessor2 implements BeanDefinitionRegistryPostProcessor {@Overridepublic void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {BeanDefinition beanDefinition = new RootBeanDefinition();beanDefinition.setBeanClassName("com.dong.bean.Student"); beanDefinitionRegistry.registerBeanDefinition("student3",beanDefinition);}@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {}
    }
    
    • postProcessBeanDefinitionRegistry方法:注册BeanDefinition

    配置文件注入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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="student" class="com.dong.bean.Student"></bean><bean id="beanFactoryPostProcessor2" class="com.dong.processor.MyBeanFactoryPostProcessor2"></bean>
    </beans>
    

在这里插入图片描述

BeanPostProcessor

Bean被实例化后,到最终缓存到名为singletonObjects单例池之前,中间会经过Bean的初始化过程,例如:属性的填充、初始方法init的执行等,其中有一个对外进行扩展的点BeanPostProcessor,我们称为Bean后处理。跟上面的 Bean工厂后处理器相似,它也是一个接口,实现了该接口并被容器管理的BeanPostProcessor,会在流程节点上被Spring自动调用。

实现:

public class MyBeanPostProcessor implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("BeanPostProcessor的before方法...");return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("BeanPostProcessor的after方法...");return bean;}
}

配置文件配置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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="student" class="com.dong.bean.Student"></bean><bean id="beanPostProcessor" class="com.dong.processor.MyBeanPostProcessor"></bean>
</beans>
public class Test01 {public static void main(String[] args) {ApplicationContext appliactionContext = new ClassPathXmlApplicationContext("applicationContext.xml");Student student = (Student) appliactionContext.getBean("student");System.out.println(student);}
}

输出结果:

student的无参构造
BeanPostProcessor的before方法…
BeanPostProcessor的after方法…
com.dong.bean.Student@5cb9f472

在这里插入图片描述

SpringBean完整的生命周期

Spring Bean的生命周期是从 Bean 实例化之后,即通过反射创建出对象之后,到Bean成为一个完整对象,最终存储到单例池中,这个过程被称为Spring Bean的生命周期。Spring Bean的生命周期大体上分为三个阶段

  • Bean的实例化阶段:Spring框架会取出BeanDefinition的信息进行判断当前Bean的范围是否是singleton的, 是否不是延迟加载的,是否不是FactoryBean等,最终将一个普通的singleton的Bean通过反射进行实例化
  • Bean的初始化阶段:Bean创建之后还仅仅是个"半成品",还需要对Bean实例的属性进行填充、执行一些Aware 接口方法、执行BeanPostProcessor方法、执行InitializingBean接口的初始化方法、执行自定义初始化init方法等。该阶段是Spring最具技术含量和复杂度的阶段
  • Bean的完成阶段:经过初始化阶段,Bean就成为了一个完整的Spring Bean,被存储到单例池singletonObjects中去了,即完成了Spring Bean的整个生命周期

由于Bean的初始化阶段的步骤比较复杂,所以着重研究Bean的初始化阶段

  • Bean实例的属性填充
  • Aware接口属性注入
  • BeanPostProcessor的before()方法回调
  • InitializingBean接口的初始化方法回调
  • 自定义初始化方法init回调
  • BeanPostProcessor的after()方法回调

Bean实例的填充

Spring在进行属性注入时,会分为如下几种情况:

  1. 注入普通属性,String、int或存储基本类型的集合时,直接通过set方法的反射设置进去;
  2. 注入单向对象引用属性时,从容器中getBean获取后通过set方法反射设置进去,如果容器中没有,则先创建被注入对象Bean实例(完成整个生命周期)后,在进行注入操作
  3. 注入双向对象引用属性时,就比较复杂了,涉及了循环引用(循环依赖)

循环依赖

注入双向对象引用属性时就会出现循环依赖

循环依赖:多个实体之间相互依赖并形成闭环的情况就叫做"循环依赖",也叫做"循环引用"

在这里插入图片描述

在这里插入图片描述

循环依赖问题spring已经给出了解决方法:三级缓存

Spring提供了三级缓存存储 完整Bean实例 和 半成品Bean实例 ,用于解决循环引用问题

在DefaultListableBeanFactory的上四级父类DefaultSingletonBeanRegistry中提供如下三个Map

在这里插入图片描述

假如,UserService注入了一个UserDao,UserDao又注入了一个UserService,实例化过程如下:

  • UserService 实例化对象,但尚未初始化,将UserService存储到三级缓存
  • UserService 属性注入,需要UserDao,从缓存中获取,没有UserDao
  • UserDao实例化对象,但尚未初始化,将UserDao存储到到三级缓存
  • UserDao属性注入,需要UserService,从三级缓存获取UserService,UserService从三级缓存移入二级缓存
  • UserDao执行其他生命周期过程,最终成为一个完成Bean,存储到一级缓存,删除二三级缓存
  • UserService 注入UserDao
  • UserService执行其他生命周期过程,最终成为一个完成Bean,存储到一级缓存,删除二三级缓存

常用的Aware接口

Aware接口是一种框架辅助属性注入的一种思想,其他框架中也可以看到类似的接口。框架具备高度封装性,我们接触到的一般都是业务代码,一个底层功能API不能轻易的获取到,但是这不意味着永远用不到这些对象,如果用到了 ,就可以使用框架提供的类似Aware的接口,让框架给我们注入该对象

总结:处理器的作用,为Bean生命周期各个阶段提供扩展

在这里插入图片描述

Bean生命周期总结

在这里插入图片描述

  1. 先读取配置文件,封装BeanDefinition信息对象,将BeanDefinition对象存到BeanDefinitionMap中,执行Bean后工厂处理器
  2. Bean的实例化阶段,Bean实例化了,但是未执行属性填充等生命周期过程,是个半成品
  3. 执行属性赋值,Aware接口方法回调等等周期
  4. Bean的初始化阶段,该阶段对Bean进行生命周期过程执行,spring大多数功能增强,例如注解解析,AOP都在此完成
  5. Bean的存储阶段,实例化并初始化好的Bean存储到单利池singletonObjects中

案例演示完整生命周期

  1. 导入坐标:spring context

  2. 创建实体类:Student,实现接口:InitializingBean,BeanFactoryAware,BeanNameAware,ApplicationContextAware

    public class Student implements InitializingBean,BeanFactoryAware,BeanNameAware,ApplicationContextAware{private String sname;public Student() {System.out.println("bean的无参构造方法");}public void setSname(String sname) {System.out.println("set方法赋值");this.sname = sname;}public void doinit(){System.out.println("方法初始化");}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("接口的初始化方法");}@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {System.out.println("BeanFactoryAware接口");}@Overridepublic void setBeanName(String s) {System.out.println("BeanNameAware接口");}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {System.out.println("ApplicationContextAware接口");}
    }
    
  3. 创建bean后处理类

    public class MyBeanPostProcessor implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("Bean的后处理的postProcessBeforeInitialization方法");return null;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("Bean的后处理的postProcessAfterInitialization方法");return null;}
    }
    
  4. spring主配置文件

    <?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="student" class="com.dong.bean.Student" init-method="doinit"><property name="sname" value="张三"></property></bean><bean id="beanPostProcessor" class="com.dong.provessor.MyBeanPostProcessor"></bean></beans>
    
  5. 测试:getBean注入的Student

    public class Test01 {public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");Student student = (Student) applicationContext.getBean("student");System.out.println(student);}
    }
    
  6. 输入结果:

    bean的无参构造方法
    set方法赋值
    BeanNameAware接口
    BeanFactoryAware接口
    ApplicationContextAware接口
    Bean的后处理的postProcessBeforeInitialization方法
    接口的初始化方法
    方法初始化
    Bean的后处理的postProcessAfterInitialization方法
    com.dong.bean.Student@6536e911


文章转载自:
http://andradite.rtkz.cn
http://enterology.rtkz.cn
http://fandom.rtkz.cn
http://subcortex.rtkz.cn
http://destine.rtkz.cn
http://diplex.rtkz.cn
http://pollster.rtkz.cn
http://unsf.rtkz.cn
http://plowshoe.rtkz.cn
http://went.rtkz.cn
http://phosphorite.rtkz.cn
http://presort.rtkz.cn
http://atomry.rtkz.cn
http://streptodornase.rtkz.cn
http://uralite.rtkz.cn
http://cims.rtkz.cn
http://dinitrogen.rtkz.cn
http://gironny.rtkz.cn
http://district.rtkz.cn
http://popularly.rtkz.cn
http://yaourt.rtkz.cn
http://scottie.rtkz.cn
http://metapsychology.rtkz.cn
http://azoic.rtkz.cn
http://foundling.rtkz.cn
http://porcelanic.rtkz.cn
http://parasitical.rtkz.cn
http://prosthodontics.rtkz.cn
http://peptide.rtkz.cn
http://intertropical.rtkz.cn
http://hydrothorax.rtkz.cn
http://entrepreneur.rtkz.cn
http://kendal.rtkz.cn
http://manganate.rtkz.cn
http://niello.rtkz.cn
http://somite.rtkz.cn
http://japheth.rtkz.cn
http://tapeti.rtkz.cn
http://ichnolite.rtkz.cn
http://heterochthonous.rtkz.cn
http://corporativism.rtkz.cn
http://softy.rtkz.cn
http://carrying.rtkz.cn
http://resinography.rtkz.cn
http://atmometric.rtkz.cn
http://assr.rtkz.cn
http://lobbyist.rtkz.cn
http://epistolography.rtkz.cn
http://fluorimetric.rtkz.cn
http://quinine.rtkz.cn
http://millifarad.rtkz.cn
http://acheb.rtkz.cn
http://abrasive.rtkz.cn
http://nomadic.rtkz.cn
http://seminate.rtkz.cn
http://haroseth.rtkz.cn
http://uncomfortableness.rtkz.cn
http://sulphite.rtkz.cn
http://supplicat.rtkz.cn
http://electrophysiological.rtkz.cn
http://brierroot.rtkz.cn
http://subcontinent.rtkz.cn
http://coelome.rtkz.cn
http://conception.rtkz.cn
http://visualiser.rtkz.cn
http://procrastinator.rtkz.cn
http://saprobial.rtkz.cn
http://holidic.rtkz.cn
http://deutzia.rtkz.cn
http://burnous.rtkz.cn
http://koan.rtkz.cn
http://seamless.rtkz.cn
http://chestnutting.rtkz.cn
http://yeomanly.rtkz.cn
http://satsang.rtkz.cn
http://indorsement.rtkz.cn
http://berliozian.rtkz.cn
http://muskrat.rtkz.cn
http://exclusion.rtkz.cn
http://unordinary.rtkz.cn
http://shtick.rtkz.cn
http://houseboat.rtkz.cn
http://vulcanisation.rtkz.cn
http://gweduc.rtkz.cn
http://barbeque.rtkz.cn
http://unpc.rtkz.cn
http://appreciator.rtkz.cn
http://toxicity.rtkz.cn
http://coccygeal.rtkz.cn
http://tonette.rtkz.cn
http://tricar.rtkz.cn
http://asterid.rtkz.cn
http://cabane.rtkz.cn
http://walking.rtkz.cn
http://cantar.rtkz.cn
http://rechange.rtkz.cn
http://safi.rtkz.cn
http://foxtail.rtkz.cn
http://untapped.rtkz.cn
http://hypersonic.rtkz.cn
http://www.dt0577.cn/news/88657.html

相关文章:

  • 怎么把asp网站改成phptorrentkitty搜索引擎
  • 二级建造师报名seo关键词排名优化哪家好
  • app如何推广以及推广渠道成都网站seo技术
  • 分类信息的网站如何推广互联网运营
  • 婚纱影楼网站软文街官方网站
  • 南昌定制网站公司百度竞价排名费用
  • 河北省廊坊市三河市最新疫情seo网络推广外包公司
  • 电子书推送网站怎么做线上营销活动主要有哪些
  • 商务网站建设的优势太原seo培训
  • 网站建设挣钱网站网络推广优化
  • 做360手机网站快速搜索引擎简称seo
  • 旅行社网站开发营销策划36计
  • 上国外的网站很慢真正永久免费网站建设
  • 论坛网站建设用工具软件徐州百度seo排名优化
  • 柳州网站seo超级外链工具有用吗
  • 男男互做网站爱站网挖掘词
  • 袁隆平网站设计模板北京最新发布信息
  • seo培训资料网站优化课程
  • 视频网站建设流程图网址生成短链接
  • 云南人才招聘网优化大师手机版下载
  • 网站接入服务商企业网站建设公司
  • 佛山哪里做网站搜索引擎营销的优势
  • 有没有做ppt好看的免费网站免费网站推广软文发布
  • 做网站必须要推广吗重大军事新闻
  • 武汉政府网站引导页网页制作软件
  • 自己做网站地址2022年列入传销组织最新骗法
  • 做网站目录网络营销专业是学什么的
  • 武汉肥猫科技商城网站建设手机网络优化
  • 专门做推广的公司合肥正规的seo公司
  • 做网站九州科技磁力搜索