The Spring FrameworkTM - PowerPoint PPT Presentation


PPT – The Spring FrameworkTM PowerPoint presentation | free to view - id: 1ffae5-ZTVjY


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation

The Spring FrameworkTM


... in a snappy phrase, but our users love it. Solid, robust, works now ... Much easier to unit test web tier, because Spring uses ... is a match made in ... – PowerPoint PPT presentation

Number of Views:76
Avg rating:3.0/5.0
Slides: 51
Provided by: rod104


Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: The Spring FrameworkTM

The Spring FrameworkTM
Rod Johnson Interface21
Spring framework goals
  • Make J2EE easier to use
  • Address end-to-end requirements rather than one
  • Eliminate need for middle tier glue
  • Provide the best Inversion of Control solution
  • Provide a pure Java AOP implementation, focused
    on solving common problems in J2EE
  • Fully portable across application servers
  • Core container can run in any environment, not
    only a server
  • Works well in WebSphere

Spring framework goals
  • Non-invasive framework
  • Application code has minimal or no dependency on
    Spring APIs
  • Key principal seen throughout Springs design
  • More power to the POJO
  • Facilitate unit testing
  • Allow effective TDD
  • Allow business objects to be unit tested outside
    the container
  • Facilitate OO best practice
  • Were used to implementing EJB or J2EE
    applications rather than OO applications.
  • It doesnt have to be this way.
  • Provide a good alternative to EJB for many
  • Enhance productivity compared to traditional
    J2EE approaches

Part of a new wave of frameworks
  • Big change in how J2EE applications are written
  • Less emphasis on EJB
  • EJB has been overused Often not appropriate
  • Not just Spring PicoContainer, HiveMind and
    other Inversion of Control frameworks
  • EJB 3.0 (2005) programming model looks a lot like
  • (Spring IoC features) Hibernate
  • EJB still has some unique capabilities for a
    minority of apps (distributed transaction
    management, RMI remoting)
  • These lightweight frameworks are different
  • Spring is the most mature, most powerful and most

Unique Spring capabilities
  • Declarative transaction management for POJOs with
    or without EJB
  • Consistent approach to data access, with common
    exception hierarchy
  • Simplifies working with JDBC, Hibernate, JDO etc
  • Flexible MVC framework
  • IoC/AOP integration
  • Integration with a wide variety of popular
  • Gestalt
  • More than the sum of its parts
  • Hard to explain in a snappy phrase, but our users
    love it
  • Solid, robust, works now
  • In production in mission-critical apps now

Spring focus
  • Spring complements application servers like
  • Spring comes out of the application space, rather
    than the server space
  • Spring avoids the need for costly in-house
  • Can produce real savings
  • Focus your developers on your domain
  • Well-understood, generic, high-quality solution
  • Facilitates testability, increase productivity
  • Provides a simpler, yet powerful, alternative to
    the EJB component model in many applications
  • But also plays well with EJB if you prefer to
    stick with EJB

A layered framework
  • Web MVC
  • AOP framework
  • Integrates with IoC
  • IoC container
  • Dependency Injection
  • Transaction management
  • Data access
  • One stop shop but can also use as modules

  • Most similar in design to Struts
  • Single shared Controller instance handles a
    particular request type
  • Controllers, interceptors run in the IoC
  • Important distinguishing feature
  • Spring eats its own dog food

Web MVC Advantages over Struts
  • Allows multiple DispatcherServlets
  • More elegant than even Struts 1.1 solution
  • DispatcherServlets can share an application
  • More flexible
  • Interface not class-based
  • Easier to customize
  • Less tied to JSP
  • Velocity, Excel, PDF etc
  • Easy to implement custom Views

Web MVC Advantages over Struts
  • Cleaner MVC separation
  • Cleanly separates controller, model and view
  • Model is not tied to Servlet API or Spring API
  • No need for custom ActionForms can reuse domain
    objects or TOs
  • Much easier to unit test web tier, because Spring
    uses interfaces, not classes
  • Integrates with middle tier with zero custom
  • No more Service Locators or ad hoc Singletons

