Spring

From HaFrWiki
Jump to: navigation, search

The Spring Framework [1] is an open source application framework for the Java platform and .NET Framework (Spring.NET).

Spring Overview

Spring contains a lot of functionality and features, which are well-organized in seven modules shown in the diagram below.

  • The Core package is the most fundamental part of the framework and provides the Dependency Injection features allowing you to manage bean container functionality. The basic concept here is the BeanFactory, which provides a factory pattern removing the need for programmatic singletons and allowing you to decouple the configuration and specification of dependencies from your actual program logic.
    • Supporting utilities
    • Bean container
  • On top of the Core package sits the Context package, providing a way to access beans in a framework-style manner, somewhat resembling a JNDI-registry. The context package inherits its features from the beans package and adds support for text messaging using e.g. resource bundles, event-propagation, resource-loading and transparent creation of contexts by, for example, a servlet container.
    • Application context
    • UI support
    • Validation
    • JNDI, EJB support and Remoting
    • Mail
  • Spring DAO package provides a JDBC-abstraction layer that removes the need to do tedious JDBC coding and parsing of database-vendor specific error codes. Also, the JDBC package provides a way to do programmatic as well as declarative transaction management, not only for classes implementing special interfaces, but for all your POJOs (plain old java objects).
    • Transaction infrastructure
    • JDBC Support
    • DAO Support
  • Spring ORM [2] package provides integration layers for popular object-relational mapping APIs, including JDO, Hibernate and iBatis. Using the ORM package you can use all those O/R-mappers in combination with all the other features Spring offers, like simple declarative transaction management mentioned before.
  • Spring AOP provides an AOP Alliance compliant aspect-oriented programming implementation allowing you to define, for example, method-interceptors and point cuts to cleanly decouple code implementing functionality that should logically speaking be separated. Using source-level metadata functionality you can incorporate all kinds of behavioural information into your code, a little like .NET attributes.
    • Source-level metadata
    • Aspect Oriented Programming infrastructure
  • Spring's Web package provides basic web-oriented integration features, such as multipart functionality, initialization of contexts using servlet listeners and a web-oriented application context. When using Spring together with WebWork or Struts, this is the package to integrate with.
    • Web Application Context
    • Multipart resolver
  • Web Utilities
  • Spring's Web MVC package provides a Model-View-Controller implementation for web-applications. Spring's MVC implementation is not just any implementation, it provides a clean separation between domain model code and web forms and allows you to use all the other features of the Spring Framework like validation.
    • Web MVC Framework
    • Web views
    • JSP / Velocity
    • PDF / Excel

Mission[edit]

Mission statements of the Frame Springwork:

  • J2EE should be easier to use
  • It is best to program to interfaces, rather than classes.
    Spring reduces the complexity cost of using interfaces to zero.
  • JavaBeans offer a great way of configuring applications.
  • OO design is more important than any implementation technology, such as J2EE.
  • Checked exceptions are overused in Java.
    A platform shouldn't force you to catch exceptions you're unlikely to be able to recover from.
  • Testability is essential, and a platform such as Spring should help make your code easier to test.

