Ckkn
Ckkn

Reputation: 139

Does cdi producer take class scope

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?

Upvotes: 2

Views: 693

Answers (2)

Siliarus
Siliarus

Reputation: 6753

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.

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();
  }
}

Upvotes: 3

jklee
jklee

Reputation: 2268

It depends on

Produces @Dependent

@ApplicationScoped
class Bean {
    @Produces
    public String producesString(){
        return "test";
    }
}

Produces @ApplicationScoped

@ApplicationScoped
class Bean {
    @Produces
    @ApplicationScoped
    public String producesString(){
        return "test";
    }
}

Produces @RequestScoped

@ApplicationScoped
class Bean {
    @Produces
    @RequestScoped
    public String producesString(){
        return "test";
    }
}

Upvotes: 1

Related Questions