Spring创建Bean过程简要总结

spring 深入了解bean的创建过程 一、创建bean之初 spring调用 beanFactory.preInstantiateSingletons()此方法完成整个bean生产以及依赖注入,那么在生成bean之前,spring做了哪些事呢? 1.盘点bean定义是否是bean工厂 遍历所有beandefinition 非懒加载以及非抽象bean,单例,去执行AbstractBeanFactory.isFactoryBean(beanName) 方法判断是否是重写 SmartFactoryBean接口的isEagerI... 阅读详情

        经过了前面两个大篇幅的对spring源码的大致梳理,大致上可以总结出spring创建bean的一些关键逻辑。本文是对之前文章的一个简要的总结,并不是spring创建bean的完整过程,我大致梳理流程如下:

  • 创建ClassPathXmlApplicationContext对象,传入spring配置文件
  • 在ClassPathXmlApplicationContext的构造方法中调用refresh()方法
  • refresh()方法中有两个重要步骤,
    • 一个是创建BeanFactory的逻辑,调用obtainFreshBeanFactory()方法,
    • 一个是创建bean的方法finishBeanFactoryInitialization()方法
  • 在创建beanFactory的过程中,有两个步骤:
    • 创建容器BeanFactory
    • 读取并解析配置文件中的bean定义信息,并将bean定义信息存储在beanDefinition对象中,并以beanName作为Key,BeanDefinition最为value保存在beanDefinitionMap中,并且将beanName保存在beanDefinitionNames的list集合中
  • 执行完obtainFreshBeanFactory()后,还会进行一些bean实例化前的步骤,例如对BeanFactory进行一些属性初始化等等操作
  • 前置方法结束后,进行bean的创建逻辑,创建是调用finishBeanFactoryInitialization()方法
  • 创建过程中,首先是循环beanDefinitionNames集合
  • 根据bean查询beanDefinition,并将beanDefinition转化为RootBeanDefinition对象
  • 然后调用getBean方法,getBean方法中调用doGetBean方法
  • doGetBean方法中调用getSingleton()方法获取bean对象
    • 先会在singletonObjects的map集合中获取,如果获取不到,调用ObjectFactory的createBean()方法
  • createBean方法中,根据RootBeanDefinition对象中的信息,得到字节码对象Clas。然后根据字节码对象得到构造器对象
  • 通过BeanUtils的instantiateClass()方法通过反射来创建对应的bean对象

以上的流程就是这几天看spring源码后,对spring创建流程的一个总结,欢迎大神指点!

spring中bean的创建过程以及底层原理 类作为value 这样,但调用context.getBean("userService")时,就可以根据"userService"找到。当我们调用context.getBean("userService")时,就会去创建一个对象,但是getBean方法。都是共同的,后续课程我们会着重将AnnotationConfigApplicationContext的底层实现,而AppConfig.class和spring.xml一样,表示Spring的配置,比如可以指定扫描路径,可以。这样下次getBean时就可。 阅读详情

相关推荐

Jetson Orin Nano Super开发板实战:视觉模型部署与性能优化指南

本文详细介绍了Jetson Orin Nano Super开发板在边缘AI视觉模型部署与性能优化的完整实战指南。内容涵盖JetPack 6.1环境配置、Docker容器化部署、YOLO11及Transformer模型的TensorRT优化技巧,并通过实测数据展示了性能提升与功耗管理策略,为开发者提供高效的视觉AI部署解决方案。

7h6j5k4l3的博客 1196

面试官:说说 Spring 源码中 BeanFactory 的创建流程?

一、获取BeanFactory子流程 时序图如下 二、BeanDefinition加载解析及注册子流程 1.该子流程涉及到如下几个关键步骤 Resource定位:指对BeanDefinition的资源定位过程。通俗讲就是找到定义Javabean信息的XML文件,并将其封装成Resource对象。 BeanDefinition载入:把用户定义好的Javabean表示为IoC容器内部的数据结构,这个容器内部的数据结构就是BeanDefinition。 注册BeanDefinition到 ..

Java烂猪皮 410

MES系统领料申请、出库、确认到料各环节的处理

在WMS和MES系统协同的车间管理中,核心分工原则为:MES负责生产需求发起(领料申请)和结果确认(到料验收),WMS执行库存管理和物流作业。具体流程为:1)MES根据生产需求生成领料单推送至WMS;2)WMS完成拣货出库并调度AGV配送;3)车间在MES确认到料后解锁生产工序。系统边界严格限定:MES不参与库存/库位管理,WMS不干预生产决策。特殊场景(紧急补料、缺料异常等)通过标准化接口流程处理,确保生产与物流高效协同的同时保持账实一致。

做有价值的事,积累有价值的笔记! 583

Spring源码系列:BeanFactory的创建

Spring的Ioc容器其实就是一个bean的关系网,依赖于core,bean,context三个组件来构建的。在spring中最核心的就是对于bean的管理。而bean又依托于我们的容器。本文将从顶层分析一下spring中beanFactory的具体创建过程,为后续的bean的生命周期提供一个基础。 BeanFactory的继承体系 从上图可以看到,BeanFactory有三个子类: Lis...

