官术网_书友最值得收藏!

Scopes

A very neat feature of the CDI is to handle the scope life cycle for you. Concretely, you decorate your beans with @ApplicationScoped and @RequestScoped, and the life of the bean is either bound to the application (it is a singleton) or the request duration (which means you can have as many different instances as you have concurrent requests).

The scope implementation is called context, and the context is mainly responsible for looking up in the right contextual instance or creating it. An application scoped instance will be looked up in a single map shared by the entire application. However, a request scoped instance will also be looked up in ThreadLocal associated with the request life cycle through ServletRequestListener.

The implications on the performance are quite immediate:

  • The context setup can be pricey (depending on the scope) and can add some overhead that you may not require. In fact, if you have no @RequestScoped bean, you don't need the ServletRequestListener instance (even if not very expensive).
  • Recreating your bean every time the context needs it will trigger the process we saw in the previous part and the life cycle hooks of the bean (@PostConstruct and @PreDestroy).
主站蜘蛛池模板: 安岳县| 连平县| 余姚市| 来安县| 庄浪县| 自贡市| 兴宁市| 慈利县| 金湖县| 商南县| 乌兰浩特市| 兴文县| 北票市| 吉木乃县| 长顺县| 陈巴尔虎旗| 土默特右旗| 宁南县| 曲周县| 左权县| 桑植县| 开阳县| 张家港市| 英吉沙县| 商都县| 开封市| 中卫市| 黎城县| 南丰县| 卫辉市| 潼南县| 仁怀市| 淮阳县| 广元市| 综艺| 曲周县| 龙泉市| 麻城市| 汝州市| 泗水县| 溧水县|