Java EE 6的和单身 [英] Java EE 6 and Singletons
问题描述
谁能解释在Java EE 6的应用程序执行Singleton的全过程?我假设我不应该在声明静态变量的典型方式来创建一个独立的,并应该使用 @Singleton
标注?我必须做这种方式?
难道仅仅是宣布它 @Singleton
,就是这样的情况?我必须再做类?
我怎么那么需要做访问单身我在其他类?
的难道仅仅是宣布它@辛格尔顿的情况,这就是它?的
块引用>是的!而已!只是设计类像任何其他的Javabean。
做但是请注意,这确实是不一样的GoF的 Singleton设计模式。相反,它正是只是创建一个的格局。也许这就是你的困惑的根源。不可否认,标注的名称是有点不好选择,在JSF和CDI的名称
@ApplicationScoped
被使用过。
的我怎么那么需要做访问单身我在其他类?的
块引用>用同样的方法,因为每个其他的EJB,通过注入为
@EJB
:@EJB
私人YourEJB yourEJB;Can anyone explain the full process of implementing a Singleton in a Java EE 6 app? I'm assuming that I shouldn't be creating a singleton in the typical way of declaring a static variable and should be using the
@Singleton
annotation? Do I have to do it this way?Is it just a case of declaring it
@Singleton
and that's it? Do I have to do anymore to the class?What do I then need to do to access the singleton in my other classes?
解决方案Is it just a case of declaring it @Singleton and that's it?
Yes! That's it! Just design the class like any other Javabean.
Do however note that this is indeed not the same as GoF's Singleton design pattern. Instead, it's exactly the "just create one" pattern. Perhaps that's the source of your confusion. Admittedly, the annotation name is somewhat poorly chosen, in JSF and CDI the name
@ApplicationScoped
is been used.
What do I then need to do to access the singleton in my other classes?
Just the same way as every other EJB, by injecting it as
@EJB
:@EJB private YourEJB yourEJB;
这篇关于Java EE 6的和单身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!