jsf - When is it necessary or convenient to use Spring or EJB3 or all of them together? -


i'm little confused mixed use of jsf2+spring+ejb3 or combination of those. know 1 of spring principal characteristics dependency injection, jsf managed beans can use @managedbean , @managedproperty anotations , dependency injection functionality. ejb3 i'm more confused when use along jsf or if there reason use it.

so, in kind of situation idea use spring+jsf2 or ejb3+jsf2?

until have created small web applications using jsf2 , never needed use spring or ejb3. however, i'm seeing in lot of places people working stuff together.

first of all, spring , ejb(+jta) competing technologies , not used in same application. choose 1 or other. spring or ejb(+jta). won't tell choose, tell bit of history , facts can easier make decision.


main problem they're trying solve providing business service layer api automatic transaction management. imagine need fire multiple sql queries perform single business task (e.g. placing order), , 1 of them failed, of course everything rolled back, db kept in same state before, if nothing happened. if didn't make use of transactions, db left in invalid state because first bunch of queries succeeded.

if you're familiar basic jdbc, should know can achieved turning off autocommit on connection, firing queries in sequence, performing commit() in same try in catch (sqlexception) rollback() performed. quite tedious implement everytime.

with spring , ejb(+jta), single (stateless) business service method call counts default transparently single full transaction. way don't need worry transaction management @ all. not need manually create entitymanagerfactory, nor explicitly call em.gettransaction().begin() , such when you're tight-coupling business service logic jsf backing bean class and/or using resource_local instead of jta in jpa. example have following ejb class utilizing jpa:

@stateless public class orderservice {      @persistencecontext     private entitymanager em;      @ejb     private productservice productservice;      public void placeorder(order neworder) {         (product orderedproduct : neworder.getproducts()) {             productservice.updatequantity(orderedproduct);         }          em.persist(neworder);     }  } 

if have @ejb private orderservice orderservice; in jsf backing bean , invoke orderservice.placeorder(neworder); in action method, single full transaction performed. if example 1 of updatequantity() calls or persist() call failed exception, rollback far executed updatequantity() calls, , leave db in clean , crisp state. of course, catch exception in jsf backing bean , display faces message or so.

noted should "spring" quite large framework not competes ejb, cdi , jpa. previously, during dark j2ee ages, when ejb 2.x extremely terrible implement (the above ejb 3.x orderservice example in ejb 2.x require @ least 5 times more code , xml code). spring offered better alternative required less java code (but still many xml code). j2ee/ejb2 learned lessons spring , came java ee 5 offers new ejb3 api more slick spring , required no xml @ all.

spring offers ioc/di (inversion of control; dependency injection) out box. during j2ee era configured xml can go quite overboard. nowadays spring uses annotations, still xml required. since java ee 6, after having learned lessons spring, cdi offered out box provide same di functionality, without need xml. spring di @component/@autowired , cdi @named/@inject can achieve same jsf @managedbean/@managedproperty, spring di , cdi offers many more advantages around it: can example write interceptors pre-process or post-process managed bean creation/destroy or managed bean method call, can create custom scopes, producers , consumers, can inject instance of narrower scope in instance of broader scope, etc.

spring offers mvc competes jsf. makes no sense mix jsf spring mvc. further spring offers data abstraction layer on jpa, further minimizing dao boilerplate (but doesn't represent business service layer whole).

see also:


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -