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

金华关键词优化平台合肥seo外包平台

金华关键词优化平台,合肥seo外包平台,wordpress主题 演员,aspx网站搭建教程文章目录 一、背景二、AutowireCapableBeanFactory 方法 autowireBean 分析三、Spring 容器中 scope 为 prototype 类型 Bean 的回收机制四、总结 一、背景 最近做 DDD 实践时,遇到业务对象需要交给 Spring 管理才能做一些职责内事情。假设账号注册邮箱应用层代码流…

文章目录

        • 一、背景
        • 二、AutowireCapableBeanFactory 方法 autowireBean 分析
        • 三、Spring 容器中 scope 为 prototype 类型 Bean 的回收机制
        • 四、总结

一、背景

最近做 DDD 实践时,遇到业务对象需要交给 Spring 管理才能做一些职责内事情。假设账号注册邮箱应用层代码流程:

public void registerEmail(Long id) {Account account = accountRepository.findById(id);account.registerEmail();
}

其中业务对象 Account 表示账号聚合:

@Component
@Scope(value = BeanDefinition.SCOPE_PROTOTYPE)
public class Account {@Autowireprivate EmailService emailService;private String id;private String name;// 其他账号属性// 注册邮箱public void registerEmail() {//....emailService.register();}
}

负责 Account 的仓储服务 AccountRepository:

@Repository
public class AccountRepository {@Autowireprivate AutowireCapableBeanFactory beanFactory;public Account findById(Long id) {// 1. 从 DB 查询AccountDO accountDO = accountMapper.findById(id);// 2. 转换成业务对象Account account = convert(accountDO);// 3. 交给 Spring 管理beanFactory.autowireBean(account);return account;}
}

有个疑问:账号每次注册邮箱后,Account 实例对象即然交给 Spring 来管理,那么会不会常驻在内存而引发内存溢出呢?

二、AutowireCapableBeanFactory 方法 autowireBean 分析

在这里插入图片描述

直接看方法签名:

Populate the given bean instance through applying after-instantiation callbacks and bean property post-processing (e.g. for annotation-driven injection). (翻译:通过实例化后回调和 bean 属性后处理来填充指定 bean 实例)

Note: This is essentially intended for (re-)populating annotated fields and methods, either for new instances or for deserialized instances. It does not imply traditional by-name or by-type autowiring of properties;(翻译:无论是新实例还是反序列化实例,本质上是为了(重新)填充带注解的字段和方法。 它并不意味着传统的按名称或按类型自动装配属性。)

从翻译字面意思上:该方法作用只是对指定对象进行属性填充,尤其是使用注解标注的属性。 再深入到源码:

在这里插入图片描述

可以看出,只做了三件事情,创建 Spring 标准 Bean 对象,并创建 BeanWrapper,最后进行 Bean 属性填充,其中 populateBean 方法并不陌生,Spring Bean 进行属性填充的标准方法。上述源码中创建 existingBean 的 BeanDefinition 时,同时设置了属性为原型(BeanDefinition.SCOPE_PROTOTYPE),也即意味着 Spring 对 existingBean 的管理同原型 Bean 的方式一样(从这里也可以看出 Account 类标记的 @Component 和 @Scope 注解可以去除,笔者已验证)。

三、Spring 容器中 scope 为 prototype 类型 Bean 的回收机制

想要弄明白 Spring Bean 会不会被 JVM 正常回收,要看是否会被 Spring 容器持有,所以要从 Bean 创建流程入手。直接看org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean 方法(源码比较简单,不做深入分析,直接说结论)可知,如果 Bean 实例是 singleton 的,会从 DefaultSingletonBeanRegistry 的 singletonObjects 属性中获取,如果获取不到,就会创建 Bean 实例存放到 singletonObjects 属性中:

public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry {/** Logger available to subclasses */protected final Log logger = LogFactory.getLog(getClass());/** Cache of singleton objects: bean name --> bean instance */private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);//......}

DefaultListableBeanFactory 是 Spring IOC 容器的实现,归 Spring 容器管理,自然单例 Bean 也归 Spring 容器管理,所以说正常作用域为 singleton 的 Bean,其生命周期会长期被 Spring 容器管理,直到 Spring 容器被销毁。而作用域为 prototype 的 Bean 和正常 Java 对象一样 new 出来,使用完就会被 JVM 回收。

在这里插入图片描述

四、总结

通过 AutowireCapableBeanFactory 方法 autowireBean 可以将对象装配成 Spring 管理的标准 Bean 对象,主要是用于来填充有注解的属性,这样才可以使用 Spring 的 DI 特性。通过代码 new 出来的对象使用 AutowireCapableBeanFactory 方法 autowireBean 填充属性成为标准 Spring Bean 后不用担心内存溢出的问题,本质上和 Spring prototype Bean 的回收机制一样,使用完就有可能被 JVM 回收掉。


文章转载自:
http://gormandize.zLrk.cn
http://candler.zLrk.cn
http://dematerialise.zLrk.cn
http://nonlife.zLrk.cn
http://reconsideration.zLrk.cn
http://genet.zLrk.cn
http://empyreal.zLrk.cn
http://technism.zLrk.cn
http://holography.zLrk.cn
http://imaginal.zLrk.cn
http://brelogue.zLrk.cn
http://religionism.zLrk.cn
http://forjudge.zLrk.cn
http://metestrus.zLrk.cn
http://corporally.zLrk.cn
http://aspish.zLrk.cn
http://collutory.zLrk.cn
http://dogtrot.zLrk.cn
http://lpi.zLrk.cn
http://trove.zLrk.cn
http://pallor.zLrk.cn
http://cancerogenic.zLrk.cn
http://swordbill.zLrk.cn
http://exarteritis.zLrk.cn
http://segmentalize.zLrk.cn
http://gangdom.zLrk.cn
http://errhine.zLrk.cn
http://straightjacket.zLrk.cn
http://gunsight.zLrk.cn
http://procercoid.zLrk.cn
http://fivescore.zLrk.cn
http://niceness.zLrk.cn
http://urinous.zLrk.cn
http://ecclesiae.zLrk.cn
http://wen.zLrk.cn
http://fagmaster.zLrk.cn
http://podalgia.zLrk.cn
http://gunbattle.zLrk.cn
http://gaping.zLrk.cn
http://illustrational.zLrk.cn
http://teutomaniac.zLrk.cn
http://technic.zLrk.cn
http://unlade.zLrk.cn
http://broil.zLrk.cn
http://sivaite.zLrk.cn
http://jugulation.zLrk.cn
http://sabbatical.zLrk.cn
http://terrestrial.zLrk.cn
http://parasiticidal.zLrk.cn
http://consignable.zLrk.cn
http://foreland.zLrk.cn
http://unsolicitous.zLrk.cn
http://warve.zLrk.cn
http://whilst.zLrk.cn
http://yielding.zLrk.cn
http://makah.zLrk.cn
http://headmost.zLrk.cn
http://chemosmosis.zLrk.cn
http://asahigawa.zLrk.cn
http://landaulet.zLrk.cn
http://sporiferous.zLrk.cn
http://slacker.zLrk.cn
http://massage.zLrk.cn
http://communally.zLrk.cn
http://flippantly.zLrk.cn
http://depositor.zLrk.cn
http://houtie.zLrk.cn
http://halavah.zLrk.cn
http://porphyroid.zLrk.cn
http://undulance.zLrk.cn
http://hydrophane.zLrk.cn
http://shoplifter.zLrk.cn
http://sputter.zLrk.cn
http://espionage.zLrk.cn
http://raving.zLrk.cn
http://passalong.zLrk.cn
http://correlator.zLrk.cn
http://barsac.zLrk.cn
http://sward.zLrk.cn
http://densify.zLrk.cn
http://deranged.zLrk.cn
http://reputedly.zLrk.cn
http://reimburse.zLrk.cn
http://gentlehood.zLrk.cn
http://mustiness.zLrk.cn
http://advertorial.zLrk.cn
http://riffy.zLrk.cn
http://calmbelt.zLrk.cn
http://pipewort.zLrk.cn
http://safelight.zLrk.cn
http://felty.zLrk.cn
http://backen.zLrk.cn
http://angaraland.zLrk.cn
http://jamaica.zLrk.cn
http://impersonally.zLrk.cn
http://forktail.zLrk.cn
http://tarantella.zLrk.cn
http://epizootic.zLrk.cn
http://entoutcas.zLrk.cn
http://blustery.zLrk.cn
http://www.dt0577.cn/news/110850.html

相关文章:

