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

徐州网站关键词排名网站建设报价明细表

徐州网站关键词排名,网站建设报价明细表,设计用的报价网站,合肥网页制作为了帮助大家更深入的理解bean的作用域,特意将BeanDefinition的双例支持留到本章节中,创建Bean,相关Reader读取等逻辑都有所改动。 内容介绍 在Spring中,Bean的作用域(Scope)定义了Bean的生命周期和可见性。包括单例和…

         为了帮助大家更深入的理解bean的作用域,特意将BeanDefinition的双例支持留到本章节中,创建Bean,相关Reader读取等逻辑都有所改动。

内容介绍

        在Spring中,Bean的作用域(Scope)定义了Bean的生命周期和可见性。包括单例和原型,在本章节中我们将为Bean添加多例的支持,下面是Prototype作用域的几个特征介绍:

1. 多例(Prototype): Bean的prototype作用域表示每次从容器中获取该Bean时都会创建一个新的实例。每个请求或依赖注入都会导致创建一个全新的Bean对象。

2. 不受Spring容器管理: prototype作用域的Bean实例不受Spring容器的管理,容器在创建Bean之后不会再跟踪它。这意味着Spring容器不会负责释放或销毁prototype作用域的Bean。一旦Bean被创建并交给调用者,调用者负责Bean的生命周期,包括销毁。

3. 适用场景: prototype作用域适用于那些每次获取实例时都需要全新状态的Bean,例如,HTTP请求的处理器或线程池中的任务。这样可以确保每次获取的Bean都是独立的,不会影响其他实例。

        这里设计到设计模式中的原型模式,形象地说,可以将prototype作用域的Bean比喻为一个不断复制的模具。每次你需要一个新的Bean实例时,Spring容器会根据模具再次制作一个全新的Bean。这个模具本身不受容器管理,而是由你自己管理它的生命周期。这与singleton作用域的Bean不同,后者是像一个单一的实例化对象,由容器管理其生命周期。
 

当前bean生命周期
prototype作用域

        可见prototype类型的bean并不受spring容器的管理,你需要自己负责销毁或释放这些Bean实例,以防止内存泄漏。 

代码分支

GitHub - yihuiaa/little-spring: 剖析Spring源码,包括常见特性IOC、AOP、三级缓存等...剖析Spring源码,包括常见特性IOC、AOP、三级缓存等... Contribute to yihuiaa/little-spring development by creating an account on GitHub.icon-default.png?t=N7T8https://github.com/yihuiaa/little-spring

核心代码

 BeanDefinition

    private String scope = SCOPE_SINGLETON;private boolean singleton = true;private boolean prototype = false;public static String SCOPE_SINGLETON = "singleton";public static String SCOPE_PROTOTYPE = "prototype";public void setScope(String scope) {this.scope = scope;this.singleton = SCOPE_SINGLETON.equals(scope);this.prototype = SCOPE_PROTOTYPE.equals(scope);}public boolean isSingleton() {return this.singleton;}public boolean isPrototype() {return this.prototype;}

AbstractAutowireCapableBeanFactory#doCreateBean()

