cdi生产者是否采用类范围 [英] Does cdi producer take class scope

查看:121
本文介绍了cdi生产者是否采用类范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我的问题是,例如,在applicationcoped bean上生成实例还是applicationcoped吗?它是否需要其类范围或始终依赖?

Hello my question is does a produce for example on a applicationscoped bean produces instances also applicationscoped? Does it take its class scope or is always dependent?

推荐答案

规范将生产者方法视为bean(基本上,生产者是如何创建给定实例的定义bean类型)。因此,规则适用,如果没有提供范围,则假定 @Default

The specification treats producer methods as beans (basically, producer is a definiton of how you create an instance of given bean type). Therefore a rule applies, that if no scope is provided, @Default is assumed.

因此您的答案问题是 - 生产者范围是 @Default 如果没有指定。生产者范围与声明它的bean范围之间没有联系。

Hence the answer to your question is - the producer scope is @Default if none is specified. There is no link between producer scope and the scope of the bean on which it is declared.

@ApplicationScoped
public MyBean {

  @Produces //this will produce @Dependent
  public Foo produceDependent() {
    return new Foo();
  }

  @Produces
  @RequestScoped //produces the scope you define
  public Bar produceReqScopedBean() {
    return new Bar();
  }
}

这篇关于cdi生产者是否采用类范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