Lightweight J2EE Architecture - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

Lightweight J2EE Architecture

Description:

JDBC abstraction and DAO module. Object/relational mapping integration module. Web module ... Supports Vendor Abstraction and Independence. Container and DB ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 39
Provided by: esage
Category:

less

Transcript and Presenter's Notes

Title: Lightweight J2EE Architecture


1
Lightweight J2EEArchitecture
2
Introduction
  • eSage Group
  • Consulting company specializing in agile project
    management system architecture and development
  • Mike Porter
  • Co-founder and System Architect
  • mike_at_esagegroup.com
  • Charlie Morss
  • System Architect
  • charlie_at_esagegroup.com

3
Overview
  • Review current state of J2EE
  • Lightweight Frameworks and what they can do
  • Todays Frameworks
  • Spring
  • Hibernate
  • Spring and Hibernate Integration
  • Demos along the way
  • Review

4
Current J2EE State
  • J2EE has done a great deal to standardize our
    industry
  • These J2EE services are tremendously successful
    with wide adoption
  • JDBC
  • Servlet/JSP/WAR
  • JNDI
  • JMS
  • Useful EJB Services
  • Declarative transactions support
  • Persistence
  • Security
  • Distributed component model
  • Instance pooling and caching

5
Genesis of EJB
  • 1998 Distributed Computing was the big buzz-word
  • Sun made EJBs remote to compete with technologies
    like CORBA
  • The specification was intrusive
  • And we have been dealing with these decisions
    ever since
  • Lets take a look at the Sun Best practices.

6
Uhh we have a problem here
7
EJB Issues
  • Intrusive Noisy
  • Must be an EJBObject
  • Must run within the container
  • Rapid iterative code/debug/test cycles difficult
  • If the Sun Pet Store is the best, were in
    trouble
  • Too many different patterns
  • Performs poorly
  • Reliance on Entity Beans
  • Too many different files needed to do simple
    things

8
Treating the Symptoms
  • Tools exist to make EJB development easier
  • XDoclet
  • IDE Wizards
  • Cactus
  • Work-a-rounds
  • Data Transfer Objects (DTOs)
  • Service Locator

9
The Way Ahead
  • Development teams need a better frameworks that
    supports these ideas
  • Implementing business requirements first
  • Simplicity
  • Productivity
  • Testability
  • Object Orientation
  • Lightweight Frameworks can get us there

10
What are Lightweight Frameworks?
  • Non-intrusive
  • No container requirements
  • Simplify Application Development
  • Remove re-occurring pattern code
  • Productivity friendly
  • Unit test friendly
  • Very Pluggable
  • Usually Open Source

11
Leading Lightweight Frameworks
  • Spring, Pico, Hivemind
  • Hibernate, IBatis, Castor
  • WebWork
  • Quartz
  • Sitemesh
  • JBoss is NOT a lightweight framework

12
Spring
13
Spring is in the Air
  • Spring is a glue framework that ties many
    frameworks together in a consistent way
  • Inversion of Control Framework (Dependency
    Injection)
  • Dont call us well call you
  • Service Injection rather than Service Lookup
  • Encourages programming to Interfaces
  • Reduces coupling between components
  • Supports Testing
  • Configuration
  • Supports externalizing the configuration
  • And more

14
Spring Modules
  • Core IoC container
  • Application context module
  • AOP module
  • JDBC abstraction and DAO module
  • Object/relational mapping integration module
  • Web module
  • MVC module
  • Quartz, Email, WebServices Integration and more.

15
Why use IoC?
16
IoC Example
  • Review Attached Spring DataSource IoC Example

17
IoC Types
  • Three basic types
  • Type I
  • Interface Injection
  • Your service or component implement a specific
    interface
  • Much like EJBs
  • Type II
  • Method Injection
  • Dependent objects are provided to the object by
    methods
  • Based on JavaBeans constructs
  • Ordering of method calls may not be achievable
  • Type III
  • Constructor Injection
  • Dependent objects are provided to the object by
    constructors
  • Large constructor argument lists

18
AOP Introduction
  • AOP Aspect Oriented Programming
  • Separates concerns of an object into their own
    aspects
  • Helps to remove infrastructure code from the
    application
  • AOP can remove replicated code that is scattered
    across an entire system
  • Used to apply cross-cutting concerns uniformly to
    objects.
  • Logging
  • Security
  • Transaction Management

19
AOP Crosscutting
  • Without AOP
  • With AOP

20
AOP Definitions
  • Aspect - An application wide concern that is
    often duplicated in many classes, that could be
    implemented before, after, or upon failure across
    a group of methods.
  • JointPoint - A join of methods across which an
    aspect can be implemented.
  • Spring allows method interception. This means
    when a method is called the framework can attach
    functionality.
  • Pointcut - Description of the collection of
    methods for which advice is to be applied
  • Introduction - Adding methods or properties to a
    class with advice.
  • AOP Proxies - Surrogate object that invokes
    advice and advises the invoked class