weixin_34376562的博客 372

金蝶ERP元数据解析:字段属性与表结构映射实战

本文深入解析金蝶ERP元数据的核心价值与应用实践,详细介绍了字段属性与表结构映射的关键技术。通过缓存获取与工具类直连两种方案对比,结合实际案例展示元数据在数据库校验、动态表单生成等场景的应用,帮助开发者高效解决二次开发中的常见问题。

weixin_33738555的博客 433

揭秘大型语言模型:从神经元到思维透明的奇妙旅程

这篇博客带你走进大型语言模型的内部世界,从神经元的小任务,到功能向量的团队合作,再到层间的协作和思维的透明化。掌握这些知识,能让你在开发中更高效地优化AI,在研究中更深入地探索模型的奥秘。建议多动手实践,把这些理论变成自己的能力!

数据与算法架构提升之路专栏 1519

Spring 源码详解之Spring BeanFactory 是怎么创建的?

Spring创建 BeanFactory 的方式 按照Bean的配置方式手动创建可以分为两种: 使用XMl配置的Bean 这种方式使用xml配置文件配置Bean的信息并且设置扫描的路径,扫描到的包可以使用注解进行配置Bean信息,一般来说手动创建BeanFactory容器的实现类为ClassPathXmlApplicationContext和SystemFileXmlApplicationContext,设置xml的路径即可创建出IOC容器。 例如: ...

m0_69860228的博客 1359

openharmony之启动恢复子系统详解

