118

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?

2
  • 12
    Have you read the Weld reference? There is some explanation about the practical differences between @ApplicationScoped and @Singleton at its section 5.4 (p. 36).
    – brandizzi
    Jul 3, 2011 at 18:50
  • 3
    which Singleton scope are you referring - javax.ejb or javax.inject?
    – John Ament
    Feb 18, 2015 at 12:51

6 Answers 6

37

@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.

2
  • 14
    That's not true. There is annotation javax.inject.Singleton. It is part of CDI. Check here: docs.jboss.org/weld/reference/1.0.1-Final/en-US/html_single/…
    – amorfis
    Dec 30, 2010 at 20:35
  • 7
    @amorphis - I have the CDI spec in front of me. I have even implemented parts of it, and it does not mention anything about @Singleton. It is shown in only one example, without clarifications. It is true that CDI relies on javax.inject, but strictly speaking it is not part of the CDI spec. That said, I corrected my answer a bit.
    – Bozho
    Dec 30, 2010 at 20:37
21

in short: You can even mix it (@Singleton and @ApplicationScoped) and it makes sense in some scenarios. (and works as expected in mine!)

Additionally to the other answers so far I'd like to add some more points for clarification in real world scenarios.

For me this question developed out of How do I force an application-scoped bean to instantiate at application startup? In some discussion there I stated this and can't find a valid argument against it so far:

In a lot of real-life scenarios/setups I would say it's hard to definitely say - from an abstract/modelling point of view - whether something is (or will become/be treated like) an EJB or an application-scoped managed bean.

(debatable but not conclusive) arguments (from my point of view) against it so far: (@BalusC and all others: I'd like to see them beeing conclusive, but if not, the above may hold true and nevertheless the arguments may still help the reader to get the differences/advantages/disadvantages/ bad/good practices)

EJB vs. Managed Bean

BalusC: That's an EJB not a managed bean, which is quite different. EJBs run in backend and managed beans in frontend. EJBs run in transactional context too. [...] You just confused enterprise beans with managed beans and I just pointed out that.

but:

me: I think you are not quite correct and overstating the meaning/usage and it looks debatable to me. http://en.wikipedia.org/wiki/Enterprise_JavaBeans

Enterprise JavaBeans (EJB) is a managed, server software for modular construction of enterprise software, and one of several Java APIs. EJB is a server-side software component that encapsulates the business logic of an application.

Types of Enterprise Beans

Session Beans [3] that can be either "Stateful", "Stateless" or "Singleton" [...]

Message Driven Beans [...]

... which still holds true in my case.

Singleton EJB vs. Application Scoped Bean

Locking

BalusC: A singleton EJB isn't the same as an application scoped bean. A singleton EJB is read/write locked and thus potentially inefficient/overconvoluted for the task you had in mind. Long story short: Grab a good Java EE book and learn to use the right tool for the job. One way definitely isn't the other way. That it works doesn't mean that it's the right tool. A sledgehammer is capable of fastening a screw, but it isn't necessarily the right tool to that :)

but:

(I can't see the sledgehammer here - sorry ...) It's good to know the locking defaults (I was not aware of it), but this seems to be incorrect again: Oracle Java EE 6 Tutorial on Managing Concurrent Access in a Singleton Session Bean

When creating a singleton session bean, concurrent access to the singleton’s business methods can be controlled in two ways: container-managed concurrency and bean-managed concurrency. [...]

Although by default, singletons use container-managed concurrency, the @ConcurrencyManagement(CONTAINER) annotation may be added at the class level of the singleton to explicitly set the concurrency management type

1
  • 1
    Weld is not amused by mixing: Exception while loading the app : CDI definition failure:WELD-000046: At most one scope may be specified on [EnhancedAnnotatedTypeImpl] public ApplicationScoped Singleton MyClass. Using javax.inject.Singleton.
    – sgflt
    Apr 20, 2020 at 14:56
15

Usually when you want to have only one instance of some object you probably should use @ApplicationScoped annotation - such object is proxied and thus can even be properly serialized out-of-the-box.

On the other hand, there are also many cases, where you want only one instance of the class, but such class cannot be proxied (e.g. because of being final) - then @Singleton is a rescue. Because Singleton is a pseudo-scope and is not being proxied like any "normal" scope.

3
  • 1
    I'm downvoting this because it's very unclear. I've been coding in Java EE for five years, taking tutorials, and reading books, but I have no clear idea what you mean when you say "such object is proxied", "can even be properly serialized", and "pseudo-scope". I would love to know what mean by this, as it sounds like you know what you're talking about, but as it is written I can't imagine your answer will help most Java EE developers.
    – DavidS
    Oct 16, 2019 at 18:25
  • 3
    That said, I do know EJBs are proxied. My confusion is partially around the distinction you're making for Singletons.
    – DavidS
    Oct 18, 2019 at 15:54
  • 1
    @DavidS: I agree the answer could be more specific and reference material to help. But I think his answer is valid. I am using Weld 3 which uses CDI 2.0, and from the docs you see what Demecki is saying about the scopes and proxies. 5.4. The singleton pseudo-scope "a little problem with this pseudo-scope. Beans with scope @Singleton don’t have a proxy object. Clients hold a direct reference to the singleton instance. So we need to consider the case of a client that can be serialized"
    – dko
    Aug 2, 2021 at 16:56
9

@Singleton in JSR-299 refers to Singleton session beans (javax.ejb.Singleton, not javax.inject.Singleton), not JSR-299 managed beans in a built-in scope called Singleton.

You might find in your server that @ApplicationScoped is one-per EAR or one-per WAR/EJB-JAR as it is not clear in the specification, but you should definitely not expect it to be one per JVM.

0
8

There is one more difference: @Singleton is not bean defining annotations, as the Singleton scope is not a normal scope. Then @ApplicationScoped is bean defining annotations.

With CDI 1.1 spec: When application in discovery-mode = annotated, Weld does not identify beans with @Singleton and not loaded this

3

One of the major differences that you can write your class with default constructor has private access modifier when using javax.inject.Singleton, but your class should have default constructor with at least default access modifier when using javax.enterprise.context.ApplicationScoped and this is JBOSS 6.1 GA Final implementation

1
  • Did you mean “default constructor”? May 1, 2019 at 23:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.