Web tier integration
  • But I love WebWork/Tapestry/Struts/JSF/whatever
  • The customer is always right
  • We dont dictate how to use Spring
  • You can preserve your investment (tools etc)
  • You can refactor to use what parts of Spring you
  • WebWork, Tapestry, Struts integration is seamless
  • Support for JSF and Portlet development in Spring
    1.1 (August)
  • JSF leaders (Ed Burns, David Geary) are working
    with us on Spring/JSF integration

Spring in the Middle Tier
  • Complete solution for managing business objects
  • Write your business objects as POJOs
  • Spring handles wiring and lookup
  • Simple, consistent, XML format (commonest choice)
  • But the IoC container is not tied to XML
  • Application code has few dependencies on the
    containeroften no dependencies on the container
  • Spring Pet Store has no dependencies on Spring
  • No magic annotations for IoC nothing
  • Easy unit testing. TDD works!

Spring in the Middle Tier
  • The most complete IoC container
  • Setter Dependency Injection
  • Configuration via JavaBean properties
  • Constructor Dependency Injection
  • Configuration via constructor arguments
  • Pioneered by PicoContainer
  • Dependency Lookup
  • Avalon/EJB-style callbacks
  • I favour Setter Injection, but the Spring
    philosophy is that you make the choice
  • We are not ideological.
  • A good IoC container must provide sophisticated
    support for both injection models to allow use of
    legacy code

