You are on page 1of 17

EJB3.

0 Vs Spring

Priya Thinagar
CEO, X ON Technologies LLC

............................................3 EJB 3.....9  3..............................2 Dependency Injection .9  3.................................................13  4...6  Concepts of Spring Framework ..........................1.................................8  3  EJB 3..........................................................7  2.................................................................................................................................1 Simplified Programming Model with Java EE 5 .............................................1 High lights of Java EE 5...........2  Enterprise JavaBeans 3......................................3  3.....................................................13  4.....16  5..............................................................................7  2..13  4.........4  Summary...........................................................................14  Conclusions and Recommendations............................................Contents 1  Introduction..1 Use Spring if:.......16  5...........1..3 Auto wiring ...............................................1 Inversion of Control ...........................1  2..............................................................................................6  2.......................................................................................0..............................................2  5  Comparison Matrix between Spring Vs EJB 3.................................1 EJB 2..................................1  Overview ..................................................................2......................................................16  5..................................9  3.....................................15  5.4  4  Main benefits of the Java EE platform ........................................................3  Architectural benefits of Spring Framework ..........................................................13  4..................7  2...............1.....................................................................................0..........................2 Spring Framework.............6  2........1  Characteristics of EJB 2....................................................19  .......... Spring and EJB 3...............2  Recommendations .............................................................................................5  Spring Framework ..........................2...............4  References........................................5  How this document is Organized .........................................................2  2  About this document ..............................................................2................13  4.............x..................................2..............................................................................10  Summary......................... ..............................................x...2  Overview ............4 Aspect Oriented Programming (AOP)........1..............................0 and the Java Persistence API (JPA)........11  Comparison Analysis ........................2...................0 .................9  3................................................................................................1  1.................................10  3...............6  2.................1.17  Appendix A............1...........2 Use EJB 3 if: .....18  Acronyms ..........................................0................................5  1............................................................3  5...........................................6  2...............................18  5.........1  Conclusion .

concepts of spring and benefits of spring In this section conclusions are discussed based upon comparison analysis and given the recommendations suitable for Leicester project. Some of the source references are given in reference section of this document. blogs.2 How this document is Organized This document is organized as different sections and each section discusses the information like below • Section 2 : Spring Framework o • Section 3: EJB 3. benefits of Java EE Section 4: Comparison Analysis o • Discussed an overview information about what is spring .0 and Spring framework and provided the comparison matrix based upon different potential services/concepts. articles and books. This document will be a justification on why the particular technology has been recommended and this justification will factor-in the nature of the project The technology recommendation by this document will solely applicable only for Leicester project and will/should not be taken for general consideration to other related or non related projects.1 About this document The main goal of this document is to analyze and compare primarily. Java EE . This document is not learning tutorial for any technology which are discussed inside but rather it should give you an overview of them. Information given in this document are based upon official documentation. interviews .1 INTRODUCTION 1. which are used in this document. Appendix A : o References are given. 1. both EJB 3. Section 5: Conclusions and Recommendations o • Discussed an overview of EJB 3. .0 and Spring framework in order to choose the suitable technology for Leicester project development. which are used to write this document o Acronyms are given.0 .0 o • In this section comparison is done for both EJB 3. tutorial which are provided by respective technology inventor and from different forums.

