I have class that is marked with @Singleton.
To get instance of that class I use the following code:
public Object getMySingleton(Class someClass){
Bean bean = (Bean) beanManager.getBeans(someClass).iterator().next();
CreationalContext ctx = beanManager.createCreationalContext(bean);
Object obj =beanManager.getReference(bean, someClass, ctx);
return obj;
}
Please, pay attention that getMySingleton is not synchronized
. Is this code thread-safe? I mean, that getMySingleton can be called from different threads - will I get in all callers the same instance of Singleton?
As javaee server I use glassfish and weld as cdi container. However, I would like to get common answer according to java-ee specs.