Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between @ApplicationScoped and @Singleton scopes in CDI?

Tags:

java

scope

cdi

In CDI there is the @ApplicationScoped and the (javax.inject) @Singleton pseudo-scope. What is the difference between them? Besides the fact that @ApplicationScoped is proxied, and @Singleton is not.

Can I just change my @Singleton bean to @ApplicationScoped? Can @ApplicationScoped bean have two (or more) instances?

like image 584
amorfis Avatar asked Dec 29 '10 16:12

amorfis


People also ask

What does the @ApplicationScoped annotation indicate?

Annotation Type ApplicationScoped. Specifies that a bean is application scoped.

What does ApplicationScoped mean?

Annotation. Description. @javax.enterprise.context.ApplicationScoped. A single bean instance is used for the application and shared among all injection points. The instance is created lazily, i.e. once a method is invoked upon the client proxy.

What is @ApplicationScoped in Java?

The Soup class is an injectable POJO, defined as @ApplicationScoped . This means that an instance will be created only once for the duration of the whole application. Try changing the @ApplicationScoped annotation to @RequestScoped and see what happens.

Is application scoped Singleton?

When beans are application scoped, the same instance of the bean is shared across multiple servlet-based applications running in the same ServletContext, while singleton scoped beans are scoped to a single application context only.


1 Answers

@Singleton is not part of the CDI specification. It is part of EJB and javax.inject (JSR-330). It is not mentioned in the spec what is its behaviour, so you can only rely on what's written in the Weld documentation.

like image 56
Bozho Avatar answered Oct 03 '22 01:10

Bozho