Вы находитесь на странице: 1из 27

EJB 3.

0 and Spring
June 2008 presented by: Reza Ghaffaripour www.RezaGh.com

Topics?

EJB2.1and3.0 JPA EJB3.0andSpringcomparison JPAandHibernatecomparison

WhatisEJB?

AnEnterpriseJavaBean(EJB)isaserversidecomponent EJBcontainerprovidessystemlevelservicestobeans EJBneedsEJBcontainertorunin Systemlevelservicesaretransactionmanagement,security, authorization,pooling,caching,...

TypesofEJBs? SessionBean Performsataskforaclient;optionallymayimplementaweb service.CanbeStatefulorStateless EntityBean NomoreinEJB3.0;insteadwehaveJPA MessageDriven Actsasalistenerforaparticularmessagingtype,suchasJMS

EJB2.1 IspartofJ2EE1.4 IsheavierthanEJB3.0

EJB3.0 IspartofJavaEE5andneedsJDK5+ EJB3.0makesanotherattempttoreduceEJB'scomplexity EJB3.1isontheway(JSR318)!

WhatisnewinEJB3.0?

Annotations

@Stateless publicclassSomeBeanimplementsSome{ publicStringgetMessage(){ return"TestMessage"; } }

@Remote publicinterfaceSome{ publicStringgetMessage(); }

WhatisnewinEJB3.0?

Annotations

@Stateless @Remote publicclassSomeBean{ publicStringgetMessage(){ return"TestMessage"; } } ButdeveloperscanstilluseXMLfilesoptionally

WhatisnewinEJB3.0?

Interceptors

Theruntimeservicesliketransactionandsecurityare applied to the bean objects at the method's invocationtime Theseservicesareoftenimplementedasthe interceptormethodsmanagedbythecontainer

Whatis(JavaPersistenceAPI)JPA?

IspartofJavaEE5 ProvidesanO/RmappinglikeHibernate HasaquerylanguagelikeHQLcalledJPQL Usesannotations AnentityisadomainobjectPOJO Entitiesaremanagedbytheentitymanager(em)

JPA
@Entity @Table(name="BOOK_TABLE") publicclassBookimplementsSerializable{ privateStringbookId; privateStringtitle; publicBook(){} publicBook(StringbookId,Stringtitle,...){ } @Id publicStringgetBookId(){ } publicvoidsetBookId(Stringid){ this.bookId=id; } ... } returnthis.bookId; this.bookId=bookId; this.title=title;

@PersistenceContext EntityManager em; ... Book book = em.find(bookID); em.persist(book); em.remove(book); em.merge(book); ...

EJB3.0andSpringComparisonAspects Persistence Transaction StateManagement Messaging Remoting DependencyInjection Aspects XMLorAnnotation Standard

EJB3.0andSpring:Persistence SpringpersistenceisbasedonJDBC,Hibernate,JDO,iBatis, andJPA(asofSpring2.0) EJBbydefaultusesJPAbutcanuseotherstoo

EJB3.0andSpring:Transaction

SpringsupportsJTA,JDBCandHibernatetransactions EJB3.0supportsJTAandcansupportothersifbasedonJTA

EJB3.0andSpring:StateManagement

EJBprovidesStatefulsessionbeans SpringhasWebFlowforSpringMVC Spring'sotherapproachesincludeputtingstateinthe database,theHTTPSession,orinaninmemorycache Springemphasizesonbeingstateless

EJB3.0andSpring:Messaging BothSpringandEJB3.0enablethesendingandreceivingof JMSmessages IntermsofimplementationMDBsareaJCPstandardwhereas SpringMDPs(MessageDrivenPOJO)arenot

EJB3.0andSpring:Remoting EJBremotingisbuiltonRMI EJBremotingincludessupportforsecurityandtransaction StatelessEJBsmayalsobeexposedasWebservices Spring2.0supportsvariousformsofremotingincludingRMI,Web services,Hessian,Burlap,andHTTPinvokers Springremotingdoesnotsupportoutoftheboxsecurityand transactionpropagation

EJB3.0andSpring:DependencyInjection

EJB3supportsinjectionofprimitivetypesandJavaEE resourcesfromJNDI Springsupportsinjectionofanything

EJB3.0andSpring:Aspects EJB3supportsinterceptors SpringistightlyintegratedwithAspectJwithallofit'sfeatures

EJB3.0andSpring:XMLVersusAnnotation

XMLfilescanexpresscomplexrelationships Annotationsaresimpleandconcise BothSpringandEJBsupportXMLandannotation

EJB3.0andSpring:Standard

SpringisnotaJavastandard IfstandardizationisimportanttoyouthenconsiderEJB3.0 EJBhaswidervendorsupport

WhentoUseSpringAlone Yourapplicationrequiresfinegrainedcontrol,alotof configurationandgluingthingstogether Youneedtobuildyourownstackwithdifferentframeworks YouneedadvancedAOPfeatures Youneedfasterdevelopmentprocess Youdon'tneedappserver ...

WhentoUseEJBAlone Youneedappserver Yourapplicationisverystateful Standardizationisanimportantconsideration Applicationshouldbescalableoutofthebox ...

EJB3.0andSpringTogether! Theyarenotenemies!Theycanworktogether! SpringinEJBscenario Canbeusedastheglueinalltiers Supportforotherpersistencemethods EJBinSpringscenario Immediateuseofappserverfacilities(loadbalancing,failover,...)

EJB3.0andSpringinthesameStack

Presentation Tier JSP Tags Spring Tags Controller Servlet Spring Controller Delegate\Facade EJB EJB Business Tier Spring POJO ** Integration Tier * *
*JPAorSpringwithHibernate/JDBC/... **SpringPOJOorEJB

JPAandHibernate JPAis(byreputationatleast)slow JPAhavetoresidewithinaJEEappserver YoucanuseHibernatewithoutaJEEcontainer JPAisastandard;Hibernateisnot GavinKingisanactivememberofJPAandEJB3.0spec!

TheEnd?

Questions?

Вам также может понравиться