Middle Tier Setter Injection
  • public class ServiceImpl implements Service
  • private int timeout
  • private AccountDao accountDao
  • public void setTimeout(int timeout)
  • this.timeout timeout
  • public void setAccountDao(AccountDao
  • this.accountDao accountDao
  • // Business methods from Service
  • ltbean id"service" class"com.mycompany.service.Se
  • ltproperty name"timeout"gtltvaluegt30lt/valuegtlt/prope
  • ltproperty name"accountDao"gtltref
  • lt/beangt

Middle Tier Constructor Injection
  • public class ServiceImpl implements Service
  • private int timeout
  • private AccountDao accountDao
  • public ServiceImpl (int timeout, AccountDao
  • this.timeout timeout
  • this.accountDao accountDao
  • // Business methods from Service
  • ltbean id"service" class"com.mycompany.service.Se
  • ltconstructor-arggtltvaluegt30lt/valuegtlt/constructor-a
  • ltconstructor-arggtltref local"accountDao"/gtlt/const
  • lt/beangt

Middle Tier Dependency Injection
  • Simple or object properties
  • Configuration (timeout)
  • Dependencies on collaborators (accountDao)
  • Configuration properties are also important
  • Can run many existing classes unchanged
  • Autowiring
  • Trivial to test application classes outside the
    container, without Spring
  • Can reuse application classes outside the
  • Hot swapping, instance pooling (with AOP)

Spring in the Middle Tier
  • Advanced IoC features
  • Can manage lists, maps or sets, with arbitrary
  • Leverages standard JavaBeans PropertyEditor
  • Register custom property editors
  • Post processors

Why AOP?
  • AOP complements IoC to deliver a non-invasive
  • Externalizes crosscutting concerns from
    application code
  • Concerns that cut across the structure of an
    object model
  • AOP offers a different way of thinking about
    program structure to an object hierarchy
  • EJB interception is conceptually similar, but not
    extensible and imposes too many constraints on
  • Spring provides important out-of-the box aspects
  • Declarative transaction management for any POJO
  • Pooling
  • Resource acquisition/release

AOP IoC A unique synergy
  • AOP IoC is a match made in heaven
  • Any object obtained from a Spring IoC container
    can be transparently advised based on
  • Advisors, pointcuts and advices can themselves be
    managed by the IoC container
  • Spring is an integrated, consistent solution

Custom AOP
  • Complements, rather then conflicts with, OOP
  • Email administrator if a particular exception is
  • Apply custom declarative security checks
  • Performance monitoring
  • Auditing
  • Caching

AspectJ integration
  • Coming in Spring 1.1 (August)
  • Particularly relevant to WebSphere users given
    IBMs backing for AspectJ
  • Integrates AspectJ aspects into Spring IoC
  • Configure and parameterize aspects in a
    consistent way
  • Will allow the use of the AspectJ pointcut
    expression language to target Spring advice
  • Can mix Spring and AspectJ aspects within a
    consistent architectural model

Spring DAO
  • Integrated with Spring transaction management
  • Unique synergy
  • Gestalt again
  • Doesnt reinvent the wheel.
  • There are good solutions for O/R mapping, we make
    them easier to use
  • Out-of-the-box support for
  • JDBC
  • Hibernate
  • JDO
  • iBATIS
  • Model allows support for other technologies
    (TopLink etc)
  • Consistent DataAccessException hierarchy allows
    truly technology-agnostic DAOs

Spring DAO Consistent exception hierarchy
  • Class library offers simpler programming model
    than raw JDBC
  • Two flavours of usage
  • Callbacks (JdbcTemplate)
  • JDBC objects Model queries, updates and stored
    procedures as objects
  • No more try/catch/finally blocks
  • No more leaked connections
  • Spring will always close a connection no scope
    for programmer error
  • Meaningful exception hierarchy
  • No more vendor code lookups
  • Spring autodetects database and knows what
    Oracle, DB2 error codes mean
  • More portable code
  • More readable code
  • catch (BadSqlGrammarException ex)
  • Stored procedure support
  • Can refactor to clean up JDBC without adopting
    Spring overall
  • Incremental adoption Step by step

Spring DAO Hibernate
  • Manages Hibernate sessions
  • No more custom ThreadLocal sessions
  • Sessions are managed within Spring transaction
  • Works with JTA if desired
  • Works within EJB container with CMT if desired
  • HibernateTemplate makes common operations easy
  • Simpler, consistent exception handling
  • Many operations become one-liners
  • Less, simpler, code compared to using Hibernate
  • Portability Switch between Hibernate, JDO and
    other transparent persistence technologies
    without changing DAO interfaces
  • Can even switch to JDBC where transparent update
    is not implied
  • Mixed use of Hibernate and JDBC within the same

HibernateTemplate DAO example
  • public class MyHibernateDao implements MyDao
  • private HibernateTemplate hibernateTemplate
  • public MyHibernateDao (net.sf.hibernate.SessionFa
    ctory sessionFactory)
  • hibernateTemplate new HibernateTemplate(sessio
  • public Collection getWorkflows()
  • return hibernateTemplate.find("from Workflow")
  • ltbean id"sessionFactory" class"org.springframewo
  • ltproperty name"dataSource"gtltref
  • ltproperty name"mappingResources"gt
  • ltvaluegtmycompany/mappings.hbm.xmllt/valuegt
  • lt/propertygt
  • ltproperty name"hibernateProperties"gt

Spring DAO JDO
  • Comparable to Hibernate support
  • Mixed use of JDO and JDBC within the same
  • Will support JDO 2.0 features (detach) and vendor
    extensions in a portable manner
  • All major vendors support similar concepts

Spring Transaction
  • Consistent abstraction
  • PlatformTransactionManager
  • Does not reinvent transaction manager
  • Choose between JTA, JDBC, Hibernate, JDO etc with
    simple changes to configuration not Java code
  • No more rewriting application to scale up from
    JDBC or Hibernate local transactions to JTA
    global transactions
  • Use the simplest transaction infrastructure that
    can possibly work
  • Programmatic transaction management
  • Simpler API than JTA
  • Use the same API for JTA, JDBC, Hibernate etc.
  • Write once have transaction management

Declarative Transaction Management
  • Most popular transaction management option
  • Built on same abstraction as programmatic
    transaction management
  • Declarative transaction management for any POJO,
    without EJB even without JTA (single database)
  • More flexible than EJB CMT
  • Declarative rollback rules roll back on
  • Non-invasive Minimizes dependence on the
  • No more passing around EJBContext

Make ServiceImpl POJO transactional
  • public class ServiceImpl implements Service
  • private int timeout
  • private AccountDao accountDao
  • public void setTimeout(int timeout)
  • this.timeout timeout
  • public void setAccountDao(AccountDao accountDao)
  • this.accountDao accountDao
  • public void doSomething() throws
  • ltbean id"serviceTarget" class"com.mycompany.serv
  • ltproperty name"timeout"gtltvaluegt30lt/timeoutgtlt/pro
  • ltproperty name"accountDao"gtltref

Make ServiceImpl transactional
  • ltbean idservice" classorg.springframework.tran
  • ltproperty name"target"gt
  • ltref local"serviceTarget"/gt
  • lt/propertygt
  • ltproperty name"transactionManager"gt
  • ltref local"localTransactionManager"/gt
  • lt/propertygt
  • ltproperty name"transactionAttributes"gt
  • ltpropsgt
  • ltprop key"do"gt
  • PROPAGATION_REQUIRED,-ServiceWithdrawnExcep
  • lt/propgt
  • lt/propsgt
  • lt/propertygt
  • lt/beangt

Make ServiceImpl transactional
  • Rollback rule means that we dont need to call
  • Of course Spring also supports programmatic
  • Can run this from a JUnit test case
  • Doesnt depend on a heavyweight container
  • Can work with JTA, JDBC, Hibernate, JDO, iBATIS
  • Just change definition of transaction manager

Make ServiceImpl transactional
  • Alternative approaches, simpler in large
  • Use auto proxy creator to apply similar
    transaction attributes to multiple beans
  • Use metadata or another pointcut approach to
    apply transactional behaviour to multiple classes

Spring J2EE
  • No more JNDI lookups
  • JndiObjectFactoryBean
  • Generic proxy for DataSources etc.
  • No more EJB API dependencies, even in code
    calling EJBs
  • EJB proxies
  • No more home.create()
  • No more Service Locators or Business Delegates
  • Codeless EJB access
  • Callers depend on Business Methods interface, not
  • Exposed via Dependency Injection (naturally)
  • Maximize code reuse by minimizing J2EE API
  • Spring does not prevent you using the full power
    of J2EE
  • Full power of WebSphere lies under the covers
  • Spring makes it easier to use effectively

Spring OOP
  • No more Singletons
  • An antipattern as commonly used
  • Program to interfaces, not classes
  • Facilitates use of the Strategy pattern
  • Makes good OO practice much easier to achieve
  • IoC Dependency Injection keeps the container from
    messing up your object model
  • Base object granularity on OO concerns, not
    Spring concerns
  • Combine with transparent persistence to achieve a
    true domain model

Spring Productivity
  • Less code to develop in house
  • Focus on your domain
  • Reduced testing effort
  • Try it and you wont look back
  • Old J2EE has a poor productivity record
  • Need to simplify the programming model, not rely
    on tools to hide the complexity

Old J2EE vs Spring
  • Write a SLSB
  • Home interface
  • Component interface
  • Business methods interface
  • Bean implementation class
  • Complex XML configuration
  • POJO delegate behind it if you want to test
    outside the container
  • Much of this is working around EJB
  • If you want parameterization it gets even more
  • Need custom code, IoC container or environment
    variables (ouch)
  • Implement a Spring object
  • Business interface
  • Implementation class
  • Straightforward XML configuration
  • The first two steps are necessary in Java anyway
  • Oops, I really meant implement a Java object,
    not implement a Spring object
  • If you want to manage simple properties or object
    dependencies, its easy

Old J2EE vs Spring
  • Use your SLSB
  • Write a Service Locator and/or Business Delegate
    need JNDI code
  • Each class that uses the service needs to depend
    on EJB interface (home.create) or you need a
    Business Delegate with substantial code
  • Hard to test outside a container
  • Use your Spring object
  • Just write the class that uses it in plain old
  • Express a dependency of the business interface
    type using Java (setter or constructor)
  • Simple, intuitive XML configuration
  • No lookup code
  • Easily test with mock object

Productivity dividend
  • Spring removes unnecessary code
  • You end with no Java plumbing code and relatively
    simple XML
  • If your Spring XML is complex, youre probably
    doing things you couldnt do the old way without
    extensive custom coding
  • The old way you have lots of Java plumbing code
    and lots of XML
  • A lot of the XML is not standard
  • Combine with Hibernate or JDO for transparent
    persistence and the advantage is huge

Quotes Plenty of choice from Spring users
  • I use the Spring Framework daily and I've simply
    been blown away by how much easier it makes
    development of new software components
  • Spring lets me port my business logic across
    those environments application server, Swing
    client with ease. That's why I love Spring.

Quotes Plenty of choice from Spring users
  • Lightweight containers make a lot of sense.
    Springs ability to decouple layers within an
    applications is very addictive.
  • You will wonder how you ever developed anything
    in Hibernate without spring, it just makes it so
    much easier. You gotta love HibernateTemplate.
    You gotta love the session management.
  • The proof of concept went up to 150 requests per
    second! Man, you guys did a hell of job with the
    whole thing. Spring MVC overhead is minimal and
    it took only 15 hours to implement it, thanks for
    the dependency injection!

  • I'm a new user of spring and I have to say, it's
    great.  It's really speeding up our development
  • One of the great things for me about the Spring
    Framework is that it makes using some of those
    "old" technologies easier to bear package is
    nice to use to reduce the amount of duplicate
    code around EJBs
  • Spring now has the momentum to dominate the J2EE
    framework space OO guru and consultant Craig

The Spring community
  • 15 developers
  • Around 6 core developers
  • Significant number of contributors
  • Architects and key developers
  • Rod Johnson
  • Juergen Hoeller
  • Test-first development on the framework
  • Vibrant community
  • Very active mailing lists and forums
  • JIRA Issue tracker at Atlassian
  • Over 50,000 downloads total

The Spring community
  • At least six books coming out in 2004
  • Spring Live (June) Matt Raible
  • J2EE Without EJB (May) Johnson/Hoeller
  • Professional Spring Development (Q4)
  • Better, Faster Lighter Java (Bruce Tate)
  • Bruce is also writing an introductory Spring book
    for OReilly (due out Q4)
  • Manning Spring in Action (Q4)
  • Related projects
  • Acegi Security for Spring

Spring services Interface21
  • Training
  • Commercial support
  • Consulting
  • We offer more choices for companies who wish to
    form a strategic partnership with us
  • We provide unique Spring and J2EE expertise

Whos using Spring (partial list)
  • Banking
  • Global investment bank
  • 2 projects live with Spring MVC, IoC, AOP, JDBC
    10,000 users daily (whole Intranet)
  • Even bigger project rolling out this month
  • German domestic bank
  • Leading US bank online banking service (8m hits
    per day)
  • At least three more global banks to my knowledge
  • Several household names in US
  • Defence
  • Publishing
  • Health care (WebSphere)
  • Power generation (WebSphere)

Whos using Spring (partial list)
  • Government/NGO
  • European Commission
  • WHO
  • CERN
  • Several universities in the US and UK, including
  • Rutgers University (New Jersey)
  • Warwick University (UK)
  • uPortal project (US University portal)
  • Many sophisticated websites
  • Used by several consultancies on multiple client
  • Major European delivery tracking service
  • Nominet
  • FA Premier League football

Where it all began
  • Describes the motivation for Spring and basic
  • Practical approach to J2EE development that works
  • One of the first expressions of lightweight J2EE

Episode 2
  • Describes the Lightweight Container Architecture
    in detail
  • Practical guide to more efficient, more
    productive J2EE
  • Not just a polemic
  • Not purely about Spring, but
  • Uses Spring for all examples
  • Discusses all key parts of Spring