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?
Annotation Type ApplicationScoped. Specifies that a bean is application scoped.
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.
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.
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.
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With