Java EE 6 and Singletons

Can someone explain the complete process for implementing Singleton in a Java EE 6 application? I assume I should not create a singleton in the typical way of declaring a static variable and should use the @Singleton annotation? Should I do it this way?

This is just a @Singleton ad announcement and what is it? Should I do more class?

What do I need to do to access singleton in other classes?

+4
source share
2 answers

Is this just an announcement of this @Singleton and what is it?

Yes! It! Just create a class like any other javab.

However, note that this is really not the same as the GoF Singleton design template . Instead, it is "just creating one" template . Perhaps this is the source of your confusion. Admittedly, the annotation name is somewhat poorly chosen, and the @ApplicationScoped name is used in JSF and CDI.


What do I need to do to access singleton in other classes?

Just like every other EJB, introducing it as @EJB :

 @EJB private YourEJB yourEJB; 
+9
source

The javax.ejb.Singleton annotation javax.ejb.Singleton used to indicate that the enterprise bean implementation class is a single-line bean session.

This information is intended to indicate an ejb container, not to create multiple instances of this bean and create only one instance. Otherwise, it is a regular bean class. More details here:

http://docs.oracle.com/javaee/6/tutorial/doc/gipvi.html

You do not need to create a static variable and do all the related things to make it singleton. Just write a regular bean, as indicated here, and the container will take care of an instance of only its object:

 @Startup @Singleton public class StatusBean { private String status; @PostConstruct void init { status = "Ready"; } ... } 
+2
source

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


All Articles