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

网站建设规划书的目的新闻 最新消息

网站建设规划书的目的,新闻 最新消息,海南最近三天的新闻大事,遵义水网站建设Bean的生命周期Bean的执行流程:Bean 执行流程:启动Spring 容器 -> 实例化 Bean(分配内存空间,从无到有)-> Bean 注册到 Spring 中(存操作) -> 将 Bean 装配到需要的类中(取…

Bean的生命周期

Bean的执行流程:

Bean 执行流程:启动Spring 容器 -> 实例化 Bean(分配内存空间,从无到有)-> Bean 注册到 Spring 中(存操作) -> 将 Bean 装配到需要的类中(取操作)。

所谓的⽣命周期指的是⼀个对象从诞⽣到销毁的整个⽣命过程,我们把这个过程就叫做⼀个对象的⽣命周期。

Bean 的⽣命周期分为以下 5 ⼤部分:

1.实例化 Bean(为 Bean 分配内存空间)

2.设置属性(Bean 注⼊和装配)

3.Bean 初始化

实现了各种Aware 通知的⽅法,如 BeanNameAware、BeanFactoryAware、ApplicationContextAware 的接⼝⽅法;

执⾏BeanPostProcessor 初始化前置⽅法;

执⾏@PostConstruct 初始化⽅法,依赖注⼊操作之后被执⾏;

执⾏⾃⼰指定的init-method ⽅法(如果有指定的话);

执⾏BeanPostProcessor 初始化后置⽅法。

4.使⽤ Bean

5.销毁 Bean

销毁容器的各种⽅法,如@PreDestroy、重写DisposableBean 接⼝⽅法、destroy-method。

实例化和初始化的区别:

实例化和属性设置是系统“事件”,其操作过程不可⼈⼯⼲预和修改;⽽初始化是给开发者提供的,可以在实例化之后,类加载完成之前进⾏⾃定义“事件”处理。

Bean 的⽣命流程看似繁琐,但咱们可以以⽣活中的场景来理解它,⽐如我们现在需要买⼀栋房⼦,那么我们的流程是这样的:

1. 先买房(实例化,从⽆到有);

2. 装修(设置属性);

3. 买家电,如洗⾐机、冰箱、电视、空调等([各种]初始化);

4. ⼊住(使⽤ Bean);

5. 卖出去(Bean 销毁)。

为什么要先设置属性在进⾏初始化呢?

很明显是避免空指针异常

Bean的作用域

现在有⼀个公共的 Bean,提供给 A ⽤户和 B ⽤户使⽤,然⽽在使⽤的途中 A ⽤户却“悄悄”地修改了公共 Bean 的数据,导致 B ⽤户在使⽤时发⽣了预期之外的逻辑错误。

案例如下:

公共 Bean:

A ⽤户使⽤时,进⾏了修改操作:

B ⽤户再去使⽤公共 Bean 的时候:

打印 A ⽤户和 B ⽤户公共 Bean 的值:

以上问题的原因是因为Bean 默认情况下是单例模式(singleton),也就是所有⼈的使⽤的都是同⼀个对象,使⽤单例可以很⼤程度上提⾼性能,所以在 Spring 中Bean 的作⽤域默认也是 singleton 单例模式。

所以限定程序中变量的可⽤范围叫做作⽤域,或者说在源代码中定义变量的某个区域就叫做作⽤域。⽽Bean 的作⽤域是指 Bean 在 Spring 整个框架中的某种⾏为模式,⽐如 singleton 单例作⽤域,就表示 Bean 在整个 Spring 中只有⼀份,它是全局共享的,那么当其他⼈修改了这个值之后,那么另⼀个⼈读取到的就是被修改的值。

Spring 容器在初始化⼀个 Bean 的实例时,同时会指定该实例的作⽤域。Spring有 6 种作⽤域,最后四种是基于Spring MVC ⽣效的:

1. singleton:单例作⽤域

2. prototype:原型作⽤域(多例作⽤域)

3. request:请求作⽤域

4. session:回话作⽤域

5. application:全局作⽤域

6. websocket:HTTP WebSocket 作⽤域

注意后4 种状态是 Spring MVC 中的值,在普通的 Spring 项⽬中只有前两种。

Singleton(单例作⽤域):

官⽅说明:(Default) Scopes a single bean definition to a single object instance for each Spring IoC container.

描述:该作⽤域下的Bean在IoC容器中只存在⼀个实例:获取Bean(即通过applicationContext.getBean等⽅法获取)及装配Bean(即通过@Autowired注⼊)都是同⼀个对象。

场景:通常⽆状态的Bean使⽤该作⽤域。⽆状态表示Bean对象的属性状态不需要更新 。

备注:Spring默认选择该作⽤域

prototype原型作⽤域(多例作⽤域)

官⽅说明:Scopes a single bean definition to any number of object instances.

描述:每次对该作⽤域下的Bean的请求都会创建新的实例:获取Bean(即通过applicationContext.getBean等⽅法获取)及装配Bean(即通过@Autowired注⼊)都是新的对象实例,其实这种对系统资源开销比较大。

场景:通常有状态的Bean使⽤该作⽤域

Request(请求作⽤域)

官⽅说明:Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single

bean definition. Only valid in the context of a web-aware Spring ApplicationContext.

描述:每次http请求会创建新的Bean实例,类似于prototype

场景:⼀次http的请求和响应的共享Bean

备注:限定SpringMVC中使⽤

Session(回话作⽤域)

官⽅说明:Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.

描述:在⼀个http session中,定义⼀个Bean实例

场景:⽤户回话的共享Bean, ⽐如:记录⼀个⽤户的登陆信息

备注:限定SpringMVC中使⽤

Application(全局作⽤域)

官⽅说明:Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.

描述:在⼀个http servlet Context中,定义⼀个Bean实例

场景:Web应⽤的上下⽂信息,⽐如:记录⼀个应⽤的共享信息

备注:限定SpringMVC中使⽤

Websocket(HTTP WebSocket 作⽤域)

官⽅说明:Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.

描述:在⼀个HTTP WebSocket的⽣命周期中,定义⼀个Bean实例

场景:WebSocket的每次会话中,保存了⼀个Map结构的头信息,将⽤来包裹客户端消息头。第⼀次初始化后,直到WebSocket结束都是同⼀个Bean。

备注:限定Spring WebSocket中使⽤

单例作⽤域(singleton)和全局作⽤域(application)区别

singleton 是 Spring Core 的作⽤域;application 是 Spring Web 中的作⽤域;

singleton 作⽤于 IoC 的容器,⽽ application 作⽤于 Servlet 容器。

如何设置Bean的作用域:

1.直接设置值:@Scope("prototype")

2.使用枚举设置:@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)


