Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the default scope for a bean created by a @Produces method without a scope annotation?

I've got a method with a @Produces annotation that creates an Apple.

When I use it with @ApplicationScoped like this

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

then the Apple gets created only once for the whole application.

When I use it with @RequestScoped like this

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

then it gets created for every request.

But what if I do not specify a scope?

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

How often will the Apple be created? I suspect on every access, is it correct? Is there documentation about this?

like image 843
Harold L. Brown Avatar asked Mar 15 '17 12:03

Harold L. Brown


People also ask

What is the default scope of the beans?

The default scope for the bean is a singleton, like the example below, in which we haven't explicitly given a scope. Singleton means that the Spring container creates only one instance of the bean, and cached in memory, and all the requests for that bean will return a shared reference to the same bean.

What is the default bean scope in spring boot?

singleton - only one instance of the spring bean will be created for the spring container. This is the default spring bean scope.

What is the scope of Bean in portlet context?

What is the scope of bean in portlet context? global-session is scope of bean in portlet context.

What is bean scope in Spring using annotation?

When a spring bean is scoped as a prototype, the Spring IoC container creates new bean instance every time when a request is made for that bean. We can define the scope of a bean as prototype using scope="prototype" attribute of element or using @Scope(value = ConfigurableBeanFactory. SCOPE_PROTOTYPE) annotation.


2 Answers

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.

like image 177
Svetlin Zarev Avatar answered Oct 02 '22 07:10

Svetlin Zarev


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.

like image 31
Rouliboy Avatar answered Oct 02 '22 09:10

Rouliboy