问题
Now, with EJB 3.1, we can find the javax.ejb.Singleton annocation that can ensure that this bean is going to be singleton.
Is there a way that i can ensure singleton using stateless beans in EJB 3.0 with some modifications in my code (use of the keyword static, or other way to do that....)
回答1:
If you're able to limit your @Stateless
bean pool size to exactly 1, then you can get pretty close to an @Singleton
.
The effect would be like having an @Singleton that uses @Lock(WRITE)
for all calls (i.e. no concurrency) and does not eagerly startup via @Startup
(it will start on first access).
You might still be able to get the effect of @Startup
if your platform has the option to eagerly fill @Stateless
bean pools.
回答2:
Is there a way that I can ensure singleton using stateless beans in EJB 3.0 with some modifications in my code (use of the keyword static, or other way to do that....)
No, nothing standard. Your container might provide some specific extensions though (e.g. JBoss has a proprietary @Service
annotation).
来源:https://stackoverflow.com/questions/3844720/can-i-make-my-own-singleton-stateless-bean-with-ejb-3-0