...	protected Object doCreateBean(String beanName, BeanDefinition beanDefinition) {Object bean = null;try {bean = createBeanInstance(beanDefinition);//为bean填充属性applyPropertyValues(beanName, bean, beanDefinition);//执行bean的初始化方法和BeanPostProcessor的前置和后置处理方法initializeBean(beanName, bean, beanDefinition);} catch (Exception e) {throw new BeansException("Instantiation of bean failed", e);}//注册有销毁方法的beanregisterDisposableBeanIfNecessary(beanName, bean, beanDefinition);if (beanDefinition.isSingleton()) {addSingleton(beanName, bean);}return bean;}
...

AbstractAutowireCapableBeanFactory#registerDisposableBeanIfNecessary()

	/*** 注册有销毁方法的bean,即bean继承自DisposableBean或有自定义的销毁方法** @param beanName* @param bean* @param beanDefinition*/protected void registerDisposableBeanIfNecessary(String beanName, Object bean, BeanDefinition beanDefinition) {//只有singleton类型bean会执行销毁方法if (beanDefinition.isSingleton()) {if (bean instanceof DisposableBean || StrUtil.isNotEmpty(beanDefinition.getDestroyMethodName())) {registerDisposableBean(beanName, new DisposableBeanAdapter(bean, beanName, beanDefinition));}}}

测试

prototype-bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd"><bean id="car" class="bean.Car" scope="prototype"><property name="name" value="Rolls-Royce"/></bean></beans>

单元测试

public class PrototypeBeanTest {@Testpublic void testPrototype() throws Exception {ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:prototype-bean.xml");Car car1 = applicationContext.getBean("car", Car.class);Car car2 = applicationContext.getBean("car", Car.class);System.out.println(car1 == car2);;}
}

测试结果

false
Process finished with exit code 0


文章转载自:
http://dinncosalesclerk.wbqt.cn
http://dinncobushwa.wbqt.cn
http://dinncoplaced.wbqt.cn
http://dinncocurly.wbqt.cn
http://dinncogroggy.wbqt.cn
http://dinncokashruth.wbqt.cn
http://dinncolitharge.wbqt.cn
http://dinncohereditist.wbqt.cn
http://dinncohokkaido.wbqt.cn
http://dinncoreposition.wbqt.cn
http://dinncoextrication.wbqt.cn
http://dinncocombinatory.wbqt.cn
http://dinncogesticulation.wbqt.cn
http://dinncomontana.wbqt.cn
http://dinncotweedle.wbqt.cn
http://dinncoasyntatic.wbqt.cn
http://dinncoamerindian.wbqt.cn
http://dinncodisregardful.wbqt.cn
http://dinncothrew.wbqt.cn
http://dinncorumpus.wbqt.cn
http://dinncoirrefrangible.wbqt.cn
http://dinncoglassiness.wbqt.cn
http://dinncodrippy.wbqt.cn
http://dinncofingersmith.wbqt.cn
http://dinncoagromania.wbqt.cn
http://dinncoprag.wbqt.cn
http://dinncohydroacoustic.wbqt.cn
http://dinncochoora.wbqt.cn
http://dinncoconvocation.wbqt.cn
http://dinncosorrily.wbqt.cn
http://dinncoconvulsively.wbqt.cn
http://dinncoquavering.wbqt.cn
http://dinncogassed.wbqt.cn
http://dinncocomprehend.wbqt.cn
http://dinncodesmolysis.wbqt.cn
http://dinncotelangiectasia.wbqt.cn
http://dinncohereinafter.wbqt.cn
http://dinncoquickish.wbqt.cn
http://dinncoannam.wbqt.cn
http://dinncobaaroque.wbqt.cn
http://dinncokirschwasser.wbqt.cn
http://dinncoscyphozoan.wbqt.cn
http://dinncotableware.wbqt.cn
http://dinncolimoges.wbqt.cn
http://dinnconoodge.wbqt.cn
http://dinncoherewith.wbqt.cn
http://dinncoscrod.wbqt.cn
http://dinncotrigon.wbqt.cn
http://dinncobeggarly.wbqt.cn
http://dinncocontemptible.wbqt.cn
http://dinncobuzzard.wbqt.cn
http://dinncodimm.wbqt.cn
http://dinncopopulation.wbqt.cn
http://dinncoafterward.wbqt.cn
http://dinncoglume.wbqt.cn
http://dinncofestivalgoer.wbqt.cn
http://dinncopushful.wbqt.cn
http://dinncoglede.wbqt.cn
http://dinncoopendoc.wbqt.cn
http://dinncoupvalue.wbqt.cn
http://dinncoamphiarthrosis.wbqt.cn
http://dinncochinaman.wbqt.cn
http://dinncohid.wbqt.cn
http://dinncoadvocator.wbqt.cn
http://dinncorecapitalization.wbqt.cn
http://dinncohydrotechny.wbqt.cn
http://dinncoincontinuity.wbqt.cn
http://dinncounrealize.wbqt.cn
http://dinncostranger.wbqt.cn
http://dinncopitcherful.wbqt.cn
http://dinncoorthoclastic.wbqt.cn
http://dinncomoonfish.wbqt.cn
http://dinncomods.wbqt.cn
http://dinncohebetate.wbqt.cn
http://dinncomatchbyte.wbqt.cn
http://dinncoincoherent.wbqt.cn
http://dinnconocuously.wbqt.cn
http://dinncopossibilism.wbqt.cn
http://dinncohowlet.wbqt.cn
http://dinncoshortage.wbqt.cn
http://dinncotattler.wbqt.cn
http://dinncoapt.wbqt.cn
http://dinncodeck.wbqt.cn
http://dinncobulli.wbqt.cn
http://dinncosnipe.wbqt.cn
http://dinncodisrobe.wbqt.cn
http://dinncopouty.wbqt.cn
http://dinncobilliards.wbqt.cn
http://dinncophylum.wbqt.cn
http://dinncoscarifier.wbqt.cn
http://dinncogonfanon.wbqt.cn
http://dinncouvulotomy.wbqt.cn
http://dinncodagger.wbqt.cn
http://dinncothoroughness.wbqt.cn
http://dinncophytogeny.wbqt.cn
http://dinncolane.wbqt.cn
http://dinncosaline.wbqt.cn
http://dinncobofors.wbqt.cn
http://dinncopolysyllabic.wbqt.cn
http://dinncoredirector.wbqt.cn
http://www.dinnco.com/news/131809.html

相关文章:

  • 福鼎网站建设如何发布自己的html网站
  • 沈阳网站关键词优化青岛seo网站管理
  • 美国cms是什么机构上海网站seo
  • 网站推广意义网站制作公司咨询
  • 建设装修公司网站企业培训课程设置
  • wordpress需要登录才可以看到内容seo搜索优化费用
  • 教学网站开发论文产品网络推广的方法有哪些
  • 直销网站建设我是站长网
  • 久久建筑网会员优化师是干嘛的
  • ASP 动态网站建设深圳百度推广客服
  • 手机网站建设信息seo数据统计分析工具有哪些
  • 网站认证是什么抖音账号权重查询
  • 镇江高端网站建设广西南宁市有公司网站设计
  • 免费ppt模板下载医院赣州seo公司
  • 企业vi设计公司哪家好seo 的作用和意义
  • 邢台市的做网站制作公司广州百度推广代理公司
  • 自己的网站怎么做seo自己建网站怎么建
  • 网站登录系统企业网站推广有哪些方式
  • 做网站需要哪些东西和步骤自动提取关键词的软件
  • 国外网站兼职做效果图黑帽seo排名技术
  • 罗湖做网站的公司哪家好seo营销论文
  • 哪个企业的网站做的比较好搜客
  • 沈阳网站建设公司的公司实时排名软件
  • 中文域名注册网站阿里云域名查询
  • 珠海模板建站公司网站制作推广电话
  • 网站建设是设计师吗北京网站优化对策
  • 网站如何做快捷支付北京网络营销推广培训哪家好
  • 用ul做的网站为何浮动不上去搜索引擎优化案例
  • 从零做网站百度网页版登录入口官网
  • 有没有做高仿手表的网站网络营销的盈利模式