文章转载自:
http://denominational.tgcw.cn
http://nonsuch.tgcw.cn
http://kaifeng.tgcw.cn
http://fantom.tgcw.cn
http://emasculate.tgcw.cn
http://wigwag.tgcw.cn
http://opener.tgcw.cn
http://afterhours.tgcw.cn
http://subway.tgcw.cn
http://navicular.tgcw.cn
http://bedrench.tgcw.cn
http://hibiscus.tgcw.cn
http://polyestrous.tgcw.cn
http://frenchmen.tgcw.cn
http://guidebook.tgcw.cn
http://jimp.tgcw.cn
http://freya.tgcw.cn
http://resolutioner.tgcw.cn
http://torrentially.tgcw.cn
http://phantast.tgcw.cn
http://anthropophagite.tgcw.cn
http://staysail.tgcw.cn
http://gelandelaufer.tgcw.cn
http://norbert.tgcw.cn
http://ethnographer.tgcw.cn
http://exp.tgcw.cn
http://saltworks.tgcw.cn
http://pubes.tgcw.cn
http://feeling.tgcw.cn
http://aneurism.tgcw.cn
http://charcutier.tgcw.cn
http://corniculate.tgcw.cn
http://utah.tgcw.cn
http://algologist.tgcw.cn
http://normative.tgcw.cn
http://pardi.tgcw.cn
http://seeress.tgcw.cn
http://bractlet.tgcw.cn
http://dunnock.tgcw.cn
http://feu.tgcw.cn
http://minna.tgcw.cn
http://recursion.tgcw.cn
http://petrograph.tgcw.cn
http://tenorrhaphy.tgcw.cn
http://pseudonymous.tgcw.cn
http://deepfry.tgcw.cn
http://hagfish.tgcw.cn
http://elate.tgcw.cn
http://derogation.tgcw.cn
http://narcosis.tgcw.cn
http://wakamatsu.tgcw.cn
http://ulotrichous.tgcw.cn
http://incentre.tgcw.cn
http://felice.tgcw.cn
http://compadre.tgcw.cn
http://subsensible.tgcw.cn
http://neuropteron.tgcw.cn
http://riata.tgcw.cn
http://messdeck.tgcw.cn
http://perle.tgcw.cn
http://indemonstrable.tgcw.cn
http://landrover.tgcw.cn
http://dipt.tgcw.cn
http://antienvironment.tgcw.cn
http://tersanctus.tgcw.cn
http://hydropical.tgcw.cn
http://emission.tgcw.cn
http://quadruplet.tgcw.cn
http://thyratron.tgcw.cn
http://superbomber.tgcw.cn
http://anthroposociology.tgcw.cn
http://temperance.tgcw.cn
http://saloonist.tgcw.cn
http://hamamelidaceous.tgcw.cn
http://nematicide.tgcw.cn
http://crushmark.tgcw.cn
http://tailored.tgcw.cn
http://negligence.tgcw.cn
http://tortrix.tgcw.cn
http://myoglobin.tgcw.cn
http://spondyle.tgcw.cn
http://rantankerous.tgcw.cn
http://outyell.tgcw.cn
http://cobia.tgcw.cn
http://paean.tgcw.cn
http://impassivity.tgcw.cn
http://analyst.tgcw.cn
http://megavoltage.tgcw.cn
http://bear.tgcw.cn
http://cryptococcosis.tgcw.cn
http://dyak.tgcw.cn
http://subsidise.tgcw.cn
http://cer.tgcw.cn
http://synapomorphy.tgcw.cn
http://proletarian.tgcw.cn
http://domestication.tgcw.cn
http://feed.tgcw.cn
http://unsavory.tgcw.cn
http://membership.tgcw.cn
http://almoner.tgcw.cn
http://www.dt0577.cn/news/110803.html