Spring's philosophy is summarized in Expert One-on-One J2EE Design and Development by Rod Johnson.
The aim of the Spring Framework:

  • Spring should be a pleasure to use
  • Your application code should not depend on Spring APIs
  • Spring should not compete with good existing solutions, but should foster integration.
    (For example, JDO, Toplink, and Hibernate [3] are great O/R mapping solutions. We don't need to develop another one.)

Usage Scenarios[edit]

With the building blocks described above you can use Spring in all sorts of scenarios, from applets up to fully-fledged enterprise applications using Spring's transaction management functionality and Web framework.

A full-fledges Spring web application
  A typical web application using most of Spring's features.
  • Using TransactionProxyFactoryBeans the web application is fully transactional, just as it would be when using container managed transaction as provided by Enterprise JavaBeans.
  • All your custom business logic can be implemented using simple POJOs, managed by Spring's Dependency Injection container.
  • Additional services such as sending email and validation, independent of the web layer enable you to choose where to execute validation rules. Spring's ORM support is integrated with Hibernate, JDO and iBatis.
  • Using for example HibernateDaoSupport, you can re-use your existing Hibernate mappings.

Form controllers seamlessly integrate the web-layer with the domain model, removing the need for ActionForms or other classes that transform HTTP parameters to values for your domain model.

Spring middle-tier using a third party web framework
  Sometimes the current circumstances do not allow you to completely switch to a different framework.
  • Spring does not force you to use everything within it; it's not an all-or-nothing solution.
  • Existing front-ends using WebWork, Struts, Tapestry, or other UI frameworks can be integrated perfectly well with a Spring-based middle-tier, allowing you to use the transaction features that Spring offers.
  • The only thing you need to do is wire up your business logic using an ApplicationContext and integrate your Web UI layer using a WebApplicationContext.
Spring Remote Scenario
  When you need to access existing code via webservices, you can use Spring's
  • Hessian-
  • Burlap-
  • Rmi-
  • JaxRpcProxyFactory classes.

Enabling remote access to existing application is all of a sudden not that hard anymore.

EJBs - wrapping existing POJOs
  Spring also provides an access layer and abstraction layer for Enterprise JavaBeans, enabling you to reuse your existing POJOs and wrap them in Stateless Session Beans, for use in scalable failsafe webapplications, that might need declarative security.

Features[edit]

Spring is a layered Java/J2EE application platform, based on code published in Expert One-on-One J2EE Design and Development by Rod Johnson.

  • The most complete lightweight container, providing centralized, automated configuration and wiring of your application objects. The container is non-invasive, capable of assembling a complex system from a set of loosely-coupled components (POJOs) in a consistent and transparent fashion. The container brings agility and leverage, and improves application testability and scalability by allowing software components to be first developed and tested in isolation, then scaled up for deployment in any environment (J2SE or J2EE).
  • A common abstraction layer for transaction management, allowing for pluggable transaction managers, and making it easy to demarcate transactions without dealing with low-level issues. Generic strategies for JTA and a single JDBC DataSource are included. In contrast to plain JTA or EJB CMT, Spring's transaction support is not tied to J2EE environments.
  • A JDBC abstraction layer that offers a meaningful exception hierarchy (no more pulling vendor codes out of SQLException), simplifies error handling, and greatly reduces the amount of code you'll need to write. You'll never need to write another finally block to use JDBC again. The JDBC-oriented exceptions comply to Spring's generic DAO exception hierarchy.
  • Integration with Toplink, Hibernate [3], JDO, and iBATIS SQL Maps: in terms of resource holders, DAO implementation support, and transaction strategies. First-class Hibernate [3] support with lots of IoC convenience features, addressing many typical Hibernate integration issues. All of these comply to Spring's generic transaction and DAO exception hierarchies.
  • AOP functionality, fully integrated into Spring configuration management. You can AOP-enable any object managed by Spring, adding aspects such as declarative transaction management. With Spring, you can have declarative transaction management without EJB... even without JTA, if you're using a single database in Tomcat or another web container without JTA support.
  • A flexible MVC web application framework, built on core Spring functionality. This framework is highly configurable via strategy interfaces, and accommodates multiple view technologies like JSP, Velocity, Tiles, iText, and POI. Note that a Spring middle tier can easily be combined with a web tier based on any other web MVC framework, like Struts, WebWork, or Tapestry.

You can use all of Spring's functionality in any J2EE server, and most of it also in non-managed environments. A central focus of Spring is to allow for reusable business and data access objects that are not tied to specific J2EE services. Such objects can be reused across J2EE environments (web or EJB), standalone applications, test environments, etc without any hassle.

Spring's layered architecture gives you a lot of flexibility. All its functionality builds on lower levels. So you can e.g. use the JavaBeans configuration management without using the MVC framework or AOP support. But if you use the web MVC framework or AOP support, you'll find they build on the core Spring configuration, so you can apply your knowledge about it immediately.


References[edit]

  1. wikipedia:Spring_Framework Spring Framework, Description is taken from Wikipedia
  2. ORM, Object Relational Mapping converts data between incompatible type systems in object-oriented programming languages.
  3. 3.0 3.1 3.2 Hibernate Homepage. Hibernate is a wrapper for Database access. See also Hibernate on this wiki.

See also[edit]

Tutorials[edit]