What's the default scope for a bean created by a @Produces method without a scope annotation?
It's @Dependent.
According to "2.4.4. Default scope" from the CDI (1.2) specification:
When no scope is explicitly declared by annotating the bean class or producer method or field the scope of a bean is defaulted.
The default scope for a bean which does not explicitly declare a scope depends upon its declared stereotypes:
• If the bean does not declare any stereotype with a declared default scope, the default scope for the bean is @Dependent.
• If all stereotypes declared by the bean that have some declared default scope have the same default scope, then that scope is the default scope for the bean.
• If there are two different stereotypes declared by the bean that declare different default scopes, then there is no default scope and the bean must explicitly declare a scope. If it does not explicitly declare a scope, the container automatically detects the problem and treats it as a definition error.
If a bean explicitly declares a scope, any default scopes declared by stereotypes are ignored.
As you do not define any Scope, your produced bean will be @Dependent
by defaut.
It means that lifecycle of the produced bean will be the lifecycle of the bean which in which it is injected (contains the @Inject
).
So if you have the following producer :
public class AppleProducer {
@Produces
public Apple createApple() {
return new Apple();
}
}
If you Inject an Apple in an @ApplicationScoped
Pie
Bean :
@ApplicationScoped
public class Pie {
@Inject
private Apple apple;
}
Then the Apple
bean will be @ApplicationScoped
, so created just one time.
If Pie
bean is @RequestScoped
then the Apple
bean will be created at each request.