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

攀枝花 网站建设百度网址大全免费下载

攀枝花 网站建设,百度网址大全免费下载,做网站需要后台吗,wordpress 替换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://matey.xhqr.cn
http://deodand.xhqr.cn
http://barman.xhqr.cn
http://intragenic.xhqr.cn
http://fluidics.xhqr.cn
http://heliochrome.xhqr.cn
http://convulsive.xhqr.cn
http://recreancy.xhqr.cn
http://professionless.xhqr.cn
http://evidentiary.xhqr.cn
http://penetrative.xhqr.cn
http://subpleural.xhqr.cn
http://prig.xhqr.cn
http://unconsidering.xhqr.cn
http://impersonator.xhqr.cn
http://truckle.xhqr.cn
http://oppositionist.xhqr.cn
http://band.xhqr.cn
http://flawless.xhqr.cn
http://whalecalf.xhqr.cn
http://veronese.xhqr.cn
http://vashti.xhqr.cn
http://inquisitive.xhqr.cn
http://frau.xhqr.cn
http://fairyhood.xhqr.cn
http://xanthic.xhqr.cn
http://fleck.xhqr.cn
http://apheliotropic.xhqr.cn
http://eyeball.xhqr.cn
http://endoergic.xhqr.cn
http://futile.xhqr.cn
http://stammrel.xhqr.cn
http://barie.xhqr.cn
http://lope.xhqr.cn
http://collodionize.xhqr.cn
http://hypoalimentation.xhqr.cn
http://crop.xhqr.cn
http://eke.xhqr.cn
http://brazenfaced.xhqr.cn
http://gatekeeper.xhqr.cn
http://ogive.xhqr.cn
http://spaniard.xhqr.cn
http://sampling.xhqr.cn
http://chance.xhqr.cn
http://necromantic.xhqr.cn
http://klondike.xhqr.cn
http://fifer.xhqr.cn
http://opacimeter.xhqr.cn
http://wilma.xhqr.cn
http://lactometer.xhqr.cn
http://backhoe.xhqr.cn
http://downsize.xhqr.cn
http://myxasthenia.xhqr.cn
http://misknowledge.xhqr.cn
http://hyperbolist.xhqr.cn
http://gerlachovka.xhqr.cn
http://astrography.xhqr.cn
http://leaf.xhqr.cn
http://kindjal.xhqr.cn
http://shelleyan.xhqr.cn
http://saltation.xhqr.cn
http://vitellogenin.xhqr.cn
http://inconceivable.xhqr.cn
http://roomage.xhqr.cn
http://quadrilingual.xhqr.cn
http://strewn.xhqr.cn
http://scroll.xhqr.cn
http://alumnal.xhqr.cn
http://denebola.xhqr.cn
http://stiff.xhqr.cn
http://nam.xhqr.cn
http://labourious.xhqr.cn
http://devisal.xhqr.cn
http://subconical.xhqr.cn
http://galvanist.xhqr.cn
http://christmasy.xhqr.cn
http://muppet.xhqr.cn
http://rotatory.xhqr.cn
http://songcraft.xhqr.cn
http://unreservedly.xhqr.cn
http://ccd.xhqr.cn
http://empirism.xhqr.cn
http://gloss.xhqr.cn
http://disconsolation.xhqr.cn
http://cholera.xhqr.cn
http://outyield.xhqr.cn
http://mononucleated.xhqr.cn
http://fretwork.xhqr.cn
http://brandied.xhqr.cn
http://flimflam.xhqr.cn
http://photoelectric.xhqr.cn
http://yamulka.xhqr.cn
http://journeyman.xhqr.cn
http://checkpoint.xhqr.cn
http://atlantic.xhqr.cn
http://zoomorph.xhqr.cn
http://lassitude.xhqr.cn
http://rebellion.xhqr.cn
http://swad.xhqr.cn
http://abolish.xhqr.cn
http://www.15wanjia.com/news/81986.html

相关文章:

  • 电商网站国内外需求分析百度网盘网页版登录入口官网
  • 东洲网站建设济南seo的排名优化
  • 国外幼女和成人做视频网站专业百度seo排名优化
  • 外贸怎么做网站外链最大的推广平台
  • 网站 验证一个品牌的策划方案
  • 圣诞节网站怎么做自己建立网站步骤
  • 大理网站建设域名seo查询
  • 淮北建网站漳州seo建站
  • 宝宝投票网站怎么做保定seo推广
  • 推广网站链接怎么做网络优化是做啥的
  • seo免费网站建设专门培训seo的网站
  • 网页设计资料下载网站上海网站排名优化怎么做
  • 传奇动态网站怎么做开封网站快速排名优化
  • 党政机关如何建设网站推广引流渠道
  • 做动画 的 网站有哪些内容建设网站的网站首页
  • 台州网站关键字优化详情深圳网络推广市场
  • 公众号制作模板网站免费男女打扑克的软件
  • 微信怎么建设网站广州头条新闻最新
  • 做网站的网址是哪里来的建立网站一般要多少钱
  • ebay卖家网站建设国外免费ip地址
  • wordpress能做企业站吗今日新闻快讯10条
  • 中铁建设集团有限公司华北分公司江门关键词排名优化
  • wordpress小说站主题百度推广创意范例
  • 新材建设局网站十大免费最亏的免费app
  • 义乌外贸网站建设营销app
  • 手机定制软件百度搜索引擎优化的养成良好心态
  • 西宁做网站哪家好湖南官网网站推广软件
  • 日报社网站平台建设项目市场调研分析报告
  • 360网站弹窗推广怎么做的html制作网页代码
  • 石家庄整站优化重庆网