Spring学习之一 bean的作用域 详细讲解

Fastp实战指南:高效处理fastq数据的质控技巧 本文是Fastp实战指南,详细介绍了如何使用Fastp工具高效处理fastq数据的质控技巧。从数据清洗的必要性、基础命令解析,到高级参数定制、UMI数据处理及批量自动化流程,提供一站式解决方案。Fastp以其“All-in-One”的快速质控能力,帮助用户确保测序数据质量,为后续生物信息学分析奠定可靠基础。 阅读详情

                             Spring Bean的作用域

 Spring是一个轻量级的开源应用开发框架,广泛应用于企业开发中,并且受到广大开发人员的喜欢。本篇只是针对spring中bean的作用域进行学习和总结。

   Bean作用域的分类

    bean的作用域分为五类:singleton ,prototype, request, session, global session 。

     Singleton 作用域

    此配置项是spring的默认项,也叫单例,在每个springIOC容器中一个bean定义对应一个对象实例,不同容器中不同。

    实验如下:bean配置如下


    bean的获取方式如下


结果如下:


如果将如上配置改为下面:

<bean id="myBean" class="com.pactera.spring.TestSingleton"  scope="singleton"/> 则结果仍然与上面一致。

prototype 作用域

    该作用域表示每次从工厂中获取bean时,都会创建一个新的bean。

    本例实验我们用包自动扫描和注解得方式去实现bean的定义


开启包注解自动扫描


获取bean


运行后结果如下:


request 作用域

该作用于只有在Web应用中才会起作用。表示每个请求中会创建一个实例化对象。

我们需要搭建一个springWebMVC框架,并声明一个bean实现ApplicationContextAware用来获取容器中的bean。


Controller层中要获取容器中的作用域声明为request的Bean.


MyStatus的注解:


运行服务器后两次请求http://localhost:8180/springMVC/test/hi.do(我的前端控制器中url-pattern配置的为*.do).日志结果显示如下:


Session作用域

和request作用域一样,也是只有在web应用中才起作用。每个session中同一个实例,不同session不同实例。

bean注解声明作用域为session


调用类


session过期时间设置为1分钟


启动服务器后调用请求:

两次请求间隔超过1分钟的结果如图:


结果证明不同session中实例不同。

两次请求时间不超过1分钟结果如图:


结果证明相同session中实例相同。

global session 作用域

此注解仅仅适用于portlet的web应用中,类似于http session。portlet web应用中的全局session可以被不同的portlet共享,同一个全局session中实例是同一个。由于不常用,所以不做代码层面的学习分析,仅作了解。

以上是对spring中bean的作用域的简单分析,后续会做源码分析。


    

    

    

    

ATECC508A芯片开发笔记(三):获取508A串号、随机数源码及I2C抓包分析 ATECC508A芯片开发笔记(三):读取508A串号、获取随机数源码及I2C抓包分析 APP层函数编写及源码分析 At508_GetSernum() GetSernum抓包记录 At508_GetRandom( ) GetRandom抓包记录 经过前两章节,CryptoAuthlib库已经成功适配在代码中了,下面简单测试一下API函数,并分析下I2C通讯流程: APP层函数编写及源码分析利用li 阅读详情

相关推荐

Windows下Python调用D435i相机避坑指南:从SDK版本选择到RealSense Viewer报错解决

本文详细介绍了在Windows环境下使用Python调用Intel D435i相机的完整避坑指南。重点解决了SDK版本选择问题,推荐使用稳定的Intel RealSense SDK 2.54.2版本,并提供了从环境配置、常见报错解决到实际应用案例的全流程实践指导,帮助开发者高效完成相机部署与开发。

palm99的专栏 923

Spring Bean的五个作用域

Spring支持五个作用域:singleton、prototype、request、session、global session

Ahuuua的博客 1万+

Jetson Orin边缘语音AI实战:用Riva ASR/TTS + Llama 2打造离线智能语音助手

本文详细介绍了如何利用NVIDIA Jetson Orin的边缘计算能力,结合Riva ASR/TTS语音引擎和Llama 2大语言模型,构建完全离线的智能语音助手。通过硬件优化、量化部署和性能调优,实现在网络不稳定环境下的高效语音交互,适用于智能家居、车载系统等场景。

weixin_29053383的博客 242

再谈Spring(一):Bean的作用域

