java - Differences : @SessionScoped vs @Stateful and @ApplicationScoped vs @Singleton -
i know, principal differences between :
- javax.enterprise.context.sessionscoped , javax.ejb.stateful
- javax.enterprise.context.applicationscoped , javax.ejb.singleton
i know @sessionscoped , @stateful allows create new instance each client. know @applicationscoped , @singleton / @stateless shared between clients.
=> when should consider it's better choose ejb, or other?
@sessionscoped
denotes scope while @stateful
in way call stereotype. @stateful
adds number services bean, among transactional behavior , passivation.
central @stateful
session behavior, indeed overlaps session scope.
the difference session scope tied http session, while @stateful
open-ended user managed session, lifetime managed client has reference bean proxy.
@stateful
remote beans binary (rmi) counter parts of servlets. servlets listened remote http requests browser, @stateful
remote beans listened remote rmi requests applets (and later swing clients).
there unfortunately many inconsistencies between two. servlet http listener, while @stateful
beans automatically brought in lot of other features. servlet shared session other servlets , shared java ee component namespace other servlets in war, while @stateful
ejb every separate bean has own session , component namespace.
with introduction of local beans in ejb 2 , sharp decline of swing/applet clients remote ejb communication, function of session that's maintained @stateful
bean has become less clear.
i think it's fair @stateful
isn't used these days. web application http session leading, means using session scope , local @stateless
beans and/or cdi beans business logic.
in cases @stateful
beans needed natural support extended persistence context jpa , passivation features (servlet doesn't have standardized passivation mechanism). note @stateful
, @sessionscoped
(or many other scopes) can combined. advantage of combining them user code no longer needs manage lifetime, container manages this.
there's similar story @applicationscoped
, @singleton
, although without legacy (@singleton
new thing). @applicationscoped
scope, while @singleton
bean type (stereotype if wish), doesn't give application scoped behavior, provides transactional behavior again, automatic locking (which can tuned via @lock
) , eager construction behavior (via @startup
).
although @stateful
, @singleton
pretty handy, current way forward in java ee seems to decompose build-in stereotypes separately useable annotations , knows, perhaps 1 day actual cdi stereotypes consisting of decomposed annotations.
Comments
Post a Comment