问题
Stateles ejbs are intended to be idempotent and have no memory of previous user interactions. That sounds like a static method to me.
so instead of having
public void save(Entity e) { em.persist(e); }
is it safe to have
public static void save(Entity e) { em.persist(e); }
inside an EJB?
回答1:
No, because static methods do not participate in container-managed transactions, AOP, security, etc. BTW your second example won't compile, em
is injected by the application server and it can't be static (?)
Also there is no such requirement that stateless EJBs should be idempotent and have no state (despite the name). Not to mention that once you start using static
methods, you don't need EJBs at all...
来源:https://stackoverflow.com/questions/13202091/does-it-make-sense-to-have-static-methods-in-stateless-ejbs