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

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).
主站蜘蛛池模板: 浦东新区| 会泽县| 武鸣县| 临邑县| 东山县| 邯郸市| 泽州县| 尼木县| 兴宁市| 宁波市| 仙桃市| 木兰县| 寿阳县| 罗田县| 秦皇岛市| 江永县| 历史| 陕西省| 修水县| 丹阳市| 东兴市| 新河县| 伽师县| 河曲县| 中宁县| 长治市| 巧家县| 茌平县| 松江区| 云南省| 靖江市| 常宁市| 长宁县| 远安县| 远安县| 莱州市| 稷山县| 曲靖市| 苍山县| 赤壁市| 嵊州市|