(尊重劳动成果,转载请注明出处:https://yangwenqiang.blog.youkuaiyun.com/article/details/91477092冷血之心的博客) 今天准备开始一个Spring系列文章,主要记录一些学习和使用Spring过程中遇到的小知识,所以我起了《再谈Spring》的名字。 Spring是一个轻量级的IOC和AOP容器框架。是为Java应用程序提供基础性服务的一套框架,目...

冷血之心的博客 2737

【数据库】银行/信贷业务测试工作中涉及的数据库表格

银行/信贷业务测试工作中涉及的数据库表格,金融信贷测试人必看!

孤之鹜的博客 1169

网络攻防实验——网络监听

利用CAIN实现ARP监听。

Trieyes_of_feng的博客 3196

Spring中Bean的作用域

Spring 中 bean 的 5 种作用域,然后详细介绍 singleton 和 prototype 这两种最常用的作用域。

unber的博客 5911

绕开华为通道,获取解锁码解锁BootLoader

刷机有风险,操作需谨慎,如果因为各种因素导致手机变砖无法开机,本教程概不负责,望悉知 前言 这篇文章的大多内容是根据我自己找的一篇文章《绕开华为通道,获取解锁码解锁BootLoader》来编写的,里面的大部分内容是文章的原内容。下面的都是我参考文章中做了一些小部分的修改,增加了我华为P8手机的一些操作,下面开始讲述我的解锁过程。 因为华为官方在2018年的七八月份吧,关闭了解锁码的申请通道,这...

Martin.Mu `s Special Column 5万+

Spring 框架中 Bean 的 6 种作用域!

在Spring中,那些组成应用程序的主体由Spring IoC容器所管理的对象,被称之为bean。简单地讲,bean就是由IoC容器初始化、装配及管理的对象。在中,总共定义了6种bean的作用域,其中有4种作用域只有当应用为web应用的时候才有效,并且Spring还支持自定义作用域。

Z99263的博客 3071

电池保护板为什么采用NTC热敏电阻而不是PTC热敏电阻?

R25=10K,即环境温度25°时,阻值是10K),温度变化1°C,阻值相应地明显变化,温度响应很灵敏,也就能精准地反向换算出阻值对应的温度。另外,还有功率型NTC热敏电阻,其主要用在开关电源设计中,串联在市电输入上,抑制浪涌电流。电池保护板上的NTC热敏电阻(NTC一端接地,另一端接电池充电控制IC管脚),在电池温度上升时,NTC热敏电阻温度也会随之上升,从而NTC电阻值会下降,因此电池充电控制IC用ADC检测的电压值就降低,当检测到的电压值达到下限阈值时,电池充电控制IC就会停止对电池的充电或放电。

电子绿洲的博客 5626

Matlab双目相机标定

1 概述 现在有许多双目相机在出厂时就已经标定好了,用户拿到手后可以直接使用,例如Intel Realsense系列。但是有些相机出厂的时候并没有完成标定工作,因而这个时候就需要我们自己来标定。由于笔者曾改装过一个双目相机,最远可以测至50m,因此有一定的心得体会想给大家分享一下。本文主要介绍双目相机标定的整个过程,以及导出标定数据的方法。 相机标定主要分为手动标定和自动标定,手动标定比较繁琐,这里主要介绍基于matlab工具箱的自动标定方式来对双目相机进行标定。具体的相关标定细节也可以参照这篇博客:m

weixin_46133643的博客 2万+

springBean的作用域

bean的作用域和生命周期

一条游向寒江的鱼 1373

第十五章数据管理成熟度评估【6’】(DAMA-CDGA 2022年以后历年模拟题真题汇总,基本包含所有考点。)

第十五章数据管理成熟度评估【6’】(DAMA-CDGA 2022年以后历年模拟题真题汇总,基本包含所有考点。)

JasonH2021的博客 403

spring bean作用域_2019最新Spring面试题含答案——《Spring Beans》(建议收藏)

1.什么是Spring beans?Spring beans 是那些形成Spring应用的主干的java对象。它们被Spring IOC容器初始化,装配,和管理。这些beans通过容器中配置的元数据创建。比如,以XML文件中 的形式定义。Spring 框架定义的beans都是单件beans。在bean tag中有个属性”singleton”,如果它被赋为TRUE,bean 就是单件,否则就是一个 ...

weixin_39843151的博客 267

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

深度学习工具包 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")

Spring Bean的作用域

在Spring中,bean作用域用于确定哪种类型的bean实例应该从Spring容器中返回给调用者。 目前Spring Bean的作用域或者说范围主要有五种。 作用域 描述 singleton 在spring IoC容器仅存在一个Bean实例,Bean以单例方式存在,bean作用域范围的默认值。 prototype 每次从容器中调用Bean时,都返回一个新的实例,即每...

forever-and-ever的博客 7万+

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

Spring Bean的作用域以及注解使用

点击上方 Java学习之道,选择 设为星标每天12点,干货准时奉上!Spring Bean的作用域Spring Bean 的作用域即为对象的作用范围。Spring Bean的作用域由配置项...

Java学习之道 4973

Spring学习(十五)Spring Bean 的5种作用域介绍

Spring Bean 中所说的作用域,在配置文件中即是“scope” 在面向对象程序设计中作用域一般指对象或变量之间的可见范围。 而在Spring容器中是指其创建的Bean对象相对于其他Bean对象的请求可见范围。 在Spring 容器当中,一共提供了5种作用域类型,在配置文件中,通过属性scope来设置bean的作用域范围。 1.    singleton: 当Bean的

wangxin1248的博客 1万+

Spring 基于注解设置 Bean 的作用域

Spring 中 Bean 的作用域 前言 在默认情况下,Spring应用上下文中所有的 Bean 都是作为以单例的形式创建的。也就是说,不管给定的一个 Bean 被注入到其他 Bean 多少次,每次所注入的都是同一个实例。 在大多数情况下,单例的 Bean 是很理想的方案,初始化和垃圾回收对象实例所带来的成本只留给一些小规模任务。在这些任务中,对对象的保持无状态并且在应用中反复重...

Carey丶zsh 的博客 9142

java中bean的作用域有哪些_Spring中Bean的作用域

本节先简单介绍了 Spring 中 bean 的 5 种作用域,然后详细介绍 singleton 和 prototype 这两种最常用的作用域。作用域的种类Spring 容器在初始化一个 Bean 的实例时,同时会指定该实例的作用域。Spring3 为 Bean 定义了五种作用域,具体如下。1)singleton单例模式,使用 singleton 定义的 Bean 在 Spring 容器中只有一个...

weixin_35106801的博客 2169

Spring Bean 的作用域

Bean 作用域定义了 Bean 在 Spring 容器中的生命周期和可见范围。不同的作用域决定了 Bean 的创建时机、存储位置和销毁时机。Spring 提供了几种常用的 Bean 作用域,每种作用域都有其特定的使用场景。除了上述内置作用域外,Spring 还允许开发者定义自定义作用域。自定义作用域需要实现Scope接口,并注册到 Spring 容器中。

FireFox1997 1630

Spring中bean的作用域

Bean简介:    在Spring中,那些组成你应用程序的主体(backbone)及由Spring IoC容器所管理的对象,被称之为bean。 简单地讲,bean就是由Spring容器初始化、装配及管理的对象,除此之外,bean就与应用程序中的其他对象没有什么区别了。 而bean定义以及bean相互间的依赖关系将通过配置元数据来描述。Bean的作用域创建一个bean定义,其实质是

fengruifang的专栏 3万+

Spring bean的作用域详解

Spring bean的作用域详解!

weixin_68093610的博客 1501

Spring中Bean的五种作用域

Bean简介: 在Spring中,那些组成你应用程序的主体(backbone)及由Spring IoC容器所管理的对象,被称之为bean。 简单地讲,bean就是由Spring容器初始化、装配及管理的对象,除此之外,bean就与应用程序中的其他对象没有什么区别了。 而bean定义以及bean相互间的依赖关系将通过配置元数据来描述。 Bean的作用域 创建一个bean定义,其实质是用该bean定义对应的类来创建真正实例的“配方(recipe)”。把bean定义看成一个配方很有意义,它与class很类似,只根据

dxmdxmw的博客 1万+

关于spring中Bean的四种作用域

Bean的作用域 在Spring中,那些组成应用程序的主体及由Spring IoC容器所管理的对象,被称之为bean。简单地讲,bean就是由IoC容器初始化、装配及管理的对象 准备工作 新建User类 public class User { private String name; private int age; public User() { } public User(String name, int age) { this.nam

qq_40631968的博客 1714

Spring Bean 作用域

常见的 4 种作用域 Spring Bean 的默认作用域是 Singleton。一般通过注解 @Scope 自定义Bean的作用域。 Singleton 整个应用中,只创建一个实例。(默认) ConfigurableBeanFactory.SCOPE_SINGLETON Prototype 每次注入或通过Spring Application Context 获取时,都...

一个被IT搞的 1268

Spring Bean 的五个作用域

一、作用域(scope)和生命周期 作用域 作用域限定了 Bean 的作用范围。在 Spring 配置文件定义 Bean 时,通过声明 scope 配置项,可以灵活定义 Bean 的作用范围。例如,当希望每次 IOC 容器返回的 Bean 是同一个实例时,可以设置 scope 为 singleton;当希望每次 IOC 容器返回的 Bean 实例是一个新的实例时,可以设置 scope 为 prototype。scope 配置项有 5 个属性,用于描述不同的作用域:singleton、prototype、re

ChineseSoftware的博客 3044
上一篇: 浅谈小组管理
下一篇: JVM学习之二 java对象的创建
tony_code_2017
博客等级 码龄9年 34粉丝 58原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值