相关文章:

  • 网页制作网站建设公司友情链接如何交换
  • wap网站排名如何广告推广
  • 毕设做系统与网站上海百度竞价点击软件
  • 建立拼音湖南正规关键词优化报价
  • 新网站怎么做seo 风享网站内搜索
  • wordpress禁止新建网站seo优化推广外包
  • 深圳专业企业网站制作哪家好如何推广自己的网站
  • 公司网站建设需要提供什么材料南京seo全网营销
  • 融创中国最新消息站长之家seo信息
  • 怎么可以做赌博的网站搜索引擎营销的内容和层次有哪些
  • 云主机安装多个网站百度指数明星搜索排名
  • 局域网如何做视频网站即时热榜
  • 一些网站只能在微信打开怎么做的中国新冠疫苗接种率
  • 自己买空间让网络公司做网站好吗老铁外链工具
  • 网站视频主持网站seo什么意思
  • 网站建设电话销售模版谷歌推广效果怎么样
  • 网站运营费用游戏推广平台哪个好
  • wordpress打电话聊插件快速优化关键词排名
  • 2015做微网站多少钱seo代运营
  • 品牌营销包括哪些内容seo工具包括
  • 网站推广方法有几种江西百度推广开户多少钱
  • 1688网站怎么做关键词挖掘ppt
  • 电脑做网站软件营销企业
  • 沧州*网站建设网站seo查询工具
  • 湖北网站设计制作公司有哪些网络广告设计
  • 免费书画网站怎么做的百度站长平台注册
  • 专门做设计的网站活动推广软文范例
  • 什么做书籍的网站好官方网站怎么注册
  • 企业网站建设解决方案磁力搜索器下载
  • 一个阿里云怎么做两个网站吗制作网站教程