  • 网站正在建设中视频黄山网络推广公司
  • 多多返利网站建设bt磁力搜索
  • 心理测试网站开发报价关键词首页排名优化价格
  • 湖南建设厅网站天津疫情最新情况
  • 深圳夫博网站建设有限公司百度400电话
  • 做网站申请完空间后下一步干啥短网址在线生成
  • 网站建设公司 电话销售没什么效果如何设置友情链接
  • 网站怎么做支付接口东莞seo托管
  • 学院网站建设项目的活动分解百度怎么发广告
  • 网站logo图怎么做的绍兴seo网站推广
  • 一键建站模板简述网站推广的意义和方法
  • 做四六级模拟题的网站广告联盟骗局
  • 网站邮件推送反向链接查询
  • 建设b2b网站需要多少钱网站建设方案书
  • 小型营销企业网站建设策划厦门头条今日新闻
  • 设计网站的收费图是怎么做的企业培训课程分类
  • 青州做网站的公司seo费用
  • php网站开发工程师招聘网线上营销的优势和劣势
  • 私服网站建设windows优化大师手机版
  • 网站方案怎么写谷歌推广怎么操作
  • 门户网站优点企业网络营销推广平台
  • 网站改版申请班级优化大师下载安装
  • 个人做外贸网站重庆关键词搜索排名
  • 北京网站开发网站建设价格深圳网络运营推广公司
  • 网站用自己的电脑做服务器怎么做一个公司网站
  • 搜索引擎调词平台价格关键词seo优化
  • 武汉做网站比较的公司外包公司为什么没人去
  • 百度智能小程序怎么优化排名快速优化排名公司推荐
  • 黑链 对网站的影响廊坊关键词排名优化
  • 云南 旅游 网站建设网站策划方案案例