Spring MVC to EJB 3.
0
  Introduction
Yet Another
Easy Enterprise
            WebDevelopment
                Framework
                             copyright 2009 Trainologic LTD
Spring MVC –to
Introduction part
               EJB1 3.0
• Introduction
  Overview
• The EJB
      MVCModel
          Pattern
• Key
  Spring
      Services
         MVC Implementation
               of the Application Server
• Controllers
• Mapping Request Handlers
• Views
• I18N and Locale Resolvers
• Customizing Look & Feel using Themes
• File Uploading
• Error Handling
• Using Annotations                   copyright 2009 Trainologic LTD
EJB 3.0
                      Introduction
• EJB (Enterprise Java Beans) is one of the main
  standards under the JavaEE umbrella.
• Version 3.0 is part of JavaEE 5.
• As we are going to see, EJB3 provides a great solution
  for building enterprise applications.
                              3
                                                copyright 2009 Trainologic LTD
EJB 3.0
          Relation to Other Standards
• Many standards exist in Java which have the ‘beans’
  word in it:
    • JavaSE – Java Beans.
    • JMX – Managed Beans.
    • JSF – Managed Beans.
    • JavaEE – Enterprise Java Beans.
• There is no relationships between those standards!
  (except for the word ‘bean’).
                             4
                                               copyright 2009 Trainologic LTD
EJB 3.0
              The Motivation for EJB
• Question: why do we need EJB?
• How can it help us in our enterprise applications?
• Answer: when working with EJB we are provided with
  out-of-the-box middleware services that ease our
  development and allow us to focus on the business logic.
• E.g., transactions, messaging, resource management,
  clustering, security, etc…
                               5
                                               copyright 2009 Trainologic LTD
EJB 3.0
              The Application Server
• EJBs must be deployed into an Application Server.
• The application server provides the middleware services
  that the EJBs require.
                            6
                                               copyright 2009 Trainologic LTD
EJB 3.0
                        Versions
• EJB 3.0 is a part of JavaEE 5.
• The previous version (2.1) was part of J2EE 1.4.
• EJB3 greatly improved the usability of the EJBs.
• By making use of annotations, it is now very easy to
  write EJBs and to configure them.
• The next version (3.1) will be a part of JavaEE 6.
                             7
                                                 copyright 2009 Trainologic LTD
Introduction to EJB 3.0
• Overview
• The EJB Model
• Key Services of the Application Server
                                      copyright 2009 Trainologic LTD
EJB 3.0
                    The EJB Model
• It is important to understand how EJBs work and why
  the creators of the standard did it this way.
• Remember, the motivation behind EJBs is to have
  middleware services that will be provided by the
  Application Server!
• Let’s see how this can be done…
                              9
                                                  copyright 2009 Trainologic LTD
EJB 3.0
                     Example Scenario
• Let’s say we have the following business logic class:
   public class Store {
      public void submitOrder(Order o) {
            // do the business logic
      }
   }
 • And now, let’s say that we want the Application Server
   to wrap the submitOrder method with a transaction.
 • Of course, if we invoke the method directly, the
   Application Server will not be able to intercept and inject
   the middleware services!
                                    10
                                                  copyright 2009 Trainologic LTD
EJB 3.0
                    The EJB Object
• The only way the Application Server can perform its
  middleware services, is by having the invocation pass
  through a mediator object.
• Known as the EJB Object.
                                              Add
                                          Transaction
                                             Logic
              submitOrder     EJB     submitOrder
     Client                                             Store
                             Object
                             11
                                                        copyright 2009 Trainologic LTD
EJB 3.0
                   The EJB Object
• It is important to mention that the EJB Object is
  automatically generated by the Application Server.
• Another important conclusion: you must never ever
  make a direct invocation of the EJB class!
• All invocations must be passed through the EJB Object.
                             12
                                                 copyright 2009 Trainologic LTD
EJB 3.0
                Creating Instances
• Ok, if in order to use an EJB we need to have a
  reference to the EJB Object, how do we get it?
• Do we use the new keyword?
• The answer is: No.
• We need to ask the Application Server to generate an
  EJBObject for us (and we need to tell it for which bean).
• So, we use the JNDI…
                             13
                                                   copyright 2009 Trainologic LTD
EJB 3.0
          The Full Picture
                            Application Server
 Client       JNDI                   EJB Container
 Code          EJB
                                  EJB                Bean
              Object
                                 Object              Class
              Proxy
                       14
                                                 copyright 2009 Trainologic LTD
EJB 3.0
                      Restrictions
• When developing EJBs, the specification forbids the use
  of a number of Java APIs:
    • You must not create threads, manage threads or
      synchronize with other beans.
    • You must not load a native library.
    • You must not use System.exit().
    • You must make changes to the filesystem.
    • You must not use GUI (AWT & Swing).
    • You must not read/write nonfinal static fields.
                              15
                                                 copyright 2009 Trainologic LTD
EJB 3.0
                      Restrictions
    • You must not listen to connections (ServerSocket).
    • You must not pass a direct reference from one EJB to
      another (can’t pass this to another EJB).
• Breaking these restrictions will result in vendor
  dependant, unexpected behavior and security
  vulnerabilities.
                             16
                                                  copyright 2009 Trainologic LTD
EJB 3.0
                       EJB Types
• There are 2 types of EJBs:
    • Session Beans – handle client invocations.
    • Message Driven Beans (MDB) – handle messaging.
• We will also learn to use Entities which are part of JPA
  (Java Persistence API).
• Note, Entities are not EJBs.
                               17
                                                  copyright 2009 Trainologic LTD
Introduction to EJB 3.0
• Overview
• The EJB Model
• Key Services of the Application Server
                                      copyright 2009 Trainologic LTD
EJB 3.0
                 Supplied Services
• The Application Server supplies the following services for
  EJBs:
    • Concurrency – all multithreading/ concurrency
      issues. You are not allowed to use multi-threaded
      code in your EJBs.
    • Resource Management – the life-cycle of the EJBs,
      namely creating, destroying and reusing EJBs.
                             19
                                                 copyright 2009 Trainologic LTD
EJB 3.0
                  Supplied Services
    • Transactions – distributed/global transactions for the
      EJBs.
    • Security – role-based security for the EJBs.
    • Messaging – there is a special type of EJB (Message
      Driven Bean) that handles messaging.
    • Scheduling –   scheduling services for EJBs.
                              20
                                                     copyright 2009 Trainologic LTD
EJB 3.0
                  Supplied Services
    • Persistence – through the JPA specification, EJBs can
      use persistence services.
    • Clustering – Although not part of the JavaEE
      specification, most Application Servers provide
      clustering services (load balancing & failover).
                              21
                                                  copyright 2009 Trainologic LTD
EJB 3.0
                         More Details
• In this course we’ll discuss most of the aforementioned
  services in details.
• But not clustering.
                              22
                                                copyright 2009 Trainologic LTD