需求: 添加一个名为my_service的自定义服务步骤1: 创建服务配置],],"env": [步骤2: 添加到主配置],"jobs": ["cmds": [步骤3: 验证配置# ✅ 检查配置格式 jq . /system/etc/init/my_service.cfg # 🔄 重新加载配置 begetctl reload # 📊 验证服务状态 begetctl dump_service my_serviceOpenHarmony init进程的完整生命周期。🔄 三阶段启动模型⚙️ 配置体系。

m0_67505153的博客 597

Spring技术专栏---beanFactory创建流程

一:BeanFactory创建流程图 二:核心代码讲解 2.1 加载beanDefinations入口 public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException { super(parentBeanFactory); this.reader.loadBeanDefinitions(resource); } 通过XmlBeanDefinition...

mclongyi的专栏 410

【Unity实战篇 】| 游戏中实现镂空遮罩效果【矩形、圆形镂空遮罩】

本文来写一下怎样在Unity中完成一个 镂空遮罩 的效果。 镂空遮罩 比较常用的有两种:矩形镂空遮罩、圆形镂空遮罩。 镂空遮罩 在游戏中常被当做新手引导必备的内容,可以给玩家更多不一样的体验。 下面就一起来看一下怎样做出这种效果吧!

努力前行,总会成为自己心中的那道光 1万+

SpringBoot 整合 Drools:从环境搭建到复杂业务落地,精通规则引擎实战指南

本文介绍了Drools规则引擎的基础概念及其与SpringBoot的整合方法。首先概述了规则引擎的核心价值,包括业务规则解耦、可维护性提升等。重点讲解了Drools的核心组件(规则、事实、会话等)及其工作原理。随后详细说明了在SpringBoot项目中集成Drools的步骤,包括添加依赖、配置自动加载规则文件、创建KieContainer等关键配置类。文章还提供了完整的DRL规则文件示例和SpringBoot集成配置代码,为开发人员提供了从理论到实践的完整指导。通过这种方式,可以轻松实现业务规则与系统代码的

本博客聚焦 YOLOv11 全流程落地,涵盖架构优化、数据集处理、训练技巧与多场景部署,兼及国产数据库、Java 开发与 AI 模型应用,内容兼顾理论与工程实践,为开发者提供系统干货,助力高效提升技术能力。 1431

Spring容器创建过程——BeanFactory的创建及预准备

一、BeanFactory预准备 1、创建Spring容器对象 2、调用refresh()方法刷新容器 refresh()方法会在刷新前,调用prepareRefresh()方法进行刷新前的预处理工作 prepareRefresh()方法会调用initPropertySources()方法初始化一些属性设置 执行完initPropertySources()方法初始化属性之后,还可以调用validateRequiredProperties()方法进行属性的合法校验 最后prepareRefresh(

qq_39794062的博客 742

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

spring创建bean过程

一.前言 spring作为JAVAEE最核心的框架,是每一个java开发者所必须掌握的,非常重要,本篇从一个简单例子入手,由浅入深的分析spring创建bean的全过程,目标在于彻底搞懂spring原理,方便以后整合其他框架 二.测试代码 /** * * * @author <a href="mailto:2256222053@qq.com">zc</a> * @Date 2022/4/3 0003 14:45 */ @Lazy public class Cr

qq_41595149的博客 2185

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Spring之Bean创建过程

Spring提供了xml、注解、JavaConfig多种方式来配置bean,不论何种方式,Spring最终都会将bean封装成BeanDefinition对象,Spring创建bean的依据也是通过BeanDefinition来完成的。当我们调用getBean()方法获取bean实例时,不管是单例bean还是原型bean,首次调用时容器内都不存在可用的bean实例,这时就不得不去创建bean了,我们分析下Spring创建bean的过程。

程序员小潘 3802

Spring的Bean创建过程

spring将内部管理的诸多对象称为一个个bean,而这些bean的创建流程大致分为两个大阶段: 下面本人将从上述两个阶段来阐述bean的完整创建过程。 在对象进行创建之前,spring容器需要了解所创建的对象的信息,才能在后续阶段根据了解的信息创建bean对象。这些信息即是实际工作中我们为对象所写的配置信息,它们一般以xml文件、properties文件和注解的形式存在于我们的项目之中。 一个以xml文件为存储形式的bean配置文件如下: 因此,

Yuan_Su_的博客 2951

深入源码级别看spring bean创建过程

我们通常聊到spring bean的生命周期,大多是从网上找帖子背些基本概念,这样我们学到的东西是不够直观清晰的,这篇文章我就试着从源码级别来讲清楚bean的创建过程。

u012453843的专栏 1094

spring bean的创建过程

spring 中bean的生命周期<简单版本,后续待更新>

qq_40866844的博客 981

Spring Bean的创建过程

本文带着如下问题,分析下spring bean的创建过程,spring是怎么创建bean?ioc又体现在哪里,创建bean的过程中做了些什么,aop的代理对象是什么时候生成的? 1、应用程序的启动 现在的工程大部分都是springboot工程,SpringBoot可以轻松创建独立的、生产级的、基于Spring的应用程序,SpringBoot应用程序可以通过main方法轻松启动,如下: public static void main(Strin...

hyp1987的专栏 707

Spring Bean创建过程解析

时序图 先上时序图帮助理解 Bean 实例创建过程 解析过程 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(&amp;quot;applicationContext.xml&amp;quot;); Person person = (Person) applicationContext.getBean(&amp;quot;person&amp;quot;);...

寒武没有纪 1万+

探索Spring创建Bean过程

Spring对于Java程序员来说,是必须要掌握的一个框架。工作中我们基本上也是使用spring来进行日常的开发。却对项目中的bean创建过程并不是很了解。我自己就属于这种情况。也在网上看过很多的视频,但是都没有将看过的知识点进行总结。导致对于源码总是一知半解的状态。本篇文章用来记录我在探索spring创建bean的源码总结。 1. 准备工作 阅读源码,个人觉得最好的方法是DEBUG断点调试法,一步一步的看下去,最终找到自己想要的知识,在DEBUG之前就需要准备一个spr...

cssweb_sh的博客 825

Spring 创建 Bean 全过程

Spring 创建 Bean 的全过程。

imonkeyi的博客 867

Spring源码分析之Bean的创建过程详解

前文传送门: Spring源码分析之预启动流程 Spring源码分析之BeanFactory体系结构 Spring源码分析之BeanFactoryPostProcessor调用过程详解 本文内容: 在IOC中,是如何通过beanDefition创建出一个bean的? 各BeanPostProcessor在这过程中扮演的角色,调用时机? 话不多说,直接正题走起,上图! 下面是bean创建过程的大致流程图,本文将以图中顺序进行逐步源码分析,小伙伴亦可与图中流程边对照边品食 原矢量图地址:https://

雕爷的架构之路 960

spring 创建bean的过程

object.class – > 调用无参构造方法 --> 对象 --> 依赖注入(属性赋值) --> 初始化前(@PostConstruct)–>初始化(InitializingBean)–>初始化后(AOP)–>bean。

weixin_45849726的博客 1144

Spring 源码之Spring Bean 的创建过程

继续上一篇Spring Bean的创建过程的解读,上一篇介绍了Spring在创建过程中doGetBean方法,在执行过程中会调用getSingleton方法并且设置一个lambda表达式,这个lambda表达式是ObjectFactory的实现,当调用这个接口的getObject方法时就会执行到createBean方法,在createBean方法中会进行bean类型的解析,并且会进行方法覆盖的设置,当我们配置了如:lookup-method或者replace...

m0_69860228的博客 269

Spring 源码的Spring Bean 的创建过程

Spring Bean的创建刚开始进行了一些准备工作,比如转换服务的初始化,占位符解析器的初始化,BeanDefinition元数据的冻结等操作,都是为了在创建Bean的过程中保证Bean的正确的创建,接下来开始进行对Bean的创建进行解析。 Bean 的创建步骤 在Spring源码中对Bean的创建遵循一个步骤就是:getBean-->doGetBean-->createBean-->doCreateBean,常规的Bean的创建过程都是按照...

m0_69860228的博客 206
上一篇: Spring中BeanDefinition的探索
下一篇: Spring使用三级缓存解决Bean的循环依赖流程简括
~小爷.
博客等级 码龄9年 26粉丝 49原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值