IoC underpins a lot of functionalities provided by Spring. IoC (Inversion of Control) 2. and hence inter-relate with each other in some sort of relationships. and the container will be responsible to “inject” the relationships when the bean is created. elegantly replace services traditionally provided by J2EE application servers 2.2 SPRING FRAMEWORK 2. relationships and flow are usually defined and maintained within the application codes.2 Dependency Injection Dependency Injection (DI) is tightly coupled with IoC. the control of logic. Spring is a Lightweight Application Framework where Struts. In a typical application. WebWork and others can be considered Web frameworks. ¾ Spring can integrate nicely with J2EE application servers (or any Java environment) ¾ Spring can. these kinds of dependencies can be defined in the XML metadata configuration. Spring addresses all tiers of an application. there are multiple classes which work together to perform certain task(s). Spring framework stands out as the most popular and the most widely accepted framework for enterprise application development. Instead of defining the relationships within the application codes. Auto wiring 4. 2. In the IoC world. In Spring framework. DI (Dependency Injection). What is IoC? In traditional application development. the control is handed over to Spring framework.2. with Good reasons. . which calls application code (usually POJO) instead of application calling the framework.2. ¾ Spring is NOT a J2EE application server. 3.e. xml format) used by the container.2 Concepts of Spring Framework Core technologies and concepts of Spring framework are 1. and hence the name “Inversion” of Control. the objects that are instantiated and managed by the Spring IoC Container are referred to as beans. in many cases. These beans. AOP (Aspect Oriented Programming) 2.1 Inversion of Control Inversion of Control (IoC) is the single most important concept of Spring framework. together with their relationships and dependencies are defined in the configuration metadata (i.1 Overview Among all the lightweight containers.

whether or not EJB has been chosen to use. Autowiring in Spring happens at bean level. 2. It works in either web or EJB container. Spring can eliminate the increase of Singletons seen on many projects. Resin. Applications built using Spring are very easy to unit test. Spring can make the use of EJB an implementation choice. JBoss. this is a major problem.3 Architectural benefits of Spring Framework Some of the benefits Spring can bring to a project are ¾ ¾ ¾ ¾ ¾ ¾ ¾ Spring can effectively organize middle tier objects. And while it is perhaps most valuable in the middle tier. Spring can facilitate good programming practice by reducing the cost of programming to interfaces. which means it is possible to autowire some beans in the container by not the others. Spring's configuration management services can be used in any architectural layer. and it would save a significant amount of typing and maintenance effort of developers. Spring can eliminate the need to use a variety of custom properties file formats. which have traditionally been addressed by EJB. in whatever runtime environment. Ever wondered what magic property keys or system properties a particular class looks for.3 Auto wiring Spring IoC container is able to autowire relationships between collaborating beans. Jetty. and has been used successfully in Web Logic.2. Spring has successfully demonstrated the value proposition. Tomcat. The use of Inversion of Control and Dependency Injection helps achieve this simplification. The first goal of Spring's AOP support is to provide J2EE services to POJOs. so there's no risk of vendor lock in. rather than classes.2. It means the container will automatically resolve the bean relationships by inspecting the BeanFactory contents. reducing testability and object orientation. rather than the determinant of application architecture.2. Developer can choose to implement business interfaces as POJOs or local EJBs without affecting calling code. .4 Aspect Oriented Programming (AOP) Since 2003 there has been much interest in applying AOP solutions to those enterprise concerns. Spring AOP is portable between application servers. Spring is designed so that applications built with it depend on as few of its APIs as possible. Most business objects in Spring applications have no dependency on Spring. Spring takes care of plumbing that would be left up to developer if he/she uses only Struts or other frameworks geared to particular J2EE APIs. 2. The use of AOP as an alternative to EJB (version 2 or above) for delivering enterprise services is growing in importance. Orion and many other application servers and web containers. such as transaction management. and had to read the Javadoc or even source code? With Spring developer simply look at the class's JavaBean properties or constructor arguments. almost to zero. by handling configuration in a consistent way throughout applications and projects.

beyond classic J2EE. ¾ Spring provides a consistent framework for data access. JDO and other O/R mapping solutions. Spring also provides a powerful and flexible MVC web framework that is integrated into the overall IoC container. 2. such as JTA or JDBC. 2. 4. Many Spring features are also usable in a wide range of Java environments. which enables a consistent programming model over a variety of underlying transaction technologies. simple programming model in many areas. 6. However.4 Summary 1. . including a simple and productive JDBC framework that greatly improves productivity and reduces the likelihood of errors. Spring's data access architecture also integrates with TopLink. Spring also provides a unique transaction management abstraction.¾ Spring helps to solve many problems without using EJB. This framework is powerful enough to enable many applications to dispense with the complexity of EJB. rather than classes. Hibernate. JNDI and many other important APIs. The architectural basis of Spring is an Inversion of Control container based around the use of JavaBean properties. Spring provides a consistent way of managing business objects and encourages good practices such as programming to interfaces. Spring can provide an alternative to EJB that's appropriate for many applications. even without a JTA implementation. making it an ideal architectural "glue. Spring is a powerful framework that solves many common problems in J2EE. Hibernate or a JDO implementation. For example. Spring can use AOP to deliver declarative transaction management without using an EJB container. 5. whether using JDBC or an O/R mapping product such as TopLink. Spring provides an AOP framework written in standard Java. ¾ Spring provides a consistent. while enjoying key services traditionally associated with EJB. JMS. which provides declarative transaction management and other enterprise services to be applied to POJOs or an ability to implement the own custom aspects. JavaMail." Developer can see this consistency in the Spring approach to JDBC. 3. Spring provides a unique data access abstraction. if developer only needs to work with a single database. this is only part of the overall picture: Spring is unique in that it uses its IoC container as the basic building block in a comprehensive solution that addresses all architectural tiers.

1 Overview The primary goal of EJB 3. with fewer lines of code. Because it is much simpler. With Java EE 5. The focus in Java EE 5 is ease of development. JSTL. and annotations are used extensively to reduce the need for deployment descriptors.0 and the Java Persistence API (JPA) The core of EJB 3. The Java Persistence API incorporates support for many of the features that EJB developers have been asking for. although CMP is still fully supported in Java EE. and Java Data Objects (JDO).1.1. and the same tasks can be accomplished in a simpler way. and rich metadata for the specification of object/relational mapping. Constructing web applications is made easier with Java Server Faces (JSF) technology and the JSP Standard Tag Library (JSTL). and benefits greatly from work done in other products and technologies such as Hibernate. Java EE 5 includes simplified web services support and the latest web services APIs. making it an ideal implementation platform for ServiceOriented Architectures (SOA). Java Persistence is based on POJOs with annotations.3 EJB 3. technologies that are crucial for building applications for Web 2.0 is also much faster to learn to use than EJB 2. The simplified EJB 3. including support for improved object modeling.0 is a great simplification of the classic EJB programming model by using Plain Old Java Objects (POJOs) and Java persistence API is the most exciting work done by the EJB expert group. EJB 3. Java Persistence is in many ways a replacement for EJB CMP (Container Manager Persistence). AJAX. and more.0 is a major simplification over the APIs defined by the EJB 2. TopLink.1 High lights of Java EE 5 ¾ Easier development. and more resembles those other technologies. ¾ New persistence API (JPA). . EJB 3. inheritance. Both component and client code are simplified. Java EE 5 supports rich thin-client technologies such as AJAX. better.2 Enterprise JavaBeans 3. 3.0 is to target ease of development and it is the main theme of the Java EE 5 platform release. defaults are used whenever possible. an expanded query language. there is less code to write -. 3. Java Persistence API to provide a light-weight POJO persistence API for object/relational mapping.0 API allows developers to program EJB components as ordinary Java objects with ordinary Java business interfaces rather than as heavy weight components. and polymorphism. Java Persistence is a much simpler approach to mapping Java objects to relational databases.0.1 and earlier specifications.much of the boilerplate code has been removed. ¾ EJB -.0 3. EJB 3.0 makes programming with Enterprise JavaBeans technology simpler through the use of Plain Old Java Objects (POJOs) ¾ Enhanced web services. Java Persistence is a significant departure from EJB CMP.simpler. ¾ JSF.

communication.com/webservices/ 2.Java Persistence is available both as part of Java EE. and session management in a web container that is integrated with a web server. a. The Java EE platform provides a framework for developing and deploying web services on the Java platform. JAX-WS web service endpoints can be invoked by heterogeneous clients. 3. . and for use outside Java EE in a Java SE environment. for example. In a similar manner. see http://java. By default. transaction management.3 Main benefits of the Java EE platform The Java EE platform provides the following: 1. The Java API for XML Web Services (JAX-WS) enables Java technology developers to develop SOAP-based interoperable and portable web services. 3. scaling. Additional annotations can be used to control security requirements for methods and transaction requirements. the public methods of the component will be exposed to clients and will run in a transaction. the Enterprise JavaBeans (EJB) container handles distributed communication. etc. Similarly. which means that developers can focus on writing business logic -. EJB components can be written as POJOs with. JAX-WS enables clients to invoke web services developed across heterogeneous platforms. Faster solutions delivery time to market. a simple @Stateless annotation. Java EE containers provide for the separation of business logic from resource and lifecycle management.1 Simplified Programming Model with Java EE 5 Java EE 5 radically simplifies the development of enterprise applications by: ¾ ¾ ¾ ¾ Adopting a plain old Java object (POJO) programming standard and setting intelligent defaults for EJB components Eliminating the need for deployment descriptors and using Java metadata annotations for deployment settings instead Introducing a simplified POJO persistence model similar to Oracle TopLink and JBoss Hibernate Using dependency injection instead of the Java Naming and Directory Interface (JNDI) to locate resources and EJB components.2. Java Servlets simplify web development by providing infrastructure for component. threading. The Java EE platform uses "containers" to simplify development. For more info.sun. A web service endpoint is described using a Web Services Description Language (WSDL) document. Developers use the standard JAX-WS programming model to develop SOAP-based web service clients and endpoints. For example. a. Complete web services support.their value-add -rather than writing enterprise infrastructure. It is much easier to write a stateless or stateful component that takes full advantage of the transactional capabilities of the EJB container.

SOAP 1. Web Applications (JSF). asynchronous way. Freedom of choice. which adds full support for W3C XML Schemas. Simplified connectivity. the latest W3C and WS-I standards (e. The Java EE platform brings Write Once. EJB 3. simplifies the development of web services by automatically generating client and server code. Java EE 5 accelerates and radically simplifies Enterprise Java development.3. packaged financial applications.0). WSDL 1. freedom of choice. which can easily be converted to Web Services with Annotations or made persistent using the Java Persistence API.2). to cell phones. the Java EE platform helps IT by reducing Total Cost of Ownership (TCO) and simultaneously avoiding single-source lock-in for their enterprise software needs. The vendors are free to compete on implementations but not on standards or APIs. significantly reducing new code development. and the REST style of web services.0 greatly simplifies the programming model through Plain Old Java Objects (POJOs).0 data binding. It is now much easier to write a stateless or stateful component that takes full advantage of the transactional capabilities of the EJB container. improved state-saving behavior. and benefits greatly from work done in Hibernate. Java EE offers Java Message Service for integrating diverse applications in a loosely coupled. and simplified connectivity. In addition. protocol and transport independence. It introduces a new database persistence model (Java Persistence) and leverages Annotations from Java SE.0. By offering one platform with faster solution delivery time to market.0). and Java Data Objects (JDO). maps Java classes to XML data and is used by JAX-WS to encode and decode data sent in web services calls. 3. .2. and the ability to turn off component ID generation. a. JavaServer Faces 1. and transactional components (EJB 3. the Java EE platform has J2EE Connectors for linking to enterprise information systems such as ERP systems.0.2 simplifies the building of user interfaces for Web-based applications by providing pre-packaged components. and CRM applications. 4. Java Persistence is a much cleaner approach to mapping Java objects to relational databases. which replaces JAX-RPC. JAXB 2. Java EE technology is a set of standards that many vendors can implement. 4. Run Anywhere (WORA) to the server. 6. Sun supplies a comprehensive Java EE Compatibility Test Suite (CTS) to Java EE licensees.g.4 Summary 1. TopLink. JAX-WS 2. a. The latest version offers better alignment with JavaServer Pages. 5. The Java EE platform also offers CORBA support for tightly linking systems through remote method calls. especially for Web Services (JAX-WS 2. 2. Java EE technology makes it easier to connect the applications and systems you already have and bring those capabilities to the web. 3. and to devices. It now provides support for JAXB 2. The Java EE CTS helps ensure compatibility among the application server vendors which facilitates portability for the applications and components written for the Java EE platform. 5.

Java EE draws upon annotations and generics from Java SE virtually eliminating the need for deployment descriptors. Java EE also simplifies common coding issues by removing boilerplate code. Annotations allows declarative information to be moved inside the code and makes it much easier to deal with persistence. transactions. . and all the other powerful capabilities of Java EE. Java EE 5 takes drudgery out of creating applications and reduces the amount of time spent worrying about Java EE plumbing and thus allows developers to concentrate on the business logic. security. 9.7. web services. 8. 10. and providing a broader set of commonly used utility classes. relying upon reasonable defaults whenever possible. The enhancements in Java EE 5 greatly reduce the amount of code and configuration with which application developers have to contend improving their productivity.

e. an API based paradigm similar to Hibernate. c. c. Spring and EJB 3.0 has addressed the shortcomings of previous versions 4.1. 4. Entity beans are slow. This is a similar idea to “convention over configuration” in the Rails world. The verbose XML deployment descriptor has been made optional. Spring was developed as a reaction against EJB and this makes comparison between the two natural. d. b. .1 EJB 2. Long develop-test-debug cycle with little unit testing.x a. Object Relational Mapping and Object queries have been completely defined instead of being left up to container vendors to sort out.x. Remoting and global synchronization are overkill.0 a.1. it is a good time to re-evaluate how well EJB 3. f. Gives 90+% of what EJB does.1 Characteristics of EJB 2.0 is a specification. EJB 3 embraces POJO programming through annotations. for example both provide a mechanism to deliver middleware services to Java applications. EJB 3 makes heavy use of “intelligent defaulting” whenever possible. EJB 3 adopts JPA. Deployment descriptors difficulties c. Instead. d. Spring is an implementation while EJB 3. Much easier to develop. POJO programming. b.4 COMPARISON ANALYSIS Most importantly.2 Spring Framework a. test and deploy. TopLink and JDO. But they do have some areas of overlap.3 EJB 3.0 4.1. Made developer to write a bunch of redundant artifacts. Entity beans are half-baked and non-portable. The Entity Bean concept is no longer managed by the container. 4. Particularly now that a new version of EJB is available. b. e.

0 Below table describes the gives the comparison details of various concepts and services involved between EJB 3.0 and Spring framework. Web services Seamless support for JAX-WS 2. JMSTemplate adds nice abstraction over JMS. Transaction Management Works right out of the box. State management Robust support through Stateful Session Beans and Extended Persistence Context Indirect support dependent on web container session management Highly required 5. No Concepts/ Services EJB 3. Robust support through AspectJ and Spring AOP alliance. iBatis Highly required 4. JDBC. However. Need to add configuration for message listeners. but supports a number of strategies including JTA. JMS resources and JPA resources Can inject almost anything including lists. Highly required 7.0 Poor direct support.4. The column “Is required for Leicester?” will have the information about if it is really matters or not . Dependency Injection Can inject anything in the container including EJBs. maps. Messaging Supported out of the box through Message Driven Beans. Hibernate. properties and JNDI resources.0 Spring Framework Is required for Leicester? 1. but only JTA is supported Have to configure it to make it work.2 Comparison Matrix between Spring Vs EJB 3. data sources. Implementation Choice 2. Highly required 6. best integration available is via configuring XFire for registered beans. JDBC and Hibernate Highly required 3. Persistence Tightly integrated through JPA Framework support for JPA. May be . AOP Simple but limited support through interceptors.

Remote transactions and security are not supported.0 Spring Framework Is required for Leicester? 8. support beyond JAAS is possible through Acegi. However protocols other than RMI such as Hessian and Burlap are supported. Remoting support may be added via configuration. Simple scheduling possible through EJB Timer service Must add and configure Quartz for scheduling. Scheduling. May be 10. No Concepts/ Services EJB 3. However. Must add and configure Acegi security. Highly required 9. . Supports distributed transactions and security. May be or N/A Remoting Integrated support through Session Bean remote interfaces. Security Integrated support for declarative and programmatic security through JAAS..

But the trade-off for this added flexibility is increased complexity in configuration.1 Use Spring if: ¾ Your application requires fine-grained control at the container level. biased to certain technology) From the earlier sections of this document.0 5. and philosophy of configuration by exception make configuring EJB 3.e. It is totally upto the user’s discretion to choose one of them since both technologies have their own pros and cons and they do have their own work around for each disadvantage.0. annotations-based configuration. ¾ You use JSF and are considering using Seam. .0 provides less flexibility but its tight technology stack. Spring gives you more flexibility in many aspects of application development than EJB does— and this is particularly true with regards to persistence and transaction providers.2 Use EJB 3 if: ¾ You like annotations and dislike a lot of XML configuration. ¾ Standardization is an important consideration.) is important to our organization or application then it is good to simply go with EJB 3. ¾ Your application requires a lot of configuration beyond gluing together components and resources. JDBC. tooling. ¾ Your application is very stateful.1 Conclusion At this point we have learned many similarities between Spring framework and EJB 3. When standardization (and by extension vendor support. EJB 3.1. ¾ You need advanced AOP features.1.0 applications quite simple. (i. Standardization: While Spring integrates many standards such as JTA. we can summarize list of situations where it might be too appropriate to consider Spring or EJB 3.0. They are subjected to debatable forever and corresponding technology inventor would justify that “they are the best” and sometimes selection would be based upon the political decision too. ¾ You prefer a tightly integrated solution stack that makes sensible default choices for you and minimizes configuration. 5.5 CONCLUSIONS AND RECOMMENDATIONS 5. and JMS it is not itself a Java standard. etc.

0) as a technology solution for Leicester project. EJB3. with Java EE 5 it’s always possible to make use of some common integration points with the Spring Framework.0 and JPA combine naturally with Spring . which provides a very powerful solution for conversational interaction built on SFSBs and JSF. Specifically. Important Note: Luckily Spring and EJB 3. EJB 3.0 would work better for us when compared to Spring framework and because of these factors it is recommended to use Java EE 5 (EJB 3. There are very powerful ways of integrating these two technologies to take advantage of their relative strengths and weaknesses. ¾ ¾ ¾ ¾ ¾ State Management Configurability Standardization Web Service Security When the Leicester is highly stateful then it is necessary to consider EJB 3.5.0 SFSBs might be a good solution. When it is highly necessary. For all the above potential factors .2 Recommendations In order to choose the right choice (close to best) for Leicester we do take the following important factors for consideration and all the other factors for this project could be sufficed by both of them. For highly conversational applications we may want to consider SEAM.0 are not mutually exclusive choices.

com/stories/ 8.sun.x/reference/index.springframework.by Chris Richardson ¾ EJB3 IN ACTION – by Debu Panda.0.sun. The Spring Framework .html?page=1 6.html 5.jcp. Reza Rahman.org/spring/docs/2.onjava. Books Referred ¾ POJO IN ACTION .jsp&FP=/cont ent .html ¾ http://springframework. 5.onjava.org/ 2. Java EE ¾ http://java.com/javaee/ ¾ http://www.sun. Derek Lane 7.com/framework.org/en/jsr/detail?id=220 ¾ http://java. EJB3 projects in Industry ¾ http://blogs.com/pub/a/onjava/2005/06/29/spring-ejb3.com/developer/technicalArticles/Interviews/shannon_qa.com/pub/a/onjava/2004/01/14/aop. EJB3 Interviews ¾ http://www.Appendix A.com/interviews/Mike-Keith ¾ http://java.Reference ¾ http://static.sun.html 4. EJB3 support by Weblogic ¾ http://www.jsp?CNT=moreinfo_WLS10.3 References 1. AOP Aspect Oriented Programming ¾ http://www.0 Comparison ¾ http://www. Spring Vs EJB3.com/javaee/community/ 3.infoq.bea.

SEAM JBoss Seam is a powerful new application framework for building next generation Web 2. Seam has been designed from the ground up to eliminate complexity at the architecture and the API level. It enables developers to assemble complex web applications with simple annotated Plain Old Java Objects (POJOs). The simplicity of Seam 1. componentized UI widgets and very little XML. POJO Plain Old Java Object 7. Java Portlets and Business Process Management (BPM).4 Acronyms SL. . SFSB Stateful Session Bean 6. AOP Aspect Oriented Programming 4.0 will enable easy integration with the JBoss Enterprise Service Bus (ESB) and Java Business Integration (JBI) in the future.0 applications by unifying and integrating technologies such as Asynchronous JavaScript and XML (AJAX). Enterprise Java Beans (EJB3). Java EE Java Enterprise Edition 5. IoC Inversion of Control 2. No Acronym Explanation 1.5. Java Server Faces (JSF). DI Dependency Injection 3.