What is the default scope for a component created by @Produces without annotating the scope?

I have an annotated method @Producesthat creates Apple.

When I use it with @ApplicationScoped, like this

public class AppleProducer {
    @ApplicationScoped
    @Produces
    public Apple createApple() {
        return new Apple();
    }
}

then Apple is created only once for the entire application.

When I use it with @RequestScoped, like this

public class AppleProducer {
    @RequestScoped
    @Produces
    public Apple createApple() {
        return new Apple();
    }
}

then it is created for each request.

But what if I do not indicate the scope?

public class AppleProducer {
    @Produces
    public Apple createApple() {
        return new Apple();
    }
}

How often will Apple be created? I suspect with every access, is this correct? Is there any documentation on this?

+6
source share
2 answers

This is @ Dependent .

"2.4.4. " CDI (1.2):

- .

, :

• scope, bean- @Dependent.

• , , , .

• , , , . , .

, , .

+8

- , bean @Dependent defaut.

, bean bean, .

, :

public class AppleProducer {
    @Produces
    public Apple createApple() {
        return new Apple();
    }
}

Apple @ApplicationScoped Pie bean:

@ApplicationScoped
public class Pie {

    @Inject
    private Apple apple;
}

Apple bean @ApplicationScoped, .

Pie bean - @RequestScoped, Apple bean.

+3

Source: https://habr.com/ru/post/1672352/


All Articles