21
AOP Example
  • Spring AOP Code Example

22
Spring Tips and Tricks
  • Start with just the IoC and build from there
  • Be careful with the autowire changes in
    Interfaces and refactoring can change autowire
    rules
  • AOP supports proxying of classes via interface
    and via CGLib
  • Interfaces are recommended to reduce coupling
  • CGLib is available to support legacy code or 3rd
    party code
  • Methods marked as final can not be advised
  • Dynamic Pointcuts (ControlFlowPointcuts) can be
    slow so try to avoid them
  • Avoid applying Advise to fine grained objects
  • Use autoproxing when you want system wide advice

23
Break Time?
24
Hibernate
25
Why ORM?
  • Productivity
  • Shield developers from messy SQL
  • Maintainability
  • Fewer lines of typically redundant code
  • Performant
  • Good ORM Solutions can perform better
    alternatives
  • Program to the Business Domain rather than to the
    Data Model
  • Domain Objects with encapsulated behavior
  • Support for Natural Object Navigation

26
What is Hibernate
  • A high performing ORM framework
  • Community driven distilled over years
  • Persistent Classes are POJOs
  • Easier to write and refactor
  • Can be serialized
  • Can execute outside a container (TEST TEST TEST)
  • Hibernate 3 is the model for EJB3
  • The core to JBoss EJB3

27
Why Use Hibernate
  • Reduces code
  • SQL Mapping Localized
  • Easy programming model (POJO)
  • Performant
  • Advanced Cache Strategy
  • Transparent Write-Behind
  • Optimized SQL-Never updates clean data
  • Lazy and Eager Association fetching strategies
  • Supports Vendor Abstraction and Independence
  • Container and DB

28
Hibernate Features
  • Supports fine grained object mapping. IE A
    Customer can be composed of Address Objects
  • True Polymorphic Associations and Mapping
  • Three types of Inheritance Mapping Strategies
  • Rich relationship types one-to-one, one-to-many,
    many-to-many, etc
  • Detached Objects No more DTOs
  • Full JTA Support
  • Excellent Spring Integration

29
Querying in Hibernate
  • HQL - Hibernate Query Language
  • SQL-esque but Object Oriented
  • Understands inheritance, polymorphism,
    association
  • Criteria API
  • Build a set of objects which represent your query
  • Also supports query by example
  • Both support limit queries
  • setFirstResults() and setMaxResults() methods
    exist on Criteria and Query (HQL)

30
Hibernate Examples
  • Review Attached Hibernate Example

31
Hibernate Tips
  • Turn on SQL logging
  • hibernate.show_sql true
  • Set lazytrue for most collections (default value
    is true)
  • Eager Fetching of a query does not result in a
    distinct list
  • return new HashSet(originalResults)
  • Cache your entities, and make sure to apply the
    cache settings to their association mappings as
    well (set, map, bag, list)
  • Be aware of Cascade Deletes
  • We have only had limited success with this
    feature
  • Understand the First Level Cache
  • Read Hibernate in Action

32
Hibernate Tips Cont
  • Implement the equals() and hashCode() methods
  • Use the natural key, not the object Id
  • Alternatively use a GUID initialized at object
    creation
  • Must be used in conjunction with the ltversiongt
    unsaved-value attribute
  • ltversion name"version" column"VERSION"
    unsaved-valuenull" /gt

33
Spring Hibernate Integration
34
Spring Hibernate Integration
  • HibernateTemplate
  • Many convenience methods
  • Use is restricted to unchecked exceptions
  • Uses anonymous inner class callbacks
  • Hibernate AOP
  • Any exception types can be thrown within the data
    access code
  • DAO must extend HibernateDAOSupport
  • Very clean DAO methods

35
Spring Transactions
  • Clear application layering supporting most data
    access and transaction technologies
  • Not bound to a container
  • Transactions can either be managed
    programmatically or declarative using AOP
  • TransactionTemplate
  • Allows execution of transactional code without
    the need to re-implement transaction workflows
    (Exception Handling)
  • Triggers a rollback if the callback throws a
    runtime exception or if it sets the transaction
    to rollback-only
  • TransactionInterceptor
  • Business object does not need to be transaction
    aware (similar to EJB)
  • Check and unchecked exceptions supported
  • Default rollback behavior is to rollback on
    runtime exceptions but this can be configured

36
Spring Hibernate Integration Examples
  • Spring Hibernate Code Example

37
Questions???
  • Questions and Comments?

38
References
  • Jason Careiras Blog
  • www.jroller.com/page/jcarreira/20041215
  • Hibernate in Action
  • Gavin King and Christian Bauer
  • J2EE Development without EJB
  • Rod Johnson
  • Spring in Action
  • Craig Walls and Ryan Breidenbach
Write a Comment
User Comments (0)